added chunks
This commit is contained in:
128
Scripts/Chunk.cs
Normal file
128
Scripts/Chunk.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
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) {
|
||||
// North
|
||||
if (pos.X < 0)
|
||||
if (NeighborN != null) return NeighborN.Get(NeighborN.sizeX + pos.X, pos.Y);
|
||||
|
||||
// South
|
||||
if (pos.X >= sizeX)
|
||||
if (NeighborS != null) return NeighborS.Get(sizeX - pos.X, pos.Y);
|
||||
|
||||
// West
|
||||
if (pos.Y < 0)
|
||||
if (NeighborW != null) return NeighborW.Get(pos.X, NeighborW.sizeY + pos.Y);
|
||||
|
||||
// East
|
||||
if (pos.Y >= sizeY)
|
||||
if (NeighborE != null) return NeighborE.Get(pos.X, sizeY - 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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user