Play zones
This commit is contained in:
parent
4826bc4035
commit
7cce797ded
7 changed files with 150 additions and 58 deletions
|
@ -3,36 +3,61 @@ using Godot;
|
|||
|
||||
public partial class GameManager : Node
|
||||
{
|
||||
public struct GameInfo
|
||||
{
|
||||
public ulong GameStart = 0;
|
||||
public ulong GameEnd = 0;
|
||||
public int Attempts = 0;
|
||||
public Node2D Checkpoint;
|
||||
|
||||
public GameInfo(Node2D checkpoint)
|
||||
{
|
||||
Checkpoint = checkpoint;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsPlaying = true;
|
||||
|
||||
public ulong GameStart = 0;
|
||||
public ulong GameEnd = 0;
|
||||
public int Attempts = 0;
|
||||
public Node2D Checkpoint = null;
|
||||
|
||||
[Export] public PlayZone FirstZone;
|
||||
[Export] public Player Player;
|
||||
|
||||
[Signal]
|
||||
public delegate void GameOverEventHandler();
|
||||
|
||||
private static GameInfo _gameInfo;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (!IsPlaying)
|
||||
{
|
||||
IsPlaying = true;
|
||||
_gameInfo = new GameInfo(FirstZone.PlayerSpawnPoint);
|
||||
}
|
||||
|
||||
|
||||
StartGame();
|
||||
}
|
||||
|
||||
public void StartGame() => GameStart = Time.GetTicksMsec();
|
||||
public void StartGame()
|
||||
{
|
||||
_gameInfo.GameStart = Time.GetTicksMsec();
|
||||
Player.Position = _gameInfo.Checkpoint.GlobalPosition;
|
||||
}
|
||||
|
||||
public void EndGame()
|
||||
{
|
||||
GameEnd = Time.GetTicksMsec();
|
||||
_gameInfo.GameEnd = Time.GetTicksMsec();
|
||||
|
||||
EmitSignal(SignalName.GameOver);
|
||||
}
|
||||
|
||||
public string GetFormattedTimeElapsed() => TimeSpan.FromMilliseconds(GameEnd - GameStart).ToString(@"hh\:mm\:ss.fff");
|
||||
public string GetFormattedTimeElapsed() =>
|
||||
TimeSpan.FromMilliseconds(_gameInfo.GameEnd - _gameInfo.GameStart).ToString(@"hh\:mm\:ss.fff");
|
||||
|
||||
public void OnPlayerDied() => Attempts++;
|
||||
public void OnPlayerDied() => _gameInfo.Attempts++;
|
||||
|
||||
public void SetCurrentZone(PlayZone zone)
|
||||
{
|
||||
GD.Print($"New zone {zone}");
|
||||
_gameInfo.Checkpoint = zone.PlayerSpawnPoint;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,48 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class PlayZone : Node2D
|
||||
{
|
||||
[Export] public Node2D TopLeftCorner;
|
||||
[Export] public Node2D BottomRightCorner;
|
||||
[Export] public Node2D PlayerSpawnPoint;
|
||||
|
||||
[Signal]
|
||||
public delegate void ZoneEnteredEventHandler(PlayZone sender);
|
||||
|
||||
[Signal]
|
||||
public delegate void ZoneLeftEventHandler(PlayZone sender);
|
||||
|
||||
public Rect2I Bounds = new Rect2I(0, 0, 0, 0);
|
||||
|
||||
private Area2D _area2D;
|
||||
private CollisionShape2D _collisionShape2D;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_area2D = (Area2D)FindChild("Area2D");
|
||||
_collisionShape2D = (CollisionShape2D)FindChild("CollisionShape2D");
|
||||
|
||||
var size = BottomRightCorner.Position - TopLeftCorner.Position;
|
||||
Bounds = new Rect2I((int)TopLeftCorner.Position.X, (int)TopLeftCorner.Position.Y, (int)size.X, (int)size.Y);
|
||||
|
||||
var middle = (BottomRightCorner.Position + TopLeftCorner.Position) / 2;
|
||||
_area2D.Position = middle;
|
||||
_collisionShape2D.Shape = new RectangleShape2D { Size = size };
|
||||
}
|
||||
|
||||
private void BodyEntered(Node2D body)
|
||||
{
|
||||
if (body is not Player player)
|
||||
return;
|
||||
|
||||
EmitSignal(SignalName.ZoneEntered, this);
|
||||
}
|
||||
|
||||
private void BodyExited(Node2D body)
|
||||
{
|
||||
if (body is not Player player)
|
||||
return;
|
||||
|
||||
EmitSignal(SignalName.ZoneLeft, this);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue