diff --git a/Attack.cs b/Attack.cs index 475ad7f..064b542 100644 --- a/Attack.cs +++ b/Attack.cs @@ -3,7 +3,9 @@ using Godot; public partial class Attack : RigidBody2D { public bool _hovered = false; + public int _damage = 1; public Vector2 _speed; + public Player _playerOwner; public override void _PhysicsProcess(double delta) { @@ -18,12 +20,19 @@ public partial class Attack : RigidBody2D _speed = FORCE; GravityScale = 1; } + + public void TakeAction(Node BODY) + { + if (BODY is Enemy enemy) + { + enemy.TakeDamage(_damage, _playerOwner); + } + } + public void OnMouseEntered(){ - GD.Print("Hovered"); _hovered = true; } public void OnMouseExited(){ - GD.Print("Not Hovered"); _hovered = true; } } diff --git a/Enemy.cs b/Enemy.cs new file mode 100644 index 0000000..1ce023d --- /dev/null +++ b/Enemy.cs @@ -0,0 +1,54 @@ +using Godot; +using System; + +public partial class Enemy : StaticBody2D +{ + [Signal] + public delegate void TurnDoneEventHandler(); + public bool _toMove = false; + public int _speed = 100, _reach = 200, _damage = 1, _health = 2; + public float _movement = 0; + public Player _player; + public override void _PhysicsProcess(double delta) + { + base._PhysicsProcess(delta); + if (_toMove) + { + Vector2 moveVector = (_player.Position - Position) * (float)delta; + if ((_movement += moveVector.Length()) <= _speed ) + { + MoveAndCollide(moveVector); + } + else + { + if ((_player.Position - Position).Length() <= _reach) + { + Attack(_player); + } + _toMove = false; + _movement = 0; + } + } + } + + public void Attack(Player PLAYER) + { + PLAYER.TakeDamage(_damage, this); + QueueFree(); + } + + public void TakeDamage(int DAMAGE, Player ATTACKER) + { + _health -= DAMAGE; + GD.Print($"Taking {DAMAGE}, Health is now {_health}"); + if (_health <= 0) + { + QueueFree(); + } + } + + public void TakeTurn() + { + _toMove = true; + } +} diff --git a/Enemy.cs.uid b/Enemy.cs.uid new file mode 100644 index 0000000..c6f0a23 --- /dev/null +++ b/Enemy.cs.uid @@ -0,0 +1 @@ +uid://dfba4vq6jv0a6 diff --git a/Main.cs b/Main.cs index 1ca81cf..7a66061 100644 --- a/Main.cs +++ b/Main.cs @@ -1,6 +1,50 @@ using Godot; using System; +using System.Collections.Generic; public partial class Main : Node { + public bool _isPlayerTurn = true; + public Player _player; + public PackedScene _enemyScene = GD.Load("res://Enemy.tscn"); + public List _enemies = new(); + public Random _rng = new(); + public override void _Ready() + { + base._Ready(); + _player = GetNode("Player"); + _player.TurnDone += ChangeTurn; + AddEnemy(); + _player.StartTurn(); + } + + public void ChangeTurn() + { + _isPlayerTurn = !_isPlayerTurn; + if (_isPlayerTurn) + { + GD.Print("Starting Player turn"); + _player.StartTurn(); + } + else + { + GD.Print("Starting Enemy turn"); + for (int i = 0; i < _enemies.Count; i++) + { + _enemies[i].TakeTurn(); + } + AddEnemy(); + ChangeTurn(); + } + } + + public void AddEnemy() + { + Enemy newEnemy = _enemyScene.Instantiate(); + newEnemy.TurnDone += ChangeTurn; + newEnemy.Position = new Vector2(_rng.Next((int)_player.GetViewportRect().Size.X), _player.GetViewportRect().Size.Y - 100); + newEnemy._player = _player; + _enemies.Add(newEnemy); + AddChild(newEnemy); + } } diff --git a/Player.cs b/Player.cs index 7a86eac..a8e92ed 100644 --- a/Player.cs +++ b/Player.cs @@ -1,35 +1,57 @@ using Godot; using System; -public partial class Player : Node2D +public partial class Player : Sprite2D { + [Signal] + public delegate void TurnDoneEventHandler(); public bool _aiming = false; + public int _health = 10; public PackedScene _attackScene = GD.Load("res://Attack.tscn"); public Attack _currentAttack; public override void _Process(double delta) { base._Process(delta); + if (_currentAttack != null) + { + if (Input.IsActionJustPressed("leftClick")){ + if (_currentAttack._hovered){ + _aiming = true; + } + } + if (_aiming){ + Vector2 offset = (_currentAttack.GlobalPosition - GetGlobalMousePosition()) * 500; + if (Input.IsActionJustReleased("leftClick")){ + ShootCurrentAttack(offset); + } + } + if (_currentAttack.Position.Y > GetViewportRect().Size.Y + 50){ + _currentAttack.QueueFree(); + _currentAttack = null; + EmitSignal(SignalName.TurnDone); + } + } + + } + + public void StartTurn() + { if (_currentAttack == null){ GD.Print("Loading attack"); _currentAttack = _attackScene.Instantiate(); + _currentAttack._playerOwner = this; _currentAttack.GravityScale = 0; - _currentAttack.Position = GetViewportRect().Size / 2; AddChild(_currentAttack); } - if (Input.IsActionJustPressed("leftClick")){ - if (_currentAttack._hovered){ - _aiming = true; - } - } - if (_aiming){ - Vector2 offset = (_currentAttack.GlobalPosition - GetGlobalMousePosition()) * 500; - if (Input.IsActionJustReleased("leftClick")){ - ShootCurrentAttack(offset); - } - } - if (_currentAttack.Position.Y > GetViewportRect().Size.Y + 50){ - _currentAttack.QueueFree(); - _currentAttack = null; + } + + public void TakeDamage(int DAMAGE, Enemy ATTACKER) + { + _health -= DAMAGE; + if (_health <= 0) + { + GD.Print("GAME OVER"); + GetTree().Quit(); } } diff --git a/attack.tscn b/attack.tscn index 0e50fdb..e8d04a8 100644 --- a/attack.tscn +++ b/attack.tscn @@ -16,5 +16,6 @@ shape = SubResource("CircleShape2D_7yfhp") [node name="Sprite2D" type="Sprite2D" parent="." unique_id=1583277900] texture = ExtResource("2_hqc8w") +[connection signal="body_entered" from="." to="." method="TakeAction"] [connection signal="mouse_entered" from="." to="." method="OnMouseEntered"] [connection signal="mouse_exited" from="." to="." method="OnMouseExited"] diff --git a/enemy.tscn b/enemy.tscn new file mode 100644 index 0000000..13f7325 --- /dev/null +++ b/enemy.tscn @@ -0,0 +1,20 @@ +[gd_scene format=3 uid="uid://drt7w0eqp13tu"] + +[ext_resource type="Script" uid="uid://dfba4vq6jv0a6" path="res://Enemy.cs" id="1_4gyqm"] +[ext_resource type="Texture2D" uid="uid://nwj4n7if8kqd" path="res://circle25r.png" id="1_7k104"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7k104"] +bounce = 0.5 + +[sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"] +radius = 25.0 + +[node name="Enemy" type="StaticBody2D" unique_id=1417697759] +physics_material_override = SubResource("PhysicsMaterial_7k104") +script = ExtResource("1_4gyqm") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1762191899] +shape = SubResource("CircleShape2D_4gyqm") + +[node name="Sprite2D" type="Sprite2D" parent="." unique_id=1941012605] +texture = ExtResource("1_7k104") diff --git a/main.tscn b/main.tscn index d051b5b..4db2287 100644 --- a/main.tscn +++ b/main.tscn @@ -7,3 +7,4 @@ script = ExtResource("1_ig7tw") [node name="Player" parent="." unique_id=203629164 instance=ExtResource("2_0xm2m")] +position = Vector2(576, 100) diff --git a/player.tscn b/player.tscn index beba6ee..8eb1457 100644 --- a/player.tscn +++ b/player.tscn @@ -2,5 +2,5 @@ [ext_resource type="Script" uid="uid://b2kj0rc3g1hkm" path="res://Player.cs" id="1_4flbx"] -[node name="Player" type="Node2D" unique_id=203629164] +[node name="Player" type="Sprite2D" unique_id=1378094015] script = ExtResource("1_4flbx")