-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6: Introduce the first version of on screen indicator to notify play…
…er about incoming agents
- Loading branch information
Danil Ko
committed
Feb 8, 2021
1 parent
332468b
commit bb772d3
Showing
10 changed files
with
221 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Godot; | ||
using System; | ||
|
||
public class ScreenIndicator : Node2D | ||
{ | ||
|
||
private float _zoom = 1.5f; | ||
|
||
private Agent _player; | ||
private Godot.Collections.Dictionary<String, Agent> _agents; | ||
|
||
private Godot.Collections.Dictionary<String, Node2D> _agentMarkers; | ||
|
||
private Node2D _agentMarker; | ||
|
||
private Godot.Collections.Dictionary<String, Team.TeamCode> _targetAgents; | ||
private Agent _targetAgent = null; | ||
private Agent _agent = null; | ||
|
||
public override void _Ready() | ||
{ | ||
_agentMarker = (Node2D)GetNode("AgentMarker"); | ||
} | ||
|
||
public void Initialize(Agent agent) | ||
{ | ||
_agent = agent; | ||
_agents = new Godot.Collections.Dictionary<String, Agent>(); | ||
_agentMarkers = new Godot.Collections.Dictionary<String, Node2D>(); | ||
_targetAgents = new Godot.Collections.Dictionary<String, Team.TeamCode>(); | ||
} | ||
|
||
public void SetPlayer(Agent agent) | ||
{ | ||
_player = agent; | ||
} | ||
|
||
public void RemovePlayer() | ||
{ | ||
_player = null; | ||
} | ||
|
||
public void AddAgent(Agent agent) | ||
{ | ||
if (!_agents.ContainsKey(agent.GetUnitName())) | ||
{ | ||
Node2D agentMarker = (Node2D)_agentMarker.Duplicate(); | ||
agentMarker.Name = agent.GetUnitName() + "_marker"; | ||
|
||
agentMarker.Modulate = Team.TeamColor[(int)agent.GetCurrentTeam()]; | ||
|
||
AddChild(agentMarker); | ||
|
||
agentMarker.Show(); | ||
|
||
// Add agent to dictonary | ||
_agents.Add(agent.GetUnitName(), agent); | ||
|
||
// Add marker to dictionary | ||
_agentMarkers.Add(agent.GetUnitName(), agentMarker); | ||
} | ||
} | ||
|
||
public void RemoveAgent(Agent agent) | ||
{ | ||
if (_agents.ContainsKey(agent.GetUnitName())) | ||
{ | ||
// Add agent to dictonary | ||
_agents.Remove(agent.GetUnitName()); | ||
|
||
Node2D agentMarker = _agentMarkers[agent.GetUnitName()]; | ||
|
||
// Add marker to dictionary | ||
_agentMarkers.Remove(agent.GetUnitName()); | ||
|
||
agentMarker.QueueFree(); | ||
} | ||
} | ||
|
||
public override void _Process(float delta) | ||
{ | ||
if(_agent == null || ! IsInstanceValid(_agent)) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (Agent agent in _agents.Values) | ||
{ | ||
if (agent != null && IsInstanceValid(agent)) | ||
{ | ||
Node2D agentMarker = _agentMarkers[agent.GetUnitName()]; | ||
agentMarker.GlobalRotation = agent.GlobalRotation + Mathf.Pi / 2.0f; | ||
|
||
// Update marker | ||
agentMarker.LookAt(agent.GlobalPosition); | ||
float distance = agentMarker.GlobalPosition.DistanceTo(agent.GlobalPosition); | ||
((Label)agentMarker.GetNode("Text")).Text = agent.GetUnitName() + " " + distance; | ||
} | ||
} | ||
} | ||
|
||
|
||
private void _onDetectionZoneBodyEntered(Node body) | ||
{ | ||
|
||
if (body.HasMethod(nameof(Agent.GetCurrentTeam)) && body != _agent) | ||
{ | ||
AddAgent((Agent)body); | ||
} | ||
} | ||
|
||
private void _onDetectionZoneBodyExited(Node body) | ||
{ | ||
if (body.HasMethod(nameof(Agent.GetCurrentTeam)) && body != _agent) | ||
{ | ||
RemoveAgent((Agent)body); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[gd_scene load_steps=4 format=2] | ||
|
||
[ext_resource path="res://assets/ui/grey_sliderUp.png" type="Texture" id=1] | ||
[ext_resource path="res://ui/ScreenIndicator.cs" type="Script" id=2] | ||
|
||
[sub_resource type="CircleShape2D" id=1] | ||
radius = 1500.0 | ||
|
||
[node name="ScreenIndicator" type="Node2D"] | ||
script = ExtResource( 2 ) | ||
|
||
[node name="AgentMarker" type="Node2D" parent="."] | ||
visible = false | ||
|
||
[node name="Indicator" type="TextureRect" parent="AgentMarker"] | ||
margin_left = 300.0 | ||
margin_right = 340.0 | ||
margin_bottom = 42.0 | ||
rect_rotation = 90.0 | ||
rect_pivot_offset = Vector2( 10, 0 ) | ||
texture = ExtResource( 1 ) | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="Text" type="Label" parent="AgentMarker"] | ||
margin_left = 522.0 | ||
margin_top = -4.0 | ||
margin_right = 861.0 | ||
margin_bottom = 10.0 | ||
text = "500" | ||
__meta__ = { | ||
"_edit_use_anchors_": false | ||
} | ||
|
||
[node name="DetectionZone" type="Area2D" parent="."] | ||
|
||
[node name="CollisionShape2D" type="CollisionShape2D" parent="DetectionZone"] | ||
shape = SubResource( 1 ) | ||
[connection signal="body_entered" from="DetectionZone" to="." method="_onDetectionZoneBodyEntered"] | ||
[connection signal="body_exited" from="DetectionZone" to="." method="_onDetectionZoneBodyExited"] |