132 lines
3.5 KiB
C#
132 lines
3.5 KiB
C#
using System;
|
|
using FOU.Scripts.Elements;
|
|
using Godot;
|
|
|
|
namespace FOU.Scripts;
|
|
|
|
public class Chunk {
|
|
public Element[,] Elements;
|
|
public Chunk NeighborN = null;
|
|
public Chunk NeighborE = null;
|
|
public Chunk NeighborS = null;
|
|
public Chunk NeighborW = null;
|
|
|
|
private readonly Image image;
|
|
private readonly int sizeX;
|
|
private readonly int sizeY;
|
|
private int frame;
|
|
private Chunk _this;
|
|
|
|
public Chunk(int x, int y) {
|
|
_this = this;
|
|
|
|
sizeX = x;
|
|
sizeY = y;
|
|
|
|
Elements = new Element[x, y];
|
|
for (int i = 0; i < sizeX; i++) {
|
|
for (int j = 0; j < sizeY; j++) {
|
|
Elements[i,j] = new Element(i, j, _this);
|
|
}
|
|
}
|
|
|
|
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
|
image.Fill(Colors.Black);
|
|
}
|
|
|
|
public void Update() {
|
|
for (int x = 0; x < sizeX; x++) {
|
|
for (int y = 0; y < sizeY; y++) {
|
|
if (Elements[x,y] != null)
|
|
Elements[x,y].Update(frame);
|
|
}
|
|
}
|
|
frame++;
|
|
// TODO: enable rain again
|
|
// MakeItRain(_main.RainAmount);
|
|
}
|
|
|
|
public void WritePixel<T>(int x, int y, int size) {
|
|
int halfsize = size/2;
|
|
|
|
Type type = typeof(T);
|
|
|
|
for (int i = -halfsize; i <= halfsize; i++) {
|
|
for (int j = -halfsize; j <= halfsize; j++) {
|
|
int X = Mathf.Clamp(x + i, 0, sizeX-1);
|
|
int Y = Mathf.Clamp(y + j, 0, sizeY-1);
|
|
object o = Activator.CreateInstance(type, X, Y, _this);
|
|
|
|
if (Elements[X,Y].GetType() != type)
|
|
Elements[X,Y] = o as Element;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Image DrawLevel() {
|
|
for (int x = 0; x < sizeX; x++) {
|
|
for (int y = 0; y < sizeY; y++) {
|
|
image.SetPixel(x,y, Elements[x,y].Color);
|
|
}
|
|
}
|
|
return image;
|
|
}
|
|
|
|
public void Swap(Element what, Vector2I pos) {
|
|
Swap(what, Get(pos));
|
|
}
|
|
|
|
public Element Get(Vector2I pos) {
|
|
if (pos.Y == 54)
|
|
GD.Print("magic number");
|
|
|
|
if (pos.Y < 0)
|
|
if (NeighborN != null) return NeighborN.Get(pos.X, NeighborN.sizeY + pos.Y);
|
|
|
|
if (pos.Y >= sizeY)
|
|
if (NeighborS != null) return NeighborS.Get(pos.X, sizeY - pos.Y);
|
|
|
|
if (pos.X < 0)
|
|
if (NeighborE != null) return NeighborE.Get(NeighborE.sizeX + pos.X, pos.Y);
|
|
|
|
if (pos.X >= sizeX)
|
|
if (NeighborW != null) return NeighborW.Get(sizeX - pos.X, pos.Y);
|
|
|
|
return Get(pos.X, pos.Y);
|
|
}
|
|
|
|
private Element Get(int x, int y) {
|
|
if (x < 0 || x >= sizeX) return null;
|
|
if (y < 0 || y >= sizeY) return null;
|
|
|
|
return Elements[x,y];
|
|
}
|
|
|
|
public bool IsEmpty(Vector2I pos) {
|
|
return Get(pos)?.GetType() == typeof(Element);
|
|
}
|
|
|
|
private void Swap(Element what, Element swapTo) {
|
|
Element swap = new Element(what);
|
|
|
|
GD.Print($"Swap: {what} -> {swapTo}");
|
|
if (swapTo.Position.Y == 0) GD.Print($"");
|
|
|
|
if (what == null || swapTo == null) return;
|
|
|
|
what.Position = swapTo.Position;
|
|
what.Chunk = swapTo.Chunk;
|
|
Elements[swapTo.Position.X, swapTo.Position.Y] = what;
|
|
|
|
swapTo.Position = swap.Position;
|
|
swapTo.Chunk = swap.Chunk;
|
|
Elements[swap.Position.X, swap.Position.Y] = swapTo;
|
|
|
|
|
|
what.Active = true;
|
|
swapTo.Active = true;
|
|
what.LastMove = frame;
|
|
swapTo.LastMove = frame;
|
|
}
|
|
}
|