Skip to content

Commit

Permalink
Add energy weapon spin & quit dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
knightofiam committed Mar 3, 2025
1 parent 8b2ac35 commit 12f97a6
Show file tree
Hide file tree
Showing 22 changed files with 403 additions and 62 deletions.
40 changes: 40 additions & 0 deletions ConfirmationDialog2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Godot;

public partial class ConfirmationDialog2 : MarginContainer
{
// @formatter:off
[Signal] public delegate void ConfirmedEventHandler();
[Signal] public delegate void CanceledEventHandler();
[Signal] public delegate void ClosedEventHandler();
private Button _okButton = null!;
private Button _cancelButton = null!;
private Button _closeButton = null!;
// @formatter:on

public override void _Ready()
{
_okButton = GetNode <Button> ("VBoxContainer/MarginContainer/HBoxContainer/OkButton");
_cancelButton = GetNode <Button> ("VBoxContainer/MarginContainer/HBoxContainer/CancelButton");
_closeButton = GetNode <Button> ("VBoxContainer/Title/HBoxContainer/VBoxContainer/CloseButton");

_okButton.Pressed += () =>
{
Hide();
EmitSignal (SignalName.Confirmed);
};

_cancelButton.Pressed += () =>
{
Hide();
EmitSignal (SignalName.Canceled);
};

_closeButton.Pressed += () =>
{
Hide();
EmitSignal (SignalName.Closed);
};

Hide();
}
}
69 changes: 69 additions & 0 deletions ConfirmationDialog2.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[gd_scene load_steps=2 format=3 uid="uid://bmpjrf8pqokyu"]

[ext_resource type="Script" path="res://ConfirmationDialog2.cs" id="1_yhsco"]

[node name="ConfirmationDialog2" type="MarginContainer"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -600.0
offset_top = -300.0
offset_right = 600.0
offset_bottom = 300.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_yhsco")

[node name="Panel" type="Panel" parent="."]
layout_mode = 2

[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 2

[node name="Title" type="MarginContainer" parent="VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="VBoxContainer/Title"]
layout_mode = 2
theme_override_font_sizes/font_size = 100
text = "Quit Game?"
horizontal_alignment = 1

[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/Title"]
layout_mode = 2
alignment = 2

[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/Title/HBoxContainer"]
layout_mode = 2

[node name="CloseButton" type="Button" parent="VBoxContainer/Title/HBoxContainer/VBoxContainer"]
layout_mode = 2
theme_override_colors/font_color = Color(1, 1, 1, 1)
theme_override_font_sizes/font_size = 50
text = " X "

[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
layout_mode = 2

[node name="MarginContainer" type="MarginContainer" parent="VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
theme_override_constants/margin_top = 300
theme_override_constants/margin_bottom = 50

[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/MarginContainer"]
layout_mode = 2
theme_override_constants/separation = 500
alignment = 1

[node name="CancelButton" type="Button" parent="VBoxContainer/MarginContainer/HBoxContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 50
text = " CANCEL "

[node name="OkButton" type="Button" parent="VBoxContainer/MarginContainer/HBoxContainer"]
layout_mode = 2
theme_override_font_sizes/font_size = 50
text = " OK "
48 changes: 48 additions & 0 deletions EnergyWeapon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Godot;

public partial class EnergyWeapon : Node3D
{
[Export] public float MinRotationSpeed = 1.0f;
[Export] public float MaxRotationSpeed = 15.0f;
[Signal] public delegate void ShotEventHandler();
public float CurrentRotationSpeed { get; private set; }
public bool IsShooting { get; private set; }
private AudioStreamPlayer3D _shootingSound = null!;
private Node3D _muzzle = null!;
private Node3D _pivot = null!;
private Tween? _tween;
public override void _PhysicsProcess (double delta) => _pivot.Rotate (Vector3.Right, CurrentRotationSpeed * (float)delta);
public void Shoot() => SpinUp();

public override void _Ready()
{
_muzzle = GetNode <Node3D> ("Pivot/Muzzle");
_pivot = GetNode <Node3D> ("Pivot");
_shootingSound = GetNode <AudioStreamPlayer3D> ("ShootingSound");
CurrentRotationSpeed = MinRotationSpeed;
}

private void SpinUp()
{
IsShooting = true;
_tween?.Kill();
_tween = CreateTween();
_tween.TweenProperty (this, "CurrentRotationSpeed", MaxRotationSpeed, 2.0f).SetTrans (Tween.TransitionType.Quad).SetEase (Tween.EaseType.Out);
_tween.Finished += FireShot;
}

private void FireShot()
{
_shootingSound.Play();
EmitSignal (SignalName.Shot);
SpinDown();
}

private void SpinDown()
{
_tween?.Kill();
_tween = CreateTween();
_tween.TweenProperty (this, "CurrentRotationSpeed", MinRotationSpeed, 2.0f).SetTrans (Tween.TransitionType.Quad).SetEase (Tween.EaseType.Out);
_tween.Finished += () => IsShooting = false;
}
}
3 changes: 2 additions & 1 deletion HostGameDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public async void Show (ENetMultiplayerPeer peer, int serverPort)
_serverAddress.Editable = !success;
}

private void OnHostGameButtonPressed()
// TODO Testing
public void OnHostGameButtonPressed()
{
_peer?.Close();
_hostGameButton.Disabled = false;
Expand Down
39 changes: 28 additions & 11 deletions Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,49 @@
public partial class Player : CharacterBody3D
{
[Signal] public delegate void HealthChangedEventHandler (int value);
[Signal] public delegate void ScoredEventHandler();
public const float Speed = 7.0f;
public const float JumpVelocity = 20.0f;
public readonly Vector3 Gravity = new(0.0f, -50.0f, 0.0f);
public int NetworkId => Name.ToString().ToInt();
public float WeaponRotationSpeed => _energyWeapon.CurrentRotationSpeed;
public Timer HitRedTimer { get; private set; } = null!;
private static readonly Color NormalColor = new("0027ff");
private static readonly Color HitColor = Colors.DarkRed;
private Camera3D _camera = null!;
private RayCast3D _aim = null!;
private AudioStreamPlayer3D _shootingSound = null!;
private MeshInstance3D _mesh = null!;
private Sprite3D _crosshairs = null!;
private Timer _shotTimer = null!;
private Sprite3D _crossHairs = null!;
private Timer _jumpTimer = null!;
private EnergyWeapon _energyWeapon = null!;
private int _health = 3;
private bool _isInputEnabled;
public override void _EnterTree() => SetMultiplayerAuthority (NetworkId);
private bool IsJumping() => _jumpTimer.IsStopped() && Input.IsActionJustPressed ("jump") && IsOnFloor();
private bool IsShooting() => _shotTimer.IsStopped() && Input.IsActionJustPressed ("shoot");
public void SetInputEnabled (bool isEnabled) => _isInputEnabled = isEnabled;
private bool IsJumping() => _isInputEnabled && _jumpTimer.IsStopped() && Input.IsActionJustPressed ("jump") && IsOnFloor();
private bool IsShooting() => _isInputEnabled && !_energyWeapon.IsShooting && Input.IsActionPressed ("shoot");
private void SetColor (Color color) => (_mesh.GetSurfaceOverrideMaterial (0) as StandardMaterial3D)!.AlbedoColor = color;

public override void _Ready()
{
_mesh = GetNode <MeshInstance3D> ("MeshInstance3D");
_aim = GetNode <RayCast3D> ("Camera3D/Aim");
_shootingSound = GetNode <AudioStreamPlayer3D> ("ShootingSound");
_crosshairs = GetNode <Sprite3D> ("Camera3D/Crosshairs");
_shotTimer = GetNode <Timer> ("ShotTimer");
_energyWeapon = GetNode <EnergyWeapon> ("Camera3D/EnergyWeapon");
_crossHairs = GetNode <Sprite3D> ("Camera3D/Crosshairs");
_jumpTimer = GetNode <Timer> ("JumpTimer");
HitRedTimer = GetNode <Timer> ("HitRedTimer");

if (!IsMultiplayerAuthority())
{
HitRedTimer.Timeout += () => SetColor (NormalColor);
_crosshairs.Hide();
_crossHairs.Hide();
return;
}

_energyWeapon.Shot += OnWeaponShot;
_camera = GetNode <Camera3D> ("Camera3D");
_camera.Current = true;
_isInputEnabled = true;
Input.MouseMode = Input.MouseModeEnum.Captured;
}

Expand Down Expand Up @@ -78,6 +82,7 @@ public override void _PhysicsProcess (double delta)

public override void _UnhandledInput (InputEvent @event)
{
if (!_isInputEnabled) return;
if (!IsMultiplayerAuthority()) return;
if (IsShooting()) Shoot();
if (@event is not InputEventMouseMotion motionEvent) return;
Expand All @@ -88,19 +93,31 @@ public override void _UnhandledInput (InputEvent @event)

private void Shoot()
{
_shotTimer.Start();
_energyWeapon.Shoot();
Rpc (MethodName.PlayShootEffects);
}

private void OnWeaponShot()
{
if (!_aim.IsColliding() || _aim.GetCollider() is not Player hitPlayer || hitPlayer.NetworkId == NetworkId) return;
GD.Print ($"{Name}: I am shooting: {hitPlayer.GetMultiplayerAuthority()}");
hitPlayer.SetColor (HitColor); // This is only for the puppet.
hitPlayer.HitRedTimer.Start(); // This is only for the puppet.
--hitPlayer._health;

if (hitPlayer._health == 0)
{
GD.Print ("Emitting scored signal");
EmitSignal (SignalName.Scored);
}

GD.Print ($"Puppet health: {hitPlayer._health}");
hitPlayer.RpcId (hitPlayer.NetworkId, MethodName.Shot);
}

[Rpc (CallLocal = true)]
private void PlayShootEffects()
{
_shootingSound.Play();
GD.Print ("PlayShootEffects(): ", _aim.IsColliding(), ", collider: ", _aim.GetCollider()?.GetType());
}

Expand Down
25 changes: 5 additions & 20 deletions Player.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[gd_scene load_steps=9 format=3 uid="uid://b1w1b7u4gtpbh"]
[gd_scene load_steps=8 format=3 uid="uid://b1w1b7u4gtpbh"]

[ext_resource type="Script" path="res://Player.cs" id="1_nipvj"]
[ext_resource type="AudioStream" uid="uid://g8e2wwr3buse" path="res://assets/shoot.wav" id="2_5m3nw"]
[ext_resource type="Texture2D" uid="uid://ddlk1r4wak13u" path="res://assets/crosshairs.png" id="2_37lxu"]
[ext_resource type="ArrayMesh" uid="uid://t8g4gpr6gkrg" path="res://assets/rocket.obj" id="3_u77m3"]
[ext_resource type="PackedScene" uid="uid://bnnjmr0ke3v6d" path="res://assets/weapons/EnergyWeapon.tscn" id="4_pk5hr"]

[sub_resource type="CapsuleMesh" id="CapsuleMesh_kwhax"]

Expand All @@ -20,23 +19,13 @@ properties/0/replication_mode = 1
properties/1/path = NodePath(".:rotation")
properties/1/spawn = true
properties/1/replication_mode = 1
properties/2/path = NodePath("Camera3D:rotation")
properties/2/path = NodePath(".:scale")
properties/2/spawn = true
properties/2/replication_mode = 1
properties/3/path = NodePath("Camera3D/Weapon:rotation")
properties/3/spawn = true
properties/3/replication_mode = 1
properties/4/path = NodePath("Camera3D/Weapon:scale")
properties/4/spawn = true
properties/4/replication_mode = 1

[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_nipvj")

[node name="ShotTimer" type="Timer" parent="."]
wait_time = 0.25
one_shot = true

[node name="JumpTimer" type="Timer" parent="."]
wait_time = 3.0
one_shot = true
Expand Down Expand Up @@ -66,12 +55,8 @@ texture = ExtResource("2_37lxu")
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0)
target_position = Vector3(0, -50, 0)

[node name="Weapon" type="CSGMesh3D" parent="Camera3D"]
transform = Transform3D(0.0987688, 0.0155578, 0.00163518, 0, 0.0104528, -0.0994522, -0.0156434, 0.0982278, 0.0103242, 0.5, 0, -1)
mesh = ExtResource("3_u77m3")

[node name="ShootingSound" type="AudioStreamPlayer3D" parent="."]
stream = ExtResource("2_5m3nw")
[node name="EnergyWeapon" parent="Camera3D" instance=ExtResource("4_pk5hr")]
transform = Transform3D(-4.37114e-09, 0, 0.1, 0, 0.1, 0, -0.1, 0, -4.37114e-09, 0.5, 0.3, -1.15)

[node name="HitRedTimer" type="Timer" parent="."]
process_callback = 0
Expand Down
1 change: 1 addition & 0 deletions Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static partial class Tools

public static (bool success, string address, string error) FindServerAddress (int port)
{
return (true, "127.0.0.1", string.Empty); // TODO Remove
var uPnp = new Upnp();
var discoverResult = (Upnp.UpnpResult)uPnp.Discover();

Expand Down
20 changes: 18 additions & 2 deletions UI.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=5 format=3 uid="uid://y2rvvh2dq5oc"]
[gd_scene load_steps=6 format=3 uid="uid://y2rvvh2dq5oc"]

[ext_resource type="PackedScene" uid="uid://b0dcb5suks4p0" path="res://HostGameDialog.tscn" id="1_vtns1"]
[ext_resource type="PackedScene" uid="uid://cx5lbfyyal0ax" path="res://JoinGameDialog.tscn" id="2_msxij"]
[ext_resource type="PackedScene" uid="uid://bmpjrf8pqokyu" path="res://ConfirmationDialog2.tscn" id="3_pp62o"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_wockn"]
bg_color = Color(1, 0, 0, 0.321569)
Expand Down Expand Up @@ -85,7 +86,6 @@ visible = false
visible = false

[node name="HUD" type="Control" parent="."]
visible = false
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
Expand All @@ -107,3 +107,19 @@ max_value = 3.0
step = 1.0
value = 3.0
show_percentage = false

[node name="Score" type="Label" parent="HUD"]
layout_mode = 0
offset_left = 10.0
offset_top = 96.0
offset_right = 223.0
offset_bottom = 206.0
theme_override_font_sizes/font_size = 40
text = "Score: "
vertical_alignment = 1

[node name="QuitDialog" parent="." instance=ExtResource("3_pp62o")]
offset_left = -608.0
offset_top = -632.0
offset_right = 592.0
offset_bottom = -32.0
Loading

0 comments on commit 12f97a6

Please sign in to comment.