Refactor Player and Character

This commit is contained in:
2023-09-04 06:42:02 -07:00
parent a8ea40dee8
commit c32bbfe45a
16 changed files with 132 additions and 206 deletions

View File

@@ -9,15 +9,16 @@ public partial class AreaExit : Node3D
public override void _Ready()
{
_gameManager = GetTree().Root.GetNode<GameManager>("Main/GameManager");
_gameManager = GetTree().Root.GetNode<GameManager>("Main/GameManager");
}
private void OnExitEntered(Node3D node)
{
GD.Print("Exit reached");
var main = GetTree().Root.GetNode<Main>("/root/Main");
main.LoadNextLevel(_levelIndex);
_gameManager.ResetPlayerPosition();
GD.Print("Exit reached");
var main = GetTree().Root.GetNode<Main>("/root/Main");
main.LoadNextLevel(_levelIndex);
if (node is Player player)
player.SpawnPoint.SetPlayerPosition(player);
}
}

View File

@@ -1,28 +1,28 @@
using Godot;
using Godot;
public partial class Player1 : Character
public partial class Character : CharacterBody3D
{
[Export]
private float _speed = 5.0f;
[Export]
private PackedScene _fireProjectile;
[Export]
private PackedScene _altFireProjectile;
private GameManager _gameManager;
[Export]
private float _speed = 3.0f;
public bool CanShoot { get; private set; }
private GameManager _gameManager;
public override void _Ready()
{
CanShoot = true;
_gameManager = GetTree().Root.GetNode<GameManager>("Main/GameManager");
_gameManager.SetP1GameOver(false);
}
public override void _PhysicsProcess(double delta)
{
if (!_gameManager.IsP1SelectingCharacter)
if (!_gameManager.IsSelectingCharacter)
{
Velocity = CalculateCharacterMovement(delta);
MoveAndSlide();
@@ -34,11 +34,11 @@ public partial class Player1 : Character
if (Input.IsActionJustPressed("exit"))
GetTree().Quit();
if (!_gameManager.IsP1SelectingCharacter)
if (!_gameManager.IsSelectingCharacter)
{
if (Input.IsActionJustPressed("p1_fire") && CanShoot)
if (Input.IsActionJustPressed($"p1_fire") && CanShoot)
Fire();
if (Input.IsActionJustPressed("p1_altfire") && CanShoot)
if (Input.IsActionJustPressed($"p1_altfire") && CanShoot)
AltFire();
}
}
@@ -47,7 +47,7 @@ public partial class Player1 : Character
{
var velocity = Velocity;
var inputDir = Input.GetVector("p1_left", "p1_right", "p1_up", "p1_down");
var inputDir = Input.GetVector($"p1_left", $"p1_right", $"p1_up", $"p1_down");
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if (direction != Vector3.Zero)
{
@@ -82,20 +82,4 @@ public partial class Player1 : Character
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
CanShoot = true;
}
private void HitDebug()
{
var node = new Node3D();
OnHit(node);
}
private void OnHit(Node3D node)
{
_gameManager.RemoveP1Character();
if (_gameManager._p1Characters.Count == 0)
{
GD.Print("Game over for P1");
_gameManager.SetP1GameOver(true);
}
}
}
}

View File

@@ -1,5 +1,6 @@
using Godot;
using Godot.Collections;
using System.Collections.Generic;
using System.Linq;
public partial class GameManager : Node
@@ -7,88 +8,60 @@ public partial class GameManager : Node
[Signal]
public delegate void OnGameOverEventHandler();
[Signal]
public delegate void OnP1GameOverEventHandler();
[Signal]
public delegate void OnP2GameOverEventHandler();
public bool IsSelectingCharacter = true;
public int NumberOfPlayers;
public bool IsP1SelectingCharacter = true;
public bool IsP2SelectingCharacter = true;
private bool _gameOver = false;
private int _characterIndex = 0;
[Export]
public Array<PackedScene> _p1Characters;
[Export]
public Array<PackedScene> _p2Characters;
private Array<PackedScene> PlayerScenes;
[Export]
public Character _p1SelectedCharacter;
public IEnumerable<Player> Players = new List<Player>();
[Export]
public Character _p2SelectedCharacter;
private bool _p1GameOver = false;
private bool _p2GameOver = false;
private int _p1CharacterIndex = 0;
private int _p2CharacterIndex = 0;
public void SetP1ToNextCharacter() => _p1CharacterIndex = (++_p1CharacterIndex) % _p1Characters.Count();
public void SetP1ToPreviousCharacter() => _p1CharacterIndex = _p1CharacterIndex == 0 ? _p1Characters.Count() - 1 : --_p1CharacterIndex;
public void ResetPlayerPosition()
public override void _Ready()
{
var playerSpawnPoint = GetNode<SpawnPoint>("P1SpawnPoint");
playerSpawnPoint.SetPlayerPosition(_p1SelectedCharacter);
foreach (var playerScene in PlayerScenes)
{
var player = playerScene.Instantiate();
Players = Players.Append((Player)player);
GD.Print("Players added");
}
}
public void OnP1CharacterSelected()
public void SetToNextCharacter(Player player) => _characterIndex = (++_characterIndex) % player.PlayableCharacters.Count();
public void SetToPreviousCharacter(Player player) => _characterIndex = _characterIndex == 0 ? player.PlayableCharacters.Count() - 1 : --_characterIndex;
public void ResetPlayerPosition(Player player)
{
player.SpawnPoint.SetPlayerPosition(player.SelectedCharacter);
}
public void OnCharacterSelected(Player player)
{
GD.Print("Instancing...");
var selectedPlayer = _p1Characters[_p1CharacterIndex].Instantiate();
_p1SelectedCharacter = selectedPlayer as Character;
GetTree().Root.AddChild(_p1SelectedCharacter);
ResetPlayerPosition();
GD.Print(player.PlayableCharacters.Count());
var selectedCharacter = player.PlayableCharacters[_characterIndex].Instantiate();
player.SelectedCharacter = selectedCharacter as Character;
GetTree().Root.AddChild(player.SelectedCharacter);
player.SpawnPoint.SetPlayerPosition(player.SelectedCharacter);
}
public void RemoveP1Character()
public void RemoveCharacter(Player player)
{
_p1Characters.Remove(_p1Characters.ElementAt(_p1CharacterIndex));
_p1SelectedCharacter?.QueueFree();
_p1CharacterIndex = 0;
player.PlayableCharacters.Remove(player.PlayableCharacters.ElementAt(_characterIndex));
player.SelectedCharacter?.QueueFree();
_characterIndex = 0;
}
public void SetP1GameOver(bool isGameOver)
public void SetGameOver(bool isGameOver)
{
_p1GameOver = isGameOver;
_gameOver = isGameOver;
if (isGameOver)
{
EmitSignal(SignalName.OnP1GameOver);
CheckForOverallGameOver();
}
}
public bool IsP1GameOver => _p1GameOver;
public void SetP2GameOver(bool isGameOver)
{
_p2GameOver = isGameOver;
if (isGameOver)
{
EmitSignal(SignalName.OnP2GameOver);
CheckForOverallGameOver();
}
}
private void CheckForOverallGameOver()
{
if (_p1GameOver && _p2GameOver)
EmitSignal(SignalName.OnGameOver);
}
}
}
public partial class Character : CharacterBody3D
{
public bool IsGameOver => _gameOver;
}

13
Scripts/Player.cs Normal file
View File

@@ -0,0 +1,13 @@
using Godot;
using Godot.Collections;
public partial class Player : Node3D
{
[Export]
public SpawnPoint SpawnPoint;
[Export]
public Array<PackedScene> PlayableCharacters;
public Character SelectedCharacter;
}

View File

@@ -1,81 +0,0 @@
using Godot;
using System;
public partial class Player2 : CharacterBody3D
{
[Export]
private float _speed = 5.0f;
[Export]
private PackedScene _fireProjectile;
[Export]
private PackedScene _altFireProjectile;
public bool CanShoot { get; private set; }
public override void _Ready()
{
CanShoot = true;
}
public override void _PhysicsProcess(double delta)
{
Velocity = CalculateCharacterMovement(delta);
MoveAndSlide();
}
public override void _UnhandledInput(InputEvent @event)
{
if (Input.IsActionJustPressed("exit"))
GetTree().Quit();
if (Input.IsActionJustPressed("p2_fire") && CanShoot)
Fire();
if (Input.IsActionJustPressed("p2_altfire") && CanShoot)
AltFire();
}
private Vector3 CalculateCharacterMovement(double delta)
{
var velocity = Velocity;
var inputDir = Input.GetVector("p2_left", "p2_right", "p2_up", "p2_down");
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if (direction != Vector3.Zero)
{
velocity.X = direction.X * _speed;
velocity.Z = direction.Z * _speed;
GetNode<Node3D>("Pivot").LookAt(Position + direction, Vector3.Up);
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
}
return velocity;
}
private async void Fire()
{
var projectile = _fireProjectile.Instantiate<Projectile>();
projectile.Position = Position + new Vector3(0f, 1f, -1f);
GetParent().AddChild(projectile);
CanShoot = false;
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
CanShoot = true;
}
private async void AltFire()
{
var projectile = _altFireProjectile.Instantiate<Projectile>();
projectile.Position = Position + new Vector3(0f, 1f, -1f);
GetParent().AddChild(projectile);
CanShoot = false;
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
CanShoot = true;
}
private void OnHit(Node3D node)
{
QueueFree();
}
}

View File

@@ -2,7 +2,7 @@ using Godot;
public partial class SpawnPoint : Marker3D
{
public void SetPlayerPosition(Character character)
public void SetPlayerPosition(Node3D character)
{
GD.Print("Moving character to spawn point");
character.Position = Position;

View File

@@ -1,9 +1,10 @@
using Godot;
using System.Linq;
public partial class StageGUI : Control
{
[Signal]
public delegate void OnCharacterSelectionMadeEventHandler();
public delegate void OnCharacterSelectionMadeEventHandler(Player player);
private GameManager _gameManager;
@@ -14,7 +15,7 @@ public partial class StageGUI : Control
public void OnCharacterSelect()
{
if (_gameManager.IsP1GameOver)
if (_gameManager.IsGameOver)
return;
var wheel1 = GetNode<TextureRect>("CharacterSelect/Wheel");
@@ -23,27 +24,26 @@ public partial class StageGUI : Control
wheel1.FocusMode = FocusModeEnum.All;
wheel1.GrabFocus();
wheel1.Visible = true;
_gameManager.IsP1SelectingCharacter = true;
_gameManager.IsSelectingCharacter = true;
}
public override void _Process(double delta)
{
var wheel1 = GetNode<TextureRect>("CharacterSelect/Wheel");
if (wheel1.HasFocus())
{
if (Input.IsActionJustPressed("p1_right"))
_gameManager.SetP1ToNextCharacter();
_gameManager.SetToNextCharacter(_gameManager.Players.ElementAt(0));
if (Input.IsActionJustPressed("p1_left"))
_gameManager.SetP1ToPreviousCharacter();
_gameManager.SetToPreviousCharacter(_gameManager.Players.ElementAt(0));
if (Input.IsActionJustPressed("p1_fire"))
{
GD.Print("Selected character");
wheel1.ReleaseFocus();
wheel1.Hide();
_gameManager.IsP1SelectingCharacter = false;
EmitSignal(SignalName.OnCharacterSelectionMade);
_gameManager.IsSelectingCharacter = false;
EmitSignal(SignalName.OnCharacterSelectionMade, _gameManager.Players.ElementAt(0));
}
}
}

View File

@@ -9,7 +9,7 @@ public partial class TestEnemy : RigidBody3D
public override void _Process(double delta)
{
var player = GetTree().GetFirstNodeInGroup("Player") as Player1;
var player = GetTree().GetFirstNodeInGroup("Player") as Character;
if (player != null)
LookAt(player.Position);
}

View File

@@ -8,7 +8,7 @@ public partial class TestLevel : Node3D
public override void _Ready()
{
var players = GetTree().GetNodesInGroup("Player");
foreach (Player1 player in players)
foreach (Character player in players)
player.Transform = _spawnPoint.Transform;
}