From d431848791adade3ebfce5de89308446fc903e4a Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Thu, 22 Dec 2022 01:10:30 +0000 Subject: [PATCH] wild tiles can heal --- .../resources/tiles/tile_types/wild.tres | 1 + half-earth/scripts/EnvironmentSystem.cs | 48 +++++---------- half-earth/scripts/tile/IDamageable.cs | 1 + half-earth/scripts/tile/TileGrid.cs | 59 +++++++++++++++++++ half-earth/scripts/tile/Wild.cs | 3 + 5 files changed, 79 insertions(+), 33 deletions(-) diff --git a/half-earth/resources/tiles/tile_types/wild.tres b/half-earth/resources/tiles/tile_types/wild.tres index 20cc915..ed76163 100644 --- a/half-earth/resources/tiles/tile_types/wild.tres +++ b/half-earth/resources/tiles/tile_types/wild.tres @@ -8,4 +8,5 @@ Name = "Wild" Key = 87 Color = Color( 0.14902, 1, 0, 0 ) HeatGeneration = -0.02 +HealRate = 0.01 Threshold = 0.6 diff --git a/half-earth/scripts/EnvironmentSystem.cs b/half-earth/scripts/EnvironmentSystem.cs index 16895ed..5fabf8b 100644 --- a/half-earth/scripts/EnvironmentSystem.cs +++ b/half-earth/scripts/EnvironmentSystem.cs @@ -45,13 +45,19 @@ public class EnvironmentSystem var tile = _tiles[i]; var type = tile.type; tile.temperature += type.HeatGeneration; - ApplyHeatDamage(ref 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)) return; @@ -59,46 +65,22 @@ public class EnvironmentSystem if (delta < 0) { // we want to heal based on how many wild tiles surround us - float healRate = 0.01f; + float wild = _tiles.GetNeighbourProportion(x, y); + float heal = damageable.HealRate * wild; - // count neighbours + tile.currentHealth += heal; } else { - // take damage // TODO: parameterised balancing + + // take damage tile.currentHealth -= delta; - tile.currentHealth = Mathf.Clamp(tile.currentHealth, 0, 1); } - } + tile.currentHealth = Mathf.Clamp(tile.currentHealth, 0, 1); - public int GetNeighbourCount(int x, int y) where T : TileType - { - 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; + _tiles[x, y] = tile; } private void Diffuse() diff --git a/half-earth/scripts/tile/IDamageable.cs b/half-earth/scripts/tile/IDamageable.cs index 6279a0d..ba606f2 100644 --- a/half-earth/scripts/tile/IDamageable.cs +++ b/half-earth/scripts/tile/IDamageable.cs @@ -1,4 +1,5 @@ public interface IDamageable { + float HealRate { get; } float Threshold { get; } } \ No newline at end of file diff --git a/half-earth/scripts/tile/TileGrid.cs b/half-earth/scripts/tile/TileGrid.cs index ef2c812..7ea685e 100644 --- a/half-earth/scripts/tile/TileGrid.cs +++ b/half-earth/scripts/tile/TileGrid.cs @@ -89,5 +89,64 @@ public class TileGrid : Resource, IReadOnlyList { 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(int x, int y) + { + int typed = GetNeighbourCount(x, y); + int all = GetNeighbourCount(x, y); + return (float)typed / (float)all; + } + + 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; + + 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; + } } diff --git a/half-earth/scripts/tile/Wild.cs b/half-earth/scripts/tile/Wild.cs index 3697cb3..b058b44 100644 --- a/half-earth/scripts/tile/Wild.cs +++ b/half-earth/scripts/tile/Wild.cs @@ -2,6 +2,9 @@ using Godot; public class Wild : TileType, IDamageable { + [Export] + public float HealRate { get; private set; } = 0.01f; + [Export] public float Threshold { get; private set; }