still working on movement, for some reason some cells do not get set as solid that's why multiple can go on a cell
This commit is contained in:
@@ -7,11 +7,20 @@ public partial class Bucket : Node2D
|
||||
[Signal]
|
||||
public delegate void AttackEnteredEventHandler(Attack ATTACK);
|
||||
public int _minX = -500, _maxX = 500, _movementSign = 1, _movementSpeed = 3;
|
||||
public Vector2 _startPoisition;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
_startPoisition = GlobalPosition;
|
||||
}
|
||||
|
||||
|
||||
public void Move()
|
||||
{
|
||||
Position += new Vector2(_movementSign * _movementSpeed, 0);
|
||||
if (Position.X >= _maxX || Position.X <= _minX)
|
||||
GlobalPosition += new Vector2(_movementSign * _movementSpeed, 0);
|
||||
Vector2 offset = _startPoisition - GlobalPosition;
|
||||
if (offset.X >= _maxX || offset.X <= _minX)
|
||||
{
|
||||
_movementSign *= -1;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ public partial class Enemy : StaticBody2D
|
||||
{
|
||||
[Signal]
|
||||
public delegate void DeathEventHandler(Enemy THIS);
|
||||
public bool _hovered = false;
|
||||
public bool _hovered = false, _track = false;
|
||||
public int _damage = 1, _health = 2, _speed, _speedRemaining, _visibilityRange = 4;
|
||||
public Vector2I _address, _range = Vector2I.Up;
|
||||
public List<Vector2I> _path = new();
|
||||
|
||||
+97
-57
@@ -1,6 +1,7 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
public partial class EnemyController : TurnController
|
||||
@@ -46,50 +47,110 @@ public partial class EnemyController : TurnController
|
||||
|
||||
}
|
||||
|
||||
public Vector2I GetBestGoal(Enemy ENEMY, Vector2I GOAL)
|
||||
{
|
||||
Vector2I goal = GOAL;
|
||||
while (_playArea._map.IsRowFull(goal.Y))
|
||||
{
|
||||
goal.Y++;
|
||||
}
|
||||
int loop = 0;
|
||||
while (_playArea._map.HasOccupant(goal))
|
||||
{
|
||||
Vector2I leftGoal = new (Math.Clamp(ENEMY._address.X - loop / 2 - 1, _playArea._map._minX, _playArea._map._maxX), goal.Y);
|
||||
Vector2I rightGoal = new (Math.Clamp(ENEMY._address.X + loop / 2 + 1, _playArea._map._minX, _playArea._map._maxX), goal.Y);
|
||||
List<Vector2I> leftPath = _playArea._map.GetPath(ENEMY._address, leftGoal);
|
||||
List<Vector2I> rightPath = _playArea._map.GetPath(ENEMY._address, rightGoal);
|
||||
|
||||
if (rightPath.Count < leftPath.Count && !_playArea._map.HasOccupant(rightGoal))
|
||||
{
|
||||
goal = rightGoal;
|
||||
}
|
||||
else if (leftPath.Count < rightPath.Count && !_playArea._map.HasOccupant(leftGoal))
|
||||
{
|
||||
goal = leftGoal;
|
||||
}
|
||||
else if (!_playArea._map.HasOccupant(rightGoal))
|
||||
{
|
||||
goal = rightGoal;
|
||||
}
|
||||
else if (!_playArea._map.HasOccupant(leftGoal))
|
||||
{
|
||||
goal = leftGoal;
|
||||
}
|
||||
loop++;
|
||||
}
|
||||
return goal;
|
||||
}
|
||||
|
||||
public void Initiate()
|
||||
{
|
||||
List<Vector2I> positions = [.. _playArea.GetNode<TileMapLayer>("InitialPositions").GetUsedCells()];
|
||||
|
||||
AddEnemies(positions);
|
||||
|
||||
|
||||
// for (int i = 0; i < _playArea._map._cells.Count; i++)
|
||||
// {
|
||||
// if (_playArea._map._astar.IsPointSolid(_playArea._map._cells[i]))
|
||||
// {
|
||||
// GD.Print(_playArea._map._cells[i]);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public void MoveEnemies()
|
||||
{
|
||||
Tween tween = CreateTween();
|
||||
tween.SetParallel();
|
||||
|
||||
for (int i = 0; i < _enemies.Count; i++)
|
||||
{
|
||||
Enemy enemy = _enemies[i];
|
||||
ShiftGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
|
||||
enemy._speedRemaining = enemy._address.Y <= _playArea._map._minY ? 0 : enemy._speed;
|
||||
if (enemy._speedRemaining > 0)
|
||||
{
|
||||
Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
|
||||
enemy._path = _playArea._map.GetPath(enemy._address, goal);
|
||||
}
|
||||
}
|
||||
_enemies = [.. _enemies.OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X))];
|
||||
int maxSpeed = _enemies.Max(e => e._speedRemaining);
|
||||
|
||||
List<Enemy> remainingEnemies = [.. _enemies];
|
||||
|
||||
for (int i = 0; i < maxSpeed; i++)
|
||||
List<Enemy> remainingEnemies = SortEnemies(_enemies);
|
||||
_enemies.ForEach(e => e._path.Clear());
|
||||
if (remainingEnemies.Count == 0)
|
||||
{
|
||||
for (int j = 0; j < remainingEnemies.Count; j++)
|
||||
return;
|
||||
}
|
||||
while (remainingEnemies.Count > 0)
|
||||
{
|
||||
Enemy enemy = remainingEnemies[j];
|
||||
remainingEnemies = SortEnemies(remainingEnemies);
|
||||
for (int i = 0; i < remainingEnemies.Count; i++)
|
||||
{
|
||||
Enemy enemy = remainingEnemies[i];
|
||||
if (enemy._speedRemaining <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (enemy._address.Y <= _playArea._map._minY)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
|
||||
|
||||
int pathIndex = enemy._speed - enemy._speedRemaining;
|
||||
|
||||
enemy._path.InsertRange(0, [.. Enumerable.Repeat(enemy._path[0], pathIndex)]);
|
||||
Vector2I cell = enemy._path[pathIndex];
|
||||
Enemy obstacle = _playArea._map.GetOccupant(cell);
|
||||
List<Vector2I> path = _playArea._map.GetPath(enemy._address, goal);
|
||||
Vector2I cell = path[0];
|
||||
if (enemy._track)
|
||||
{
|
||||
GD.Print(cell, _playArea._map._astar.IsPointSolid(cell));
|
||||
}
|
||||
_playArea._map.SetCellEnemy(cell, enemy);
|
||||
enemy._path.Add(cell);
|
||||
enemy._speedRemaining--;
|
||||
}
|
||||
}
|
||||
|
||||
Tween tween = CreateTween();
|
||||
tween.SetParallel();
|
||||
|
||||
List<Enemy> movingEnemies = [.. _enemies.Where(e => e._path.Count > 0)];
|
||||
int maxSteps = movingEnemies.Max(e => e._path.Count);
|
||||
for (int i = 0; i < maxSteps; i++)
|
||||
{
|
||||
for (int j = 0; j < movingEnemies.Count; j++)
|
||||
{
|
||||
Enemy mEnemy = movingEnemies[j];
|
||||
if (mEnemy._path.Count <= i)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (j == 0)
|
||||
{
|
||||
tween.Chain();
|
||||
@@ -98,25 +159,12 @@ public partial class EnemyController : TurnController
|
||||
{
|
||||
tween.Parallel();
|
||||
}
|
||||
tween.TweenProperty(enemy, "global_position", _playArea._map.GetCellPositionFromAddress(enemy._address), .2f);
|
||||
|
||||
if (enemy._address.Y <= _playArea._map._minY)
|
||||
{
|
||||
remainingEnemies.Remove(enemy);
|
||||
continue;
|
||||
}
|
||||
enemy._speedRemaining--;
|
||||
if (enemy._speedRemaining <= 0)
|
||||
{
|
||||
remainingEnemies.Remove(enemy);
|
||||
continue;
|
||||
}
|
||||
ShiftGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
|
||||
|
||||
tween.TweenProperty(mEnemy, "global_position", _playArea._map.GetCellPositionFromAddress(mEnemy._path[i]), .25f);
|
||||
}
|
||||
}
|
||||
|
||||
tween.TweenCallback(Callable.From(() => EmitSignal(SignalName.TurnDone)));
|
||||
_playArea._map.HighlightCells();
|
||||
}
|
||||
|
||||
public void RemoveEnemy(Enemy ENEMY_TO_REMOVE)
|
||||
@@ -126,6 +174,11 @@ public partial class EnemyController : TurnController
|
||||
ENEMY_TO_REMOVE.QueueFree();
|
||||
}
|
||||
|
||||
public List<Enemy> SortEnemies(List<Enemy> ENEMIES)
|
||||
{
|
||||
return [.. ENEMIES.Where(e => e._speedRemaining > 0 && _playArea._map.GetPath(e._address, new Vector2I(e._address.X, Math.Max(e._address.Y - e._visibilityRange, _playArea._map._minY))).Count > 0).OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
|
||||
}
|
||||
|
||||
public override void StartTurn()
|
||||
{
|
||||
AddEnemies(1);
|
||||
@@ -136,25 +189,12 @@ public partial class EnemyController : TurnController
|
||||
|
||||
public void SetEnemy(Enemy ENEMY, Vector2I CELL)
|
||||
{
|
||||
if (CELL == new Vector2I(8, 13))
|
||||
{
|
||||
ENEMY._track = true;
|
||||
}
|
||||
_playArea._map.SetCellEnemy(CELL, ENEMY);
|
||||
ENEMY.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL);
|
||||
}
|
||||
|
||||
public void ShiftGoal(Enemy ENEMY, Vector2I GOAL)
|
||||
{
|
||||
while(_playArea._map.IsRowFull(GOAL.Y))
|
||||
{
|
||||
GOAL.Y++;
|
||||
}
|
||||
int loop = 0;
|
||||
while (_playArea._map.GetOccupant(GOAL) != null)
|
||||
{
|
||||
GD.Print(ENEMY._address, "-->", GOAL);
|
||||
GOAL.X = Math.Clamp(ENEMY._address.X + ((int)MathF.Floor(loop / 2) + 1) * (loop % 2 == 0 ? 1 : -1), _playArea._map._minX, _playArea._map._maxX);
|
||||
loop++;
|
||||
}
|
||||
|
||||
ENEMY._path = _playArea._map.GetPath(ENEMY._address, GOAL);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
|
||||
public partial class Map : TileMapLayer
|
||||
@@ -33,17 +34,64 @@ public partial class Map : TileMapLayer
|
||||
SetupAstar();
|
||||
}
|
||||
|
||||
|
||||
public Vector2I GetAddressFromPosition(Vector2 POSITION)
|
||||
{
|
||||
if (POSITION.X < GlobalPosition.X || POSITION.Y < GlobalPosition.Y)
|
||||
{
|
||||
return new Vector2I(-1,-1);
|
||||
}
|
||||
else if (POSITION.X > (GlobalPosition + (new Vector2(_maxX, _maxY) * _cellSize)).X || POSITION.Y > (GlobalPosition + (new Vector2(_maxX, _maxY) * _cellSize)).Y)
|
||||
{
|
||||
return new Vector2I(-1,-1);
|
||||
}
|
||||
return new Vector2I((int)Math.Floor(POSITION.X / _cellSize.X), (int)Math.Floor(POSITION.Y / _cellSize.Y));
|
||||
}
|
||||
public Vector2 GetCellPositionFromAddress(Vector2I CELL_ADDRESS)
|
||||
{
|
||||
return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2;
|
||||
}
|
||||
|
||||
public int GetFirstOpenRow()
|
||||
{
|
||||
for (int i = 0; i < _maxY; i++)
|
||||
{
|
||||
if (!IsRowFull(i))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
public Enemy GetOccupant(Vector2I CELL_TO_CHECK)
|
||||
{
|
||||
return _addressOccupants[CELL_TO_CHECK];
|
||||
}
|
||||
|
||||
public bool HasOccupant(Vector2I CELL_TO_CHECK)
|
||||
{
|
||||
return GetOccupant(CELL_TO_CHECK) != null;
|
||||
}
|
||||
|
||||
public void HighlightCells()
|
||||
{
|
||||
_cells.ForEach(c =>
|
||||
{
|
||||
if (HasOccupant(c))
|
||||
{
|
||||
SetCell(c, 0, new Vector2I(4,1));
|
||||
}
|
||||
else if (_astar.IsPointSolid(c))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCell(c, 0, new Vector2I(0, (c.X + c.Y) % 2 == 0 ? 0 : 1));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public bool IsCellSolid(Vector2I CELL_TO_CHECK)
|
||||
{
|
||||
bool hasOccupant = GetOccupant(CELL_TO_CHECK) != null;
|
||||
@@ -67,32 +115,28 @@ public partial class Map : TileMapLayer
|
||||
}
|
||||
|
||||
public void SetCellEnemy(Vector2I ADDRESS, Enemy ENEMY)
|
||||
{
|
||||
if (ENEMY != null)
|
||||
{
|
||||
if (ENEMY._address != -(Vector2I)Vector2.Inf)
|
||||
{
|
||||
_addressOccupants[ENEMY._address] = null;
|
||||
}
|
||||
SetCellSolid(ENEMY._address);
|
||||
|
||||
|
||||
ENEMY._address = ADDRESS;
|
||||
}
|
||||
|
||||
_addressOccupants[ADDRESS] = ENEMY;
|
||||
|
||||
SetCellSolid(ADDRESS);
|
||||
|
||||
}
|
||||
|
||||
public void SetCellSolid(Vector2I ADDRESS)
|
||||
{
|
||||
_astar.SetPointSolid(ADDRESS, IsCellSolid(ADDRESS));
|
||||
// _astar.SetPointWeightScale(ADDRESS, IsCellSolid(ADDRESS) ? 1000 : 1);
|
||||
}
|
||||
|
||||
public void SetupAstar()
|
||||
{
|
||||
_astar.Region = new Rect2I(_minX, _minY, _topRow.Count, _leftmostColumn.Count);
|
||||
_astar.CellSize = _cellSize;
|
||||
_astar.DefaultComputeHeuristic = AStarGrid2D.Heuristic.Euclidean;
|
||||
_astar.DefaultComputeHeuristic = AStarGrid2D.Heuristic.Manhattan;
|
||||
_astar.DiagonalMode = AStarGrid2D.DiagonalModeEnum.Never;
|
||||
_astar.Update();
|
||||
EvaluateSolidCells();
|
||||
@@ -125,4 +169,6 @@ public partial class Map : TileMapLayer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
-237
File diff suppressed because one or more lines are too long
+1
-1
@@ -27,7 +27,7 @@ public partial class PlayerController : TurnController
|
||||
Tower tower = GetNode<Tower>("Tower1");
|
||||
Vector2 towerPositionCorrection = tower._offset.Position;
|
||||
float spaceBetweenTowers = _playArea._map._sizeInPixels.X / (TOWER_COUNT - 1);
|
||||
tower.GlobalPosition = _playArea._map.GlobalPosition - _playArea._map._sizeInPixels / 2 + towerPositionCorrection;
|
||||
tower.GlobalPosition = _playArea._map.GlobalPosition + towerPositionCorrection;
|
||||
|
||||
_towers.Add(tower);
|
||||
for (int i = 1; i < TOWER_COUNT; i++)
|
||||
|
||||
@@ -29,8 +29,7 @@ public partial class Tower : Sprite2D
|
||||
{
|
||||
if (_arcEnd != GetGlobalMousePosition())
|
||||
{
|
||||
float arcAngle = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
|
||||
DrawArc(arcAngle, _attackSpawn.GlobalPosition, GetGlobalMousePosition());
|
||||
DrawArc(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
|
||||
}
|
||||
if (Input.IsActionJustPressed("rightClick"))
|
||||
{
|
||||
@@ -96,8 +95,17 @@ public partial class Tower : Sprite2D
|
||||
|
||||
bestAngle = testAngle;
|
||||
}
|
||||
bestAngle = (float)Mathf.Clamp(bestAngle, Math.PI, Math.PI * 2);
|
||||
|
||||
if (ToLaunchVector(bestAngle).Y < 0)
|
||||
{
|
||||
if (END.X < START.X)
|
||||
{
|
||||
bestAngle = (float)Math.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
bestAngle = 0;
|
||||
}
|
||||
}
|
||||
return bestAngle;
|
||||
}
|
||||
|
||||
@@ -107,12 +115,12 @@ public partial class Tower : Sprite2D
|
||||
path.Curve.ClearPoints();
|
||||
}
|
||||
|
||||
public void DrawArc(float ANGLE, Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
|
||||
public void DrawArc(Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
|
||||
{
|
||||
CalculateLaunchAngle(START, END);
|
||||
float arcAngle = CalculateLaunchAngle(START, END);
|
||||
|
||||
float speed = _launchSpeed, gravity = Globals._gravity, drag = Globals._drag, delta = 1.0f / 60.0f;
|
||||
Vector2 velocity = new Vector2(Mathf.Cos(ANGLE), Mathf.Sin(ANGLE)) * speed;
|
||||
Vector2 velocity = new Vector2(Mathf.Cos(arcAngle), Mathf.Sin(arcAngle)) * speed;
|
||||
Vector2 position = START;
|
||||
|
||||
float directionSign = Mathf.Sign(END.X - START.X);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[gd_scene format=3 uid="uid://dcp7p6al4i0b7"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cg1m762ed04kv" path="res://Main.cs" id="1_ig7tw"]
|
||||
[ext_resource type="PackedScene" uid="uid://dumcridek4xy3" path="res://PlayArea.tscn" id="2_1bvp3"]
|
||||
[ext_resource type="PackedScene" uid="uid://dumcridek4xy3" path="res://play_area.tscn" id="2_1bvp3"]
|
||||
[ext_resource type="PackedScene" uid="uid://c6b188d2a20eq" path="res://enemy_controller.tscn" id="4_1bvp3"]
|
||||
[ext_resource type="PackedScene" uid="uid://b7kvx7p0b2086" path="res://player_controller.tscn" id="4_lquwl"]
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
script = ExtResource("1_ig7tw")
|
||||
|
||||
[node name="PlayArea" parent="." unique_id=1123610167 instance=ExtResource("2_1bvp3")]
|
||||
position = Vector2(931, 542)
|
||||
position = Vector2(360, 180)
|
||||
|
||||
[node name="EnemyController" parent="." unique_id=1894449838 instance=ExtResource("4_1bvp3")]
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user