Add working camera with an offset effect for persception of motion

This commit is contained in:
rsxri 2024-08-12 19:47:47 +01:00
parent 9d052b0489
commit e90949a8d9
5 changed files with 109 additions and 79 deletions

View file

@ -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="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="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"] [sub_resource type="CircleShape2D" id="CircleShape2D_btpq3"]
radius = 41.0488 radius = 41.0488
@ -18,3 +19,6 @@ texture = ExtResource("2_1ykv0")
[node name="LaserSpawn" type="Node2D" parent="."] [node name="LaserSpawn" type="Node2D" parent="."]
position = Vector2(0, -58) position = Vector2(0, -58)
[node name="Camera2D" type="Camera2D" parent="."]
script = ExtResource("3_ui7sm")

View file

@ -81,6 +81,7 @@ public partial class asteroid : Area2D
var radius = circleshape.Radius; var radius = circleshape.Radius;
var ScreenSize = GetViewportRect().Size; var ScreenSize = GetViewportRect().Size;
/*
if (GlobalPosition.Y + radius < 0) if (GlobalPosition.Y + radius < 0)
{ {
GlobalPosition = new Vector2(GlobalPosition.X, ScreenSize.Y + radius); GlobalPosition = new Vector2(GlobalPosition.X, ScreenSize.Y + radius);
@ -97,5 +98,6 @@ public partial class asteroid : Area2D
{ {
GlobalPosition = new Vector2(-radius, GlobalPosition.Y); GlobalPosition = new Vector2(-radius, GlobalPosition.Y);
} }
*/
} }
} }

28
MB_FYP/script/camera.cs Normal file
View file

@ -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<CharacterBody2D>();
}
// 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);
}
}

View file

@ -38,15 +38,15 @@ public partial class game : Node2D
} }
public override void _Process(double delta) public override void _Process(double delta)
{ {
if (Input.IsActionJustPressed("reset")) if (Input.IsActionJustPressed("reset"))
{ {
GetTree().ReloadCurrentScene(); GetTree().ReloadCurrentScene();
} }
} }
public void SpawnAsteroid(Vector2 position, int size) public void SpawnAsteroid(Vector2 position, int size)
{ {
var a = new asteroid(); var a = new asteroid();
a = AsteroidScene.Instantiate<asteroid>(); a = AsteroidScene.Instantiate<asteroid>();
@ -64,7 +64,7 @@ public partial class game : Node2D
GD.Print(h.Score.Text);*/ GD.Print(h.Score.Text);*/
ScoreLabel.Text = "SCORE: " + score.ToString(); ScoreLabel.Text = "SCORE: " + score.ToString();
} }
//Signals and Connections //Signals and Connections
public void OnPlayerLaserShot(Area2D Laser) public void OnPlayerLaserShot(Area2D Laser)

View file

@ -3,83 +3,79 @@ using System;
public partial class player : CharacterBody2D public partial class player : CharacterBody2D
{ {
[Signal] [Signal]
public delegate void LaserShotEventHandler(Area2D Laser); public delegate void LaserShotEventHandler(Area2D Laser);
[Export] [Export]
public Vector2 ScreenSize; public Vector2 ScreenSize;
[Export] [Export]
public int MaxSpeed { get; set;} = 300; public int MaxSpeed { get; set;} = 300;
[Export] [Export]
public int MainSpeed { get; set; } = 10; public int MainSpeed { get; set; } = 10;
[Export] [Export]
public int StrafeSpeed { get; set; } = 5; public int StrafeSpeed { get; set; } = 5;
[Export] [Export]
public float RotationSpeed { get; set; } = 2f; 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<PackedScene>("res://scenes/laser.tscn"); private readonly PackedScene LaserScene = GD.Load<PackedScene>("res://scenes/laser.tscn");
public void GetInput() public void GetInput()
{ {
/*LookAt(GetGlobalMousePosition()); //used for mouse-based rotation and movement /*LookAt(GetGlobalMousePosition()); //used for mouse-based rotation and movement
Velocity = Transform.X * Input.GetAxis("down", "up") * Speed; Velocity = Transform.X * Input.GetAxis("down", "up") * Speed;
*/ */
// Movement, could probably move into its own methods instead of GetInput() // Movement, could probably move into its own methods instead of GetInput()
_rotationDirection = (int)Input.GetAxis("left", "right"); _rotationDirection = (int)Input.GetAxis("left", "right");
Velocity += (Transform.X * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.Y * Input.GetAxis("up", "down") * MainSpeed); Velocity += (Transform.X * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.Y * Input.GetAxis("up", "down") * MainSpeed);
Velocity = Velocity.LimitLength(MaxSpeed); Velocity = Velocity.LimitLength(MaxSpeed);
//move into selection statement for toggling between fa off and on //move into selection statement for toggling between fa off and on
if(Input.GetAxis("strafe_left", "strafe_right") == 0){ if(Input.GetAxis("strafe_left", "strafe_right") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f);
} }
else if(Input.GetAxis("down", "up") == 0){ else if(Input.GetAxis("down", "up") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f);
} }
} }
public void ShootLaser() public void ShootLaser()
{ {
Node2D Laser = LaserScene.Instantiate<Node2D>(); Node2D Laser = LaserScene.Instantiate<Node2D>();
Laser.Position = LaserSpawn.GlobalPosition; Laser.Position = LaserSpawn.GlobalPosition;
Laser.Rotation = Rotation; Laser.Rotation = Rotation;
GD.Print("shoooot"); EmitSignal(SignalName.LaserShot, Laser);
var l = new Laser }
{
Position = new Vector2(LaserSpawn.Position.X, LaserSpawn.Position.Y),
Rotation = Rotation
};
EmitSignal(SignalName.LaserShot, Laser);
}
public override void _Ready() public override void _Ready()
{ {
GD.Print(GetViewportRect().Size); //GD.Print(GetViewportRect().Size);
ScreenSize = GetViewportRect().Size; ScreenSize = GetViewportRect().Size;
LaserSpawn = GetNode<Node2D>("LaserSpawn"); LaserSpawn = GetNode<Node2D>("LaserSpawn");
} }
public override void _Process(double delta) public override void _Process(double delta)
{ {
if(Input.IsActionJustPressed("shoot")) if(Input.IsActionJustPressed("shoot"))
{ {
ShootLaser(); ShootLaser();
} }
} }
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ // every frame { // every frame
GetInput(); GetInput();
Rotation += _rotationDirection * RotationSpeed * (float)delta; Rotation += _rotationDirection * RotationSpeed * (float)delta;
Velocity.LimitLength(MaxSpeed); Velocity.LimitLength(MaxSpeed);
MoveAndSlide(); 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.Y < 0) {Position = new Vector2(Position.X, ScreenSize.Y);}
if (Position.X < 0) { Position = new Vector2(ScreenSize.X, Position.Y);} if (Position.Y > ScreenSize.Y) {Position = new Vector2(Position.X, 0);}
if (Position.X > ScreenSize.X) { Position = new Vector2(0, Position.Y);} if (Position.X < 0) { Position = new Vector2(ScreenSize.X, Position.Y);}
} if (Position.X > ScreenSize.X) { Position = new Vector2(0, Position.Y);}
*/
}
} }