Work on weapons firing and observer pattern
This commit is contained in:
parent
94f51b7c23
commit
eb1ad24f21
|
|
@ -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"]
|
||||
|
|
|
|||
26
MB_FYP/scenes/laser.tscn
Normal file
26
MB_FYP/scenes/laser.tscn
Normal file
|
|
@ -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"]
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
18
MB_FYP/script/Laser.cs
Normal file
18
MB_FYP/script/Laser.cs
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
30
MB_FYP/script/game.cs
Normal file
30
MB_FYP/script/game.cs
Normal file
|
|
@ -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<Node>("Lasers");
|
||||
//Player = GetNode<CharacterBody2D>("Player");
|
||||
//Laser = GetNode<Area2D>("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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -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<PackedScene>("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<Node2D>();
|
||||
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<Node2D>("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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue