Skip to content

Commit

Permalink
Add different players
Browse files Browse the repository at this point in the history
  • Loading branch information
ApmeM committed Aug 15, 2024
1 parent 90eb40f commit 2b133dd
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/Presentation/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public partial class Player {
public static int PlayerNeutral = 0;
}
24 changes: 22 additions & 2 deletions src/Presentation/Drones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
using GodotAnalysers;

[SceneReference("Drones.tscn")]
[Tool]
public partial class Drones
{
private int playerId = 0;
private bool playerIdDirty = true;
[Export]
public int PlayerId
{
get => playerId;
set
{
this.playerId = value;
this.playerIdDirty = true;
}
}

private int dronesCount = 0;
private bool dronesCountDirty = true;

[Export]
public int DronesCount
{
Expand All @@ -34,6 +45,15 @@ public override void _Process(float delta)
if (this.dronesCountDirty)
{
this.dronesCountLabel.Text = this.dronesCount.ToString();
this.dronesCountDirty = false;
}

if (this.playerIdDirty)
{
this.nodeNeutral.Visible = this.playerId == Main.PlayerNeutralId;
this.nodeEnemy.Visible = this.playerId == Main.PlayerEnemyId;
this.nodeAlly.Visible = this.playerId == Main.PlayerAllyId;
this.playerIdDirty = false;
}
}
}
66 changes: 53 additions & 13 deletions src/Presentation/Drones.tscn
Original file line number Diff line number Diff line change
@@ -1,31 +1,71 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://Presentation/assets/Drone_sprite.png" type="Texture" id=1]
[ext_resource path="res://Presentation/assets/Drone_neutral_sprite.png" type="Texture" id=1]
[ext_resource path="res://Presentation/Drones.cs" type="Script" id=2]
[ext_resource path="res://Presentation/assets/Drone_enemy_sprite.png" type="Texture" id=3]
[ext_resource path="res://Presentation/assets/Drone_ally_sprite.png" type="Texture" id=4]

[node name="Planet" type="Node2D"]
script = ExtResource( 2 )

[node name="PlanetSprite" type="Sprite" parent="." groups=["MinimapElement"]]
[node name="DronesCountLabel" type="Label" parent="."]
margin_left = -23.0
margin_top = -23.0
margin_right = 23.0
margin_bottom = 23.0
text = "0"
align = 1
valign = 1

[node name="NodeNeutral" type="Node2D" parent="."]

[node name="DroneNeutral1" type="Sprite" parent="NodeNeutral"]
position = Vector2( -5, -2 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 1 )

[node name="PlanetSprite2" type="Sprite" parent="." groups=["MinimapElement"]]
[node name="DroneNeutral2" type="Sprite" parent="NodeNeutral"]
position = Vector2( 6, 6 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 1 )

[node name="PlanetSprite3" type="Sprite" parent="." groups=["MinimapElement"]]
[node name="DroneNeutral3" type="Sprite" parent="NodeNeutral"]
position = Vector2( 6, -8 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 1 )

[node name="DronesCountLabel" type="Label" parent="."]
margin_left = -23.0
margin_top = -23.0
margin_right = 23.0
margin_bottom = 23.0
text = "0"
align = 1
valign = 1
[node name="NodeEnemy" type="Node2D" parent="."]
visible = false

[node name="DroneEnemy1" type="Sprite" parent="NodeEnemy"]
position = Vector2( -5, -2 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 3 )

[node name="DroneEnemy2" type="Sprite" parent="NodeEnemy"]
position = Vector2( 6, 6 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 3 )

[node name="DroneEnemy3" type="Sprite" parent="NodeEnemy"]
position = Vector2( 6, -8 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 3 )

[node name="NodeAlly" type="Node2D" parent="."]
visible = false

[node name="DroneAlly1" type="Sprite" parent="NodeAlly"]
position = Vector2( -5, -2 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 4 )

[node name="DroneAlly2" type="Sprite" parent="NodeAlly"]
position = Vector2( 6, 6 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 4 )

[node name="DroneAlly3" type="Sprite" parent="NodeAlly"]
position = Vector2( 6, -8 )
scale = Vector2( 0.3, 0.3 )
texture = ExtResource( 4 )
26 changes: 23 additions & 3 deletions src/Presentation/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public partial class Main
[Export]
public PackedScene DronesScene;

public const int PlayerNeutralId = 0;
public const int PlayerAllyId = 1;
public const int PlayerEnemyId = 2;

private Random r = new Random();

private const int PlanetSize = 50;
Expand All @@ -30,17 +34,20 @@ public override void _Ready()

