hierarchy rework
fixed swap
This commit is contained in:
@@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
[node name="Main" type="Node2D"]
|
[node name="Main" type="Node2D"]
|
||||||
script = ExtResource("1_k1i8e")
|
script = ExtResource("1_k1i8e")
|
||||||
BrushSize = 3
|
BrushSize = 1
|
||||||
TextureResolution = 0.25
|
TextureResolution = 0.25
|
||||||
RainAmount = 0.15
|
RainAmount = 1.0
|
||||||
|
|
||||||
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
[node name="CanvasLayer" type="CanvasLayer" parent="."]
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,8 @@ public class Dirt : Solid {
|
|||||||
public override bool Update(int currentFrame) {
|
public override bool Update(int currentFrame) {
|
||||||
if (!base.Update(currentFrame)) return false;
|
if (!base.Update(currentFrame)) return false;
|
||||||
|
|
||||||
Vector2I freePos = Check(this, Vector2I.Down + Vector2I.Right);
|
Tick();
|
||||||
|
|
||||||
if (freePos != Vector2I.Zero) { // diagonally right
|
|
||||||
Level.Swap(this, freePos);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true; // not necessarily end, subclasses could do some more things
|
return true; // not necessarily end, subclasses could do some more things
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ public class Element {
|
|||||||
|
|
||||||
private int lastUpdate = -1;
|
private int lastUpdate = -1;
|
||||||
|
|
||||||
|
public Element(Element e) {
|
||||||
|
Position = e.Position;
|
||||||
|
Level = e.Level;
|
||||||
|
}
|
||||||
|
|
||||||
public Element(int x, int y, Level level) {
|
public Element(int x, int y, Level level) {
|
||||||
Position.X = x;
|
Position.X = x;
|
||||||
Position.Y = y;
|
Position.Y = y;
|
||||||
@@ -34,24 +39,20 @@ public class Element {
|
|||||||
return $"{GetType()} {Position}";
|
return $"{GetType()} {Position}";
|
||||||
}
|
}
|
||||||
|
|
||||||
// OBSOLETE:
|
protected virtual void Tick() {
|
||||||
// protected bool CheckBelow(int sourceX, int sourceY) {
|
int randomDirection = 1;
|
||||||
// if (sourceY+1 >= _level.SizeY) return false;
|
if (GD.Randi() % 2 != 0)
|
||||||
//
|
randomDirection *= -1;
|
||||||
// if (_level.Get(sourceX, sourceY+1).GetType() == GetType())
|
|
||||||
// return false;
|
|
||||||
//
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// <summary>
|
if (Level.IsEmpty(Position + Vector2I.Down)) {
|
||||||
/// Checks from source to maxDirection (also X-mirrored) and returns a free position
|
Level.Swap(this, Position + Vector2I.Down);
|
||||||
/// </summary>
|
|
||||||
/// <param name="source">from where to check</param>
|
} else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection)) {
|
||||||
/// <param name="maxDirection">where to go to (max). X is treated as [-X..X]</param>
|
Level.Swap(this, Position + Vector2I.Right * randomDirection);
|
||||||
/// <returns>free position or V2.zero if nothing was found</returns>
|
|
||||||
protected virtual Vector2I Check(Element source, Vector2I maxDirection) {
|
} else if (Level.IsEmpty(Position + Vector2I.Down + Vector2I.Right * randomDirection * -1)) {
|
||||||
return Vector2I.Zero;
|
Level.Swap(this, Position + Vector2I.Right*randomDirection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Color AddColorVariance(Color baseColor) {
|
protected Color AddColorVariance(Color baseColor) {
|
||||||
|
|||||||
@@ -1,39 +1,9 @@
|
|||||||
using Godot;
|
using System;
|
||||||
|
using Godot;
|
||||||
|
|
||||||
namespace FOU.Scripts.Elements;
|
namespace FOU.Scripts.Elements;
|
||||||
|
|
||||||
public abstract class Liquid : Element {
|
public abstract class Liquid : Element {
|
||||||
protected Liquid(int x, int y, ref Level level) : base(x, y, level) { }
|
protected Liquid(int x, int y, ref Level level) : base(x, y, level) { }
|
||||||
|
|
||||||
|
|
||||||
// TODO: rework this!
|
|
||||||
protected override Vector2I Check(Element source, Vector2I maxDirection) {
|
|
||||||
if (GD.Randi() % 2 != 0)
|
|
||||||
maxDirection.X *= -1;
|
|
||||||
|
|
||||||
Vector2I freePos = Vector2I.Zero;
|
|
||||||
for (int y = maxDirection.Y; y > 0; y--) { // height
|
|
||||||
if (source.Position.Y + y >= Level.SizeY) return freePos; // bounds check
|
|
||||||
|
|
||||||
// check if we can further
|
|
||||||
if (Level.Get(source.Position.X, source.Position.Y + y).GetType() == typeof(Element)) {
|
|
||||||
freePos = new Vector2I(source.Position.X, source.Position.Y + y);
|
|
||||||
} else {
|
|
||||||
freePos = Vector2I.Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = -maxDirection.X; x <= maxDirection.X; x++) { // width
|
|
||||||
if (freePos != Vector2I.Zero) break;
|
|
||||||
|
|
||||||
if (source.Position.X + x >= Level.SizeX || source.Position.X + x < 0) continue; // bounds check
|
|
||||||
|
|
||||||
if (Level.Get(source.Position.X + x, source.Position.Y + y).GetType() == typeof(Element)) {
|
|
||||||
freePos = new Vector2I(source.Position.X + x, source.Position.Y + y);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return freePos;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
using Godot;
|
|
||||||
|
|
||||||
namespace FOU.Scripts.Elements;
|
|
||||||
|
|
||||||
public class Snow : Solid {
|
|
||||||
public Snow(int x, int y, ref Level level) : base(x, y, ref level) {
|
|
||||||
Color = Colors.White;
|
|
||||||
Color = AddColorVariance(Color);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool Update(int currentFrame) {
|
|
||||||
if (!base.Update(currentFrame)) return false;
|
|
||||||
|
|
||||||
Vector2I freePos = Check(this, Vector2I.Down);
|
|
||||||
|
|
||||||
if (freePos != Vector2I.Zero) { // diagonally right
|
|
||||||
Level.Swap(this, freePos);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true; // not necessarily end, subclasses could do some more things
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,32 +5,4 @@ namespace FOU.Scripts.Elements;
|
|||||||
public abstract class Solid : Element {
|
public abstract class Solid : Element {
|
||||||
protected Solid(int x, int y, ref Level level) : base(x, y, level) { }
|
protected Solid(int x, int y, ref Level level) : base(x, y, level) { }
|
||||||
|
|
||||||
protected Vector2I Check(Element source, Vector2I maxDirection) {
|
|
||||||
if (GD.Randi() % 2 != 0)
|
|
||||||
maxDirection.X *= -1;
|
|
||||||
|
|
||||||
Vector2I freePos = Vector2I.Zero;
|
|
||||||
for (int y = maxDirection.Y; y > 0; y--) { // height
|
|
||||||
if (source.Position.Y + y >= Level.SizeY) return freePos; // bounds check
|
|
||||||
|
|
||||||
if (Level.Get(source.Position.X, source.Position.Y + y).GetType() == typeof(Element)) {
|
|
||||||
freePos = new Vector2I(source.Position.X, source.Position.Y + y);
|
|
||||||
} else {
|
|
||||||
freePos = Vector2I.Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int x = -maxDirection.X; x <= maxDirection.X; x++) { // width
|
|
||||||
if (freePos != Vector2I.Zero) break;
|
|
||||||
|
|
||||||
if (source.Position.X + x >= Level.SizeX || source.Position.X + x < 0) return freePos; // bounds check
|
|
||||||
|
|
||||||
if (Level.Get(source.Position.X + x, source.Position.Y + y).GetType() == typeof(Element)) {
|
|
||||||
freePos = new Vector2I(source.Position.X + x, source.Position.Y + y);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return freePos;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
Scripts/Elements/Water.cs
Normal file
35
Scripts/Elements/Water.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace FOU.Scripts.Elements;
|
||||||
|
|
||||||
|
public class Water : Solid {
|
||||||
|
public Water(int x, int y, ref Level level) : base(x, y, ref level) {
|
||||||
|
Color = Colors.Blue;
|
||||||
|
Color = AddColorVariance(Color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Update(int currentFrame) {
|
||||||
|
if (!base.Update(currentFrame)) return false;
|
||||||
|
|
||||||
|
Tick();
|
||||||
|
|
||||||
|
return true; // not necessarily end, subclasses could do some more things
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Tick() {
|
||||||
|
int randomDirection = 1;
|
||||||
|
if (GD.Randi() % 2 != 0)
|
||||||
|
randomDirection *= -1;
|
||||||
|
|
||||||
|
if (Level.IsEmpty(Position + Vector2I.Down)) {
|
||||||
|
Level.Swap(this, Position + Vector2I.Down);
|
||||||
|
return;
|
||||||
|
} else if (Level.IsEmpty(Position + Vector2I.Right * randomDirection)) {
|
||||||
|
Level.Swap(this, Position + Vector2I.Right * randomDirection);
|
||||||
|
return;
|
||||||
|
} else if (Level.IsEmpty(Position + Vector2I.Right * randomDirection * -1)) {
|
||||||
|
Level.Swap(this, Position + Vector2I.Right*randomDirection);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,7 +46,7 @@ public class Level {
|
|||||||
|
|
||||||
private void MakeItRain(float rainAmount) {
|
private void MakeItRain(float rainAmount) {
|
||||||
if (GD.Randf() < rainAmount) {
|
if (GD.Randf() < rainAmount) {
|
||||||
WritePixel<Snow>((int)(GD.Randi() % SizeX), 0, 1);
|
WritePixel<Water>((int)(GD.Randi() % SizeX), 0, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,20 +69,25 @@ public class Level {
|
|||||||
public Image DrawLevel() {
|
public Image DrawLevel() {
|
||||||
for (int x = 0; x < SizeX; x++) {
|
for (int x = 0; x < SizeX; x++) {
|
||||||
for (int y = 0; y < SizeY; y++) {
|
for (int y = 0; y < SizeY; y++) {
|
||||||
// if (_elements[x,y] == null) continue;
|
|
||||||
|
|
||||||
mImage.SetPixel(x,y, _elements[x,y].Color);
|
mImage.SetPixel(x,y, _elements[x,y].Color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mImage;
|
return mImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Swap(Element what, Vector2I swapTo) {
|
public void Swap(Element what, Element swapTo) {
|
||||||
Element old = Get(swapTo);
|
Element swap = new Element(what);
|
||||||
Set(old, what.Position);
|
|
||||||
Set(what, swapTo);
|
what.Position = swapTo.Position;
|
||||||
|
_elements[swapTo.Position.X, swapTo.Position.Y] = what;
|
||||||
|
|
||||||
|
swapTo.Position = swap.Position;
|
||||||
|
_elements[swap.Position.X, swap.Position.Y] = swapTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Swap(Element what, Vector2I pos) {
|
||||||
|
Swap(what, Get(pos));
|
||||||
|
}
|
||||||
|
|
||||||
public Element Get(Vector2I where) {
|
public Element Get(Vector2I where) {
|
||||||
if (where.X < 0 || where.X >= SizeX) return null;
|
if (where.X < 0 || where.X >= SizeX) return null;
|
||||||
@@ -98,10 +103,10 @@ public class Level {
|
|||||||
return _elements[x,y];
|
return _elements[x,y];
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Set(Element what, Vector2I where) {
|
public bool IsEmpty(Vector2I pos) {
|
||||||
if (what == null) return;
|
if (pos.X < 0 || pos.X >= SizeX) return false;
|
||||||
|
if (pos.Y < 0 || pos.Y >= SizeY) return false;
|
||||||
|
|
||||||
what.Position = where;
|
return Get(pos).GetType() == typeof(Element);
|
||||||
_elements[where.X, where.Y] = what;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ public partial class Main : Node2D {
|
|||||||
float mappedX = mouse.X / GetViewportRect().Size.X * mLevel.SizeX;
|
float mappedX = mouse.X / GetViewportRect().Size.X * mLevel.SizeX;
|
||||||
float mappedY = mouse.Y / GetViewportRect().Size.Y * mLevel.SizeY;
|
float mappedY = mouse.Y / GetViewportRect().Size.Y * mLevel.SizeY;
|
||||||
|
|
||||||
|
GD.Print($"click: {mouse}");
|
||||||
mLevel.WritePixel<Dirt>((int)mappedX, (int)mappedY, BrushSize);
|
mLevel.WritePixel<Dirt>((int)mappedX, (int)mappedY, BrushSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user