From eb1ad24f214ebe16f5db1f68002f469633393cff Mon Sep 17 00:00:00 2001 From: maximus Date: Mon, 20 Nov 2023 09:35:59 +0000 Subject: [PATCH] Work on weapons firing and observer pattern --- MB_FYP/scenes/game.tscn | 10 ++++++++-- MB_FYP/scenes/laser.tscn | 26 ++++++++++++++++++++++++++ MB_FYP/scenes/player.tscn | 19 ++++++++++++++++++- MB_FYP/script/Laser.cs | 18 ++++++++++++++++++ MB_FYP/script/game.cs | 30 ++++++++++++++++++++++++++++++ MB_FYP/script/player.cs | 39 +++++++++++++++++++++++++++++++++------ 6 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 MB_FYP/scenes/laser.tscn create mode 100644 MB_FYP/script/Laser.cs create mode 100644 MB_FYP/script/game.cs diff --git a/MB_FYP/scenes/game.tscn b/MB_FYP/scenes/game.tscn index edc682f..5004c23 100644 --- a/MB_FYP/scenes/game.tscn +++ b/MB_FYP/scenes/game.tscn @@ -1,9 +1,15 @@ -[gd_scene load_steps=2 format=3 uid="uid://635xs5haibcn"] +[gd_scene load_steps=3 format=3 uid="uid://635xs5haibcn"] -[ext_resource type="PackedScene" uid="uid://ckh362yqjkpi0" path="res://scenes/Player.tscn" id="1_1w06w"] +[ext_resource type="PackedScene" uid="uid://ckh362yqjkpi0" path="res://scenes/player.tscn" id="1_1w06w"] +[ext_resource type="Script" path="res://script/game.cs" id="1_dukjm"] [node name="Game" type="Node2D"] +script = ExtResource("1_dukjm") [node name="Player" parent="." instance=ExtResource("1_1w06w")] position = Vector2(800, 450) scale = Vector2(0.6, 0.6) + +[node name="Lasers" type="Node" parent="."] + +[connection signal="LaserShot" from="Player" to="." method="_on_player_laser_shot"] diff --git a/MB_FYP/scenes/laser.tscn b/MB_FYP/scenes/laser.tscn new file mode 100644 index 0000000..526c240 --- /dev/null +++ b/MB_FYP/scenes/laser.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=4 format=3 uid="uid://b77r6f5awabe0"] + +[ext_resource type="Script" path="res://script/Laser.cs" id="1_ctmji"] +[ext_resource type="Texture2D" uid="uid://b1tayknrnnrnx" path="res://assets/PNG/Lasers/laserBlue01.png" id="2_nq6d6"] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_odhrn"] +radius = 3.0 +height = 56.0 + +[node name="Laser" type="Area2D"] +scale = Vector2(0.6, 0.6) +script = ExtResource("1_ctmji") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(0, -27) +shape = SubResource("CapsuleShape2D_odhrn") + +[node name="Sprite2D" type="Sprite2D" parent="."] +position = Vector2(0, -28) +texture = ExtResource("2_nq6d6") + +[node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."] +position = Vector2(0, -27.5) +scale = Vector2(0.5, 2.75) + +[connection signal="screen_exited" from="VisibleOnScreenNotifier2D" to="." method="_on_visible_on_screen_notifier_2d_screen_exited"] diff --git a/MB_FYP/scenes/player.tscn b/MB_FYP/scenes/player.tscn index ac2ec56..772f523 100644 --- a/MB_FYP/scenes/player.tscn +++ b/MB_FYP/scenes/player.tscn @@ -1,3 +1,20 @@ -[gd_scene format=3 uid="uid://ckh362yqjkpi0"] +[gd_scene load_steps=4 format=3 uid="uid://ckh362yqjkpi0"] + +[ext_resource type="Script" path="res://script/player.cs" id="1_lhmq0"] +[ext_resource type="Texture2D" uid="uid://lcb2vm87qo30" path="res://assets/PNG/playerShip1_blue.png" id="2_1ykv0"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_btpq3"] +radius = 41.0488 [node name="Player" type="CharacterBody2D"] +motion_mode = 1 +script = ExtResource("1_lhmq0") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_btpq3") + +[node name="Sprite2D" type="Sprite2D" parent="."] +texture = ExtResource("2_1ykv0") + +[node name="LaserSpawn" type="Node2D" parent="."] +position = Vector2(0, -58) diff --git a/MB_FYP/script/Laser.cs b/MB_FYP/script/Laser.cs new file mode 100644 index 0000000..ac89dd0 --- /dev/null +++ b/MB_FYP/script/Laser.cs @@ -0,0 +1,18 @@ +using Godot; +using System; + +public partial class Laser : Area2D +{ + [Export] + public int Speed { get; set;} = 500; + + public Vector2 MovementVector { get; set; } = new Vector2(0, -1); + + + private void _on_visible_on_screen_notifier_2d_screen_exited(){ + QueueFree(); + } + public override void _PhysicsProcess(double delta){ + GlobalPosition += MovementVector.Rotated(Rotation) * Speed * (float)delta; + } +} diff --git a/MB_FYP/script/game.cs b/MB_FYP/script/game.cs new file mode 100644 index 0000000..1313f57 --- /dev/null +++ b/MB_FYP/script/game.cs @@ -0,0 +1,30 @@ +using Godot; +using System; + +public partial class game : Node2D +{ + public Node Lasers = null; + public CharacterBody2D Player = null; + public Area2D Laser = null; + + public override void _Ready() + { + //Lasers = GetNode("Lasers"); + //Player = GetNode("Player"); + //Laser = GetNode("Laser"); + //Callable OnLaserShot = new Callable(this, "_on_player_laser_shot"); + //Player.Connect("LaserShot", OnLaserShot); + + //Player.ShootLaser += () => _on_player_laser_shot(Laser); + + } + + public void _on_player_laser_shot(Area2D Laser) + { + Lasers.AddChild(Laser); + } + + +} + + diff --git a/MB_FYP/script/player.cs b/MB_FYP/script/player.cs index f0fab1d..39b05ec 100644 --- a/MB_FYP/script/player.cs +++ b/MB_FYP/script/player.cs @@ -3,6 +3,8 @@ using System; public partial class player : CharacterBody2D { + [Signal] + public delegate void LaserShotEventHandler(Area2D Laser); [Export] public Vector2 ScreenSize; [Export] @@ -14,16 +16,20 @@ public partial class player : CharacterBody2D [Export] public float RotationSpeed { get; set; } = 2f; + public Node2D LaserSpawn = null; + private int _rotationDirection; - public void GetInput(){ + private readonly PackedScene LaserScene = GD.Load("res://scenes/laser.tscn"); + public void GetInput() + { /*LookAt(GetGlobalMousePosition()); //used for mouse-based rotation and movement Velocity = Transform.X * Input.GetAxis("down", "up") * Speed; */ // Movement, could probably move into its own methods instead of GetInput() _rotationDirection = (int)Input.GetAxis("left", "right"); - Velocity += (Transform.Y * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.X * Input.GetAxis("down", "up") * MainSpeed); + Velocity += (Transform.X * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.Y * Input.GetAxis("up", "down") * MainSpeed); Velocity = Velocity.LimitLength(MaxSpeed); if(Input.GetAxis("strafe_left", "strafe_right") == 0){ @@ -34,12 +40,33 @@ public partial class player : CharacterBody2D } } - public override void _Ready(){ // on init - GD.Print(GetViewportRect().Size); - ScreenSize = GetViewportRect().Size; + public void ShootLaser() + { + Node2D Laser = LaserScene.Instantiate(); + Laser.Position = LaserSpawn.Position; + Laser.Rotation = Rotation; + GD.Print("shoooot"); + EmitSignal(SignalName.LaserShot, Laser); + //GetParent().AddChild(Laser); + AddChild(Laser); } - public override void _PhysicsProcess(double delta){ // every frame + public override void _Ready() + { + GD.Print(GetViewportRect().Size); + ScreenSize = GetViewportRect().Size; + LaserSpawn = GetNode("LaserSpawn"); + } + + public override void _Process(double delta) + { + if(Input.IsActionJustPressed("shoot")) + { + ShootLaser(); + } + } + public override void _PhysicsProcess(double delta) + { // every frame GetInput(); Rotation += _rotationDirection * RotationSpeed * (float)delta; Velocity.LimitLength(MaxSpeed);