added chunks
This commit is contained in:
153
Scripts/Level.cs
153
Scripts/Level.cs
@@ -1,123 +1,84 @@
|
||||
using System;
|
||||
using FOU.Scripts.Elements;
|
||||
using Godot;
|
||||
using Godot;
|
||||
|
||||
namespace FOU.Scripts;
|
||||
|
||||
public class Level {
|
||||
public int SizeX;
|
||||
public int SizeY;
|
||||
private Image mImage;
|
||||
private int _frame;
|
||||
|
||||
private Element[,] _elements;
|
||||
private Level _this;
|
||||
private Main _main;
|
||||
public Vector2I Resolution;
|
||||
|
||||
public Level(int x, int y, Main main) {
|
||||
GD.Print($"Generating level ({x}:{y})");
|
||||
_this = this;
|
||||
_main = main;
|
||||
private Chunk[,] chunks;
|
||||
private Image image;
|
||||
|
||||
SizeX = x;
|
||||
SizeY = y;
|
||||
private int chunksX;
|
||||
private int chunksY;
|
||||
|
||||
_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);
|
||||
// per chunk:
|
||||
private int chunkResX;
|
||||
private int chunkResY;
|
||||
|
||||
public Level(Main main, int sizeX, int sizeY) {
|
||||
GD.Print($"Generating level ({sizeX}:{sizeY})");
|
||||
|
||||
Resolution = new Vector2I(sizeX, sizeY);
|
||||
chunksX = main.ChunksPerAxis;
|
||||
chunksY = main.ChunksPerAxis;
|
||||
|
||||
chunkResX = sizeX / chunksX;
|
||||
chunkResY = sizeY / chunksY;
|
||||
|
||||
image = Image.Create(sizeX, sizeY, false, Image.Format.Rgb8);
|
||||
|
||||
chunks = new Chunk[chunksX, chunksY];
|
||||
// create all chunks
|
||||
for (int x = 0; x < chunksX; x++) {
|
||||
for (int y = 0; y < chunksY; y++) {
|
||||
chunks[x, y] = new Chunk(chunkResX, chunkResY);
|
||||
}
|
||||
}
|
||||
|
||||
mImage = Image.Create(SizeX, SizeY, false, Image.Format.Rgb8);
|
||||
mImage.Fill(Colors.Black);
|
||||
// assign neighbors
|
||||
for (int x = 0; x < chunksX; x++) {
|
||||
for (int y = 0; y < chunksY; y++) {
|
||||
|
||||
if (x > 0) chunks[x, y].NeighborN = chunks[x-1, y];
|
||||
if (x < chunksX-1) chunks[x, y].NeighborS = chunks[x+1, y];
|
||||
|
||||
if (y > 0) chunks[x, y].NeighborW = chunks[x, y-1];
|
||||
if (y < chunksY-1) chunks[x, y].NeighborE = chunks[x, y+1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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++;
|
||||
MakeItRain(_main.RainAmount);
|
||||
foreach (Chunk c in chunks)
|
||||
c.Update();
|
||||
}
|
||||
|
||||
// TODO!
|
||||
private void MakeItRain(float rainAmount) {
|
||||
int rainDrops = (int)Math.Round(rainAmount);
|
||||
|
||||
for (int i = 0; i <= rainDrops; i++) {
|
||||
if (GD.Randf() < rainAmount)
|
||||
WritePixel<Water>((int)(GD.Randi() % SizeX), 0, 1);
|
||||
}
|
||||
// int rainDrops = (int)Math.Round(rainAmount);
|
||||
//
|
||||
// for (int i = 0; i <= rainDrops; i++) {
|
||||
// if (GD.Randf() < rainAmount)
|
||||
// WritePixel<Water>((int)(GD.Randi() % SizeX), 0, 1);
|
||||
// }
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
chunks[x/chunkResX, y/chunkResY].WritePixel<T>(x % chunkResX, y % chunkResY, size);
|
||||
}
|
||||
|
||||
public Image DrawLevel() {
|
||||
for (int x = 0; x < SizeX; x++) {
|
||||
for (int y = 0; y < SizeY; y++) {
|
||||
mImage.SetPixel(x,y, _elements[x,y].Color);
|
||||
for (int cx = 0; cx < chunksX; cx++) {
|
||||
for (int cy = 0; cy < chunksY; cy++) {
|
||||
|
||||
for (int x = 0; x < chunkResX; x++) {
|
||||
for (int y = 0; y < chunkResY; y++) {
|
||||
image.SetPixel(cx*chunkResX + x, cy*chunkResY + y, chunks[cx,cy].Elements[x, y].Color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mImage;
|
||||
}
|
||||
|
||||
private void Swap(Element what, Element swapTo) {
|
||||
Element swap = new Element(what);
|
||||
|
||||
if (what == null || swapTo == null) return;
|
||||
|
||||
what.Position = swapTo.Position;
|
||||
_elements[swapTo.Position.X, swapTo.Position.Y] = what;
|
||||
|
||||
swapTo.Position = swap.Position;
|
||||
_elements[swap.Position.X, swap.Position.Y] = swapTo;
|
||||
|
||||
what.Active = true;
|
||||
swapTo.Active = true;
|
||||
what.LastMove = _frame;
|
||||
swapTo.LastMove = _frame;
|
||||
}
|
||||
|
||||
public void Swap(Element what, Vector2I pos) {
|
||||
Swap(what, Get(pos));
|
||||
}
|
||||
|
||||
public Element Get(Vector2I where) {
|
||||
if (where.X < 0 || where.X >= SizeX) return null;
|
||||
if (where.Y < 0 || where.Y >= SizeY) return null;
|
||||
|
||||
return _elements[where.X, where.Y];
|
||||
}
|
||||
|
||||
public 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) {
|
||||
if (pos.X < 0 || pos.X >= SizeX) return false;
|
||||
if (pos.Y < 0 || pos.Y >= SizeY) return false;
|
||||
|
||||
return Get(pos).GetType() == typeof(Element);
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user