for (var i = 0; i < 10; i++)
{
int dronesCount = r.Next(100);
int dronesCount = r.Next(50);
Vector2 position = new Vector2(r.Next(480 - 2 * PlanetSize) + PlanetSize, r.Next(800 - 2 * PlanetSize) + PlanetSize);
var isNeutral = i > 0 && r.Next(100) > 10;

var planet = PlanetScene.Instance<Planet>();
planet.Position = position;
planet.DronesCount = dronesCount;
planet.PlayerId = isNeutral ? Main.PlayerNeutralId : Main.PlayerAllyId;
this.AddChild(planet);

var planet2 = PlanetScene.Instance<Planet>();
planet2.Position = new Vector2(480, 800) - position;
planet2.DronesCount = dronesCount;
planet2.PlayerId = isNeutral ? Main.PlayerNeutralId : Main.PlayerEnemyId;
this.AddChild(planet2);
}
}
Expand All @@ -61,6 +68,7 @@ public override void _Input(InputEvent @event)
{
draggingFrom = this.GetTree().GetNodesInGroup(Groups.Planet)
.Cast<Planet>()
.Where(a => a.PlayerId == PlayerAllyId)
.Where(a => a.GetRect().HasPoint(a.ToLocal(mouse.Position)))
.FirstOrDefault();

Expand All @@ -84,6 +92,7 @@ public override void _Input(InputEvent @event)
drones.DronesCount = draggingFrom.DronesCount;
drones.To = draggingTo;
drones.Position = draggingFrom.Position;
drones.PlayerId = draggingFrom.PlayerId;
this.AddChild(drones);

draggingFrom.DronesCount = 0;
Expand All @@ -92,7 +101,6 @@ public override void _Input(InputEvent @event)
tween.TweenProperty(drones, "position", draggingTo.Position, (draggingFrom.Position - draggingTo.Position).Length() / 50);
tween.TweenCallback(this, nameof(DronesArrived), new Godot.Collections.Array { drones });
}

}

draggingFrom = null;
Expand All @@ -105,7 +113,19 @@ public override void _Input(InputEvent @event)

private void DronesArrived(Drones drones)
{
drones.To.DronesCount += drones.DronesCount;
if (drones.To.PlayerId == drones.PlayerId)
{
drones.To.DronesCount += drones.DronesCount;
}
else
{
if (drones.DronesCount > drones.To.DronesCount)
{
drones.To.PlayerId = drones.PlayerId;
}
drones.To.DronesCount = Math.Abs(drones.DronesCount - drones.To.DronesCount);
}

drones.QueueFree();
}

Expand Down
1 change: 0 additions & 1 deletion src/Presentation/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ margin_bottom = 779.0

[node name="CenterNode" parent="." instance=ExtResource( 5 )]
position = Vector2( 240, 400 )
DronesCount = 10

[node name="MouseDirectionLine" type="Line2D" parent="."]
points = PoolVector2Array( 0, 0, 10, 0 )
Expand Down
43 changes: 39 additions & 4 deletions src/Presentation/Planet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
using GodotAnalysers;

[SceneReference("Planet.tscn")]
[Tool]
public partial class Planet : IMinimapElement
{
public bool VisibleOnBorder => true;

public Sprite Sprite => this.planetSprite;
public Sprite Sprite => this.planetNeutralSprite;

private int playerId = 0;
private bool playerIdDirty = true;
[Export]
public int PlayerId
{
get => playerId;
set
{
this.playerId = value;
this.playerIdDirty = true;
}
}

private int dronesCount = 0;
private bool dronesCountDirty = true;

[Export]
public int DronesCount
{
Expand All @@ -24,6 +35,11 @@ public int DronesCount
}
}

[Export]
public float GrowSpeed = 1;
public float GrowTimeout;
public int DronesMaxCount = 50;

public override void _Ready()
{
base._Ready();
Expand All @@ -40,8 +56,27 @@ public override void _Process(float delta)
if (this.dronesCountDirty)
{
this.dronesCountLabel.Text = this.dronesCount.ToString();
this.dronesCountDirty = false;
}

if (this.playerIdDirty)
{
this.planetNeutralSprite.Visible = this.playerId == Main.PlayerNeutralId;
this.planetEnemySprite.Visible = this.playerId == Main.PlayerEnemyId;
this.planetAllySprite.Visible = this.playerId == Main.PlayerAllyId;
this.playerIdDirty = false;
}

if (dronesCount < DronesMaxCount && this.playerId != Main.PlayerNeutralId)
{
this.GrowTimeout += this.GrowSpeed * delta;
if (this.GrowTimeout > 1)
{
this.DronesCount++;
this.GrowTimeout--;
}
}
}

public Rect2 GetRect() => this.planetSprite.GetRect();
public Rect2 GetRect() => this.planetNeutralSprite.GetRect();
}
18 changes: 14 additions & 4 deletions src/Presentation/Planet.tscn
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://Presentation/assets/Example_sprite.png" type="Texture" id=1]
[ext_resource path="res://Presentation/assets/Planet_neutral_sprite.png" type="Texture" id=1]
[ext_resource path="res://Presentation/Planet.cs" type="Script" id=2]
[ext_resource path="res://Presentation/assets/Planet_ally_sprite.png" type="Texture" id=3]
[ext_resource path="res://Presentation/assets/Planet_enemy_sprite.png" type="Texture" id=4]

[node name="Planet" type="Node2D"]
script = ExtResource( 2 )

[node name="PlanetSprite" type="Sprite" parent="." groups=["MinimapElement"]]
[node name="PlanetNeutralSprite" type="Sprite" parent="."]
texture = ExtResource( 1 )

[node name="PlanetEnemySprite" type="Sprite" parent="."]
visible = false
texture = ExtResource( 4 )

[node name="PlanetAllySprite" type="Sprite" parent="."]
visible = false
texture = ExtResource( 3 )

[node name="DronesCountLabel" type="Label" parent="."]
margin_left = -23.0
margin_top = -23.0
margin_right = 23.0
margin_bottom = 23.0
text = "1"
text = "0"
align = 1
valign = 1
Binary file added src/Presentation/assets/Drone_ally_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Presentation/assets/Drone_enemy_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added src/Presentation/assets/Planet_ally_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Presentation/assets/Planet_enemy_sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes

0 comments on commit 2b133dd

Please sign in to comment.