diff --git a/MB_FYP/scenes/player.tscn b/MB_FYP/scenes/player.tscn index 772f523..3cb4706 100644 --- a/MB_FYP/scenes/player.tscn +++ b/MB_FYP/scenes/player.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=3 uid="uid://ckh362yqjkpi0"] +[gd_scene load_steps=5 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"] +[ext_resource type="Script" path="res://script/camera.cs" id="3_ui7sm"] [sub_resource type="CircleShape2D" id="CircleShape2D_btpq3"] radius = 41.0488 @@ -18,3 +19,6 @@ texture = ExtResource("2_1ykv0") [node name="LaserSpawn" type="Node2D" parent="."] position = Vector2(0, -58) + +[node name="Camera2D" type="Camera2D" parent="."] +script = ExtResource("3_ui7sm") diff --git a/MB_FYP/script/asteroid.cs b/MB_FYP/script/asteroid.cs index e0b75ca..cd92ba4 100644 --- a/MB_FYP/script/asteroid.cs +++ b/MB_FYP/script/asteroid.cs @@ -81,6 +81,7 @@ public partial class asteroid : Area2D var radius = circleshape.Radius; var ScreenSize = GetViewportRect().Size; + /* if (GlobalPosition.Y + radius < 0) { GlobalPosition = new Vector2(GlobalPosition.X, ScreenSize.Y + radius); @@ -97,5 +98,6 @@ public partial class asteroid : Area2D { GlobalPosition = new Vector2(-radius, GlobalPosition.Y); } + */ } } diff --git a/MB_FYP/script/camera.cs b/MB_FYP/script/camera.cs new file mode 100644 index 0000000..281aa51 --- /dev/null +++ b/MB_FYP/script/camera.cs @@ -0,0 +1,28 @@ +using Godot; +using System; + +public partial class camera : Camera2D +{ + [Export] public Vector2 OffsetAmount = new Vector2(100, 0); + [Export] public float SmoothingSpeed = 0.1f; + // Called when the node enters the scene tree for the first time. + + public CharacterBody2D Player; + + public override void _Ready() + { + + Player = GetParent(); + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + + Vector2 playerPos = Player.GlobalPosition; + Vector2 playerV = Player.Velocity; + Vector2 direction = playerV.Normalized(); + Vector2 cameraTargetPos = playerPos + OffsetAmount * direction; + GlobalPosition = GlobalPosition.Lerp(cameraTargetPos, SmoothingSpeed); + } +} diff --git a/MB_FYP/script/game.cs b/MB_FYP/script/game.cs index 67e7d11..231aa7f 100644 --- a/MB_FYP/script/game.cs +++ b/MB_FYP/script/game.cs @@ -38,15 +38,15 @@ public partial class game : Node2D } - public override void _Process(double delta) - { - if (Input.IsActionJustPressed("reset")) + public override void _Process(double delta) + { + if (Input.IsActionJustPressed("reset")) { GetTree().ReloadCurrentScene(); } - } + } - public void SpawnAsteroid(Vector2 position, int size) + public void SpawnAsteroid(Vector2 position, int size) { var a = new asteroid(); a = AsteroidScene.Instantiate(); @@ -64,7 +64,7 @@ public partial class game : Node2D GD.Print(h.Score.Text);*/ ScoreLabel.Text = "SCORE: " + score.ToString(); - } + } //Signals and Connections public void OnPlayerLaserShot(Area2D Laser) diff --git a/MB_FYP/script/player.cs b/MB_FYP/script/player.cs index 310ac8e..8c9e688 100644 --- a/MB_FYP/script/player.cs +++ b/MB_FYP/script/player.cs @@ -3,83 +3,79 @@ using System; public partial class player : CharacterBody2D { - [Signal] - public delegate void LaserShotEventHandler(Area2D Laser); - [Export] - public Vector2 ScreenSize; - [Export] - public int MaxSpeed { get; set;} = 300; - [Export] - public int MainSpeed { get; set; } = 10; - [Export] - public int StrafeSpeed { get; set; } = 5; - [Export] - public float RotationSpeed { get; set; } = 2f; + [Signal] + public delegate void LaserShotEventHandler(Area2D Laser); + [Export] + public Vector2 ScreenSize; + [Export] + public int MaxSpeed { get; set;} = 300; + [Export] + public int MainSpeed { get; set; } = 10; + [Export] + public int StrafeSpeed { get; set; } = 5; + [Export] + public float RotationSpeed { get; set; } = 2f; - public Node2D LaserSpawn = null; + public Node2D LaserSpawn = null; - private int _rotationDirection; + private int _rotationDirection; - 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.X * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.Y * Input.GetAxis("up", "down") * MainSpeed); - Velocity = Velocity.LimitLength(MaxSpeed); + 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.X * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.Y * Input.GetAxis("up", "down") * MainSpeed); + Velocity = Velocity.LimitLength(MaxSpeed); - //move into selection statement for toggling between fa off and on - if(Input.GetAxis("strafe_left", "strafe_right") == 0){ - Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); - } - else if(Input.GetAxis("down", "up") == 0){ - Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); - } - } + //move into selection statement for toggling between fa off and on + if(Input.GetAxis("strafe_left", "strafe_right") == 0){ + Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); + } + else if(Input.GetAxis("down", "up") == 0){ + Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); + } + } - public void ShootLaser() - { - Node2D Laser = LaserScene.Instantiate(); - Laser.Position = LaserSpawn.GlobalPosition; - Laser.Rotation = Rotation; - GD.Print("shoooot"); - var l = new Laser - { - Position = new Vector2(LaserSpawn.Position.X, LaserSpawn.Position.Y), - Rotation = Rotation - }; - EmitSignal(SignalName.LaserShot, Laser); - } + public void ShootLaser() + { + Node2D Laser = LaserScene.Instantiate(); + Laser.Position = LaserSpawn.GlobalPosition; + Laser.Rotation = Rotation; + EmitSignal(SignalName.LaserShot, Laser); + } - public override void _Ready() - { - GD.Print(GetViewportRect().Size); - ScreenSize = GetViewportRect().Size; - LaserSpawn = GetNode("LaserSpawn"); - } + 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); - - MoveAndSlide(); + 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); + + MoveAndSlide(); - if (Position.Y < 0) {Position = new Vector2(Position.X, ScreenSize.Y);} - if (Position.Y > ScreenSize.Y) {Position = new Vector2(Position.X, 0);} - if (Position.X < 0) { Position = new Vector2(ScreenSize.X, Position.Y);} - if (Position.X > ScreenSize.X) { Position = new Vector2(0, Position.Y);} - } -} \ No newline at end of file + /* + if (Position.Y < 0) {Position = new Vector2(Position.X, ScreenSize.Y);} + if (Position.Y > ScreenSize.Y) {Position = new Vector2(Position.X, 0);} + if (Position.X < 0) { Position = new Vector2(ScreenSize.X, Position.Y);} + if (Position.X > ScreenSize.X) { Position = new Vector2(0, Position.Y);} + */ + } +}