wild tiles can heal

This commit is contained in:
Cat Flynn 2022-12-22 01:10:30 +00:00
parent 78e4c6ee9e
commit d431848791
5 changed files with 79 additions and 33 deletions

View File

@ -8,4 +8,5 @@ Name = "Wild"
Key = 87 Key = 87
Color = Color( 0.14902, 1, 0, 0 ) Color = Color( 0.14902, 1, 0, 0 )
HeatGeneration = -0.02 HeatGeneration = -0.02
HealRate = 0.01
Threshold = 0.6 Threshold = 0.6

View File

@ -45,13 +45,19 @@ public class EnvironmentSystem
var tile = _tiles[i]; var tile = _tiles[i];
var type = tile.type; var type = tile.type;
tile.temperature += type.HeatGeneration; tile.temperature += type.HeatGeneration;
ApplyHeatDamage(ref tile);
_tiles[i] = tile; _tiles[i] = tile;
} }
for (int i = 0; i < _tiles.Count; i++)
{
ApplyHeatDamage(i);
}
} }
private void ApplyHeatDamage(ref Tile tile) private void ApplyHeatDamage(int i)
{ {
_tiles.MapIndex(i, out var x, out var y);
var tile = _tiles[x, y];
if (!(tile.type is IDamageable damageable)) if (!(tile.type is IDamageable damageable))
return; return;
@ -59,46 +65,22 @@ public class EnvironmentSystem
if (delta < 0) if (delta < 0)
{ {
// we want to heal based on how many wild tiles surround us // we want to heal based on how many wild tiles surround us
float healRate = 0.01f; float wild = _tiles.GetNeighbourProportion<Wild>(x, y);
float heal = damageable.HealRate * wild;
// count neighbours tile.currentHealth += heal;
} }
else else
{ {
// take damage
// TODO: parameterised balancing // TODO: parameterised balancing
// take damage
tile.currentHealth -= delta; tile.currentHealth -= delta;
tile.currentHealth = Mathf.Clamp(tile.currentHealth, 0, 1);
} }
} tile.currentHealth = Mathf.Clamp(tile.currentHealth, 0, 1);
public int GetNeighbourCount<T>(int x, int y) where T : TileType _tiles[x, y] = tile;
{
int count = 0;
for (int i = -1; i < 1; i += 2)
{
for (int j = -1; j < 1; j += 2)
{
int xi = x + 1;
int yj = y + 1;
// assume tiles out side of bounds are the same type as this one
if (!_tiles.IsInBounds(xi, yj))
{
count += 1;
continue;
}
if (!(_tiles[xi, yj] is T))
continue;
count++;
}
}
return count;
} }
private void Diffuse() private void Diffuse()

View File

@ -1,4 +1,5 @@
public interface IDamageable public interface IDamageable
{ {
float HealRate { get; }
float Threshold { get; } float Threshold { get; }
} }

View File

@ -89,5 +89,64 @@ public class TileGrid : Resource, IReadOnlyList<Tile>
{ {
return x >= 0 && x < Size && y >= 0 && y < Size; return x >= 0 && x < Size && y >= 0 && y < Size;
} }
private readonly int[] _neighbours = new int[]
{
-1, 0,
0, 1,
1, 0,
0, -1
};
private void GetCoordinate(int dir, out int x, out int y)
{
dir %= 4;
int idx = dir * 2;
x = _neighbours[idx];
y = _neighbours[idx + 1];
}
public float GetNeighbourProportion<T>(int x, int y)
{
int typed = GetNeighbourCount<T>(x, y);
int all = GetNeighbourCount(x, y);
return (float)typed / (float)all;
}
public int GetNeighbourCount<T>(int x, int y)
{
int count = 0;
for (int i = 0; i < 4; i++)
{
GetCoordinate(i, out var nx, out var ny);
if (!IsInBounds(x + nx, y + ny))
continue;
if (!(this[x + nx, y + ny].type is T))
continue;
count++;
}
return count;
}
public int GetNeighbourCount(int x, int y)
{
int count = 0;
for (int i = 0; i < 4; i++)
{
GetCoordinate(i, out var nx, out var ny);
if (!IsInBounds(x + nx, y + ny))
continue;
count++;
}
return count;
}
} }

View File

@ -2,6 +2,9 @@ using Godot;
public class Wild : TileType, IDamageable public class Wild : TileType, IDamageable
{ {
[Export]
public float HealRate { get; private set; } = 0.01f;
[Export] [Export]
public float Threshold { get; private set; } public float Threshold { get; private set; }