Fix multiplayer
This commit is contained in:
@@ -1,85 +1,5 @@
|
||||
using Godot;
|
||||
using Godot;
|
||||
|
||||
public partial class Character : CharacterBody3D
|
||||
{
|
||||
[Export]
|
||||
private PackedScene _fireProjectile;
|
||||
[Export]
|
||||
private PackedScene _altFireProjectile;
|
||||
|
||||
[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");
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (!_gameManager.IsSelectingCharacter)
|
||||
{
|
||||
Velocity = CalculateCharacterMovement(delta);
|
||||
MoveAndSlide();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (Input.IsActionJustPressed("exit"))
|
||||
GetTree().Quit();
|
||||
|
||||
if (!_gameManager.IsSelectingCharacter)
|
||||
{
|
||||
if (Input.IsActionJustPressed($"p1_fire") && CanShoot)
|
||||
Fire();
|
||||
if (Input.IsActionJustPressed($"p1_altfire") && CanShoot)
|
||||
AltFire();
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 CalculateCharacterMovement(double delta)
|
||||
{
|
||||
var velocity = Velocity;
|
||||
|
||||
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)
|
||||
{
|
||||
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, 0f);
|
||||
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, 0f);
|
||||
GetParent().AddChild(projectile);
|
||||
CanShoot = false;
|
||||
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
||||
CanShoot = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
79
Scripts/Character1.cs
Normal file
79
Scripts/Character1.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Character1 : Character
|
||||
{
|
||||
[Export]
|
||||
private PackedScene _fireProjectile;
|
||||
[Export]
|
||||
private PackedScene _altFireProjectile;
|
||||
|
||||
[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");
|
||||
}
|
||||
|
||||
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($"p1_fire") && CanShoot)
|
||||
Fire();
|
||||
if (Input.IsActionJustPressed($"p1_altfire") && CanShoot)
|
||||
AltFire();
|
||||
}
|
||||
|
||||
private Vector3 CalculateCharacterMovement(double delta)
|
||||
{
|
||||
var velocity = Velocity;
|
||||
|
||||
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)
|
||||
{
|
||||
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, 0f);
|
||||
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, 0f);
|
||||
GetParent().AddChild(projectile);
|
||||
CanShoot = false;
|
||||
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
||||
CanShoot = true;
|
||||
}
|
||||
}
|
||||
79
Scripts/Character2.cs
Normal file
79
Scripts/Character2.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Character2 : Character
|
||||
{
|
||||
[Export]
|
||||
private PackedScene _fireProjectile;
|
||||
[Export]
|
||||
private PackedScene _altFireProjectile;
|
||||
|
||||
[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");
|
||||
}
|
||||
|
||||
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, 0f);
|
||||
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, 0f);
|
||||
GetParent().AddChild(projectile);
|
||||
CanShoot = false;
|
||||
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
||||
CanShoot = true;
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ public partial class GameManager : Node
|
||||
{
|
||||
[Signal]
|
||||
public delegate void OnGameOverEventHandler();
|
||||
|
||||
public bool IsSelectingCharacter = true;
|
||||
[Signal]
|
||||
public delegate void ReselectCharacterEventHandler(Player player);
|
||||
|
||||
private bool _gameOver = false;
|
||||
private int _characterIndex = 0;
|
||||
@@ -40,7 +40,6 @@ public partial class GameManager : Node
|
||||
public void OnCharacterSelected(Player player)
|
||||
{
|
||||
GD.Print("Instancing...");
|
||||
GD.Print(player.PlayableCharacters.Count());
|
||||
var selectedCharacter = player.PlayableCharacters[_characterIndex].Instantiate();
|
||||
player.SelectedCharacter = selectedCharacter as Character;
|
||||
GetTree().Root.AddChild(player.SelectedCharacter);
|
||||
@@ -52,15 +51,15 @@ public partial class GameManager : Node
|
||||
player.PlayableCharacters.Remove(player.PlayableCharacters.ElementAt(_characterIndex));
|
||||
player.SelectedCharacter?.QueueFree();
|
||||
_characterIndex = 0;
|
||||
if (player.PlayableCharacters.Count() > 0)
|
||||
EmitSignal(SignalName.ReselectCharacter, player);
|
||||
}
|
||||
|
||||
public void SetGameOver(bool isGameOver)
|
||||
{
|
||||
_gameOver = isGameOver;
|
||||
if (isGameOver)
|
||||
{
|
||||
EmitSignal(SignalName.OnGameOver);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsGameOver => _gameOver;
|
||||
|
||||
@@ -10,4 +10,6 @@ public partial class Player : Node3D
|
||||
public Array<PackedScene> PlayableCharacters;
|
||||
|
||||
public Character SelectedCharacter;
|
||||
|
||||
public bool IsSelectingCharacter = true;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public partial class StageGUI : Control
|
||||
_gameManager = GetTree().Root.GetNode<GameManager>("Main/GameManager");
|
||||
}
|
||||
|
||||
public void OnCharacterSelect()
|
||||
public void OnCharacterSelect(Player player)
|
||||
{
|
||||
if (_gameManager.IsGameOver)
|
||||
return;
|
||||
@@ -21,30 +21,71 @@ public partial class StageGUI : Control
|
||||
var wheel1 = GetNode<TextureRect>("CharacterSelect/Wheel");
|
||||
var wheel2 = GetNode<TextureRect>("CharacterSelect/Wheel2");
|
||||
|
||||
wheel1.FocusMode = FocusModeEnum.All;
|
||||
wheel1.GrabFocus();
|
||||
wheel1.Visible = true;
|
||||
_gameManager.IsSelectingCharacter = true;
|
||||
if (player == _gameManager.Players.ElementAt(0))
|
||||
{
|
||||
wheel1.FocusMode = FocusModeEnum.All;
|
||||
wheel1.GrabFocus();
|
||||
wheel1.Visible = true;
|
||||
}
|
||||
else if (player == _gameManager.Players.ElementAt(1))
|
||||
{
|
||||
wheel2.FocusMode = FocusModeEnum.All;
|
||||
wheel2.GrabFocus();
|
||||
wheel2.Visible = true;
|
||||
}
|
||||
player.IsSelectingCharacter = true;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
var wheel1 = GetNode<TextureRect>("CharacterSelect/Wheel");
|
||||
if (wheel1.HasFocus())
|
||||
var playersCurrentlySelecting = _gameManager.Players.Where(x => x.IsSelectingCharacter);
|
||||
foreach (var player in playersCurrentlySelecting)
|
||||
{
|
||||
if (Input.IsActionJustPressed("p1_right"))
|
||||
_gameManager.SetToNextCharacter(_gameManager.Players.ElementAt(0));
|
||||
if (Input.IsActionJustPressed("p1_left"))
|
||||
_gameManager.SetToPreviousCharacter(_gameManager.Players.ElementAt(0));
|
||||
|
||||
if (Input.IsActionJustPressed("p1_fire"))
|
||||
if (player == _gameManager.Players.ElementAt(0))
|
||||
{
|
||||
GD.Print("Selected character");
|
||||
wheel1.ReleaseFocus();
|
||||
wheel1.Hide();
|
||||
_gameManager.IsSelectingCharacter = false;
|
||||
EmitSignal(SignalName.OnCharacterSelectionMade, _gameManager.Players.ElementAt(0));
|
||||
if (Input.IsActionJustPressed("p1_right"))
|
||||
_gameManager.SetToNextCharacter(_gameManager.Players.ElementAt(0));
|
||||
if (Input.IsActionJustPressed("p1_left"))
|
||||
_gameManager.SetToPreviousCharacter(_gameManager.Players.ElementAt(0));
|
||||
|
||||
if (Input.IsActionJustPressed("p1_fire"))
|
||||
{
|
||||
GD.Print("Selected character");
|
||||
_gameManager.Players.ElementAt(0).IsSelectingCharacter = false;
|
||||
var wheel1 = GetNode<TextureRect>("CharacterSelect/Wheel");
|
||||
wheel1.Hide();
|
||||
EmitSignal(SignalName.OnCharacterSelectionMade, _gameManager.Players.ElementAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
if (player == _gameManager.Players.ElementAt(1))
|
||||
{
|
||||
if (Input.IsActionJustPressed("p2_right"))
|
||||
_gameManager.SetToNextCharacter(_gameManager.Players.ElementAt(1));
|
||||
if (Input.IsActionJustPressed("p2_left"))
|
||||
_gameManager.SetToPreviousCharacter(_gameManager.Players.ElementAt(1));
|
||||
|
||||
if (Input.IsActionJustPressed("p2_fire"))
|
||||
{
|
||||
GD.Print("Selected character");
|
||||
_gameManager.Players.ElementAt(1).IsSelectingCharacter = false;
|
||||
var wheel2 = GetNode<TextureRect>("CharacterSelect/Wheel2");
|
||||
wheel2.Hide();
|
||||
EmitSignal(SignalName.OnCharacterSelectionMade, _gameManager.Players.ElementAt(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFirstPlayerCharacterSelect()
|
||||
{
|
||||
var player = _gameManager.Players.ElementAt(0);
|
||||
OnCharacterSelect(player);
|
||||
}
|
||||
|
||||
private void OnSecondPlayerCharacterSelect()
|
||||
{
|
||||
var player = _gameManager.Players.ElementAt(1);
|
||||
OnCharacterSelect(player);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user