From 94f51b7c230a638bec4d6edb60f1efbd1b3c502b Mon Sep 17 00:00:00 2001 From: maximus Date: Mon, 20 Nov 2023 03:27:16 +0000 Subject: [PATCH] Fine tune movement, added looping screen --- MB_FYP/scenes/game.tscn | 8 +++++++- MB_FYP/script/player.cs | 28 +++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/MB_FYP/scenes/game.tscn b/MB_FYP/scenes/game.tscn index d08d223..edc682f 100644 --- a/MB_FYP/scenes/game.tscn +++ b/MB_FYP/scenes/game.tscn @@ -1,3 +1,9 @@ -[gd_scene format=3 uid="uid://635xs5haibcn"] +[gd_scene load_steps=2 format=3 uid="uid://635xs5haibcn"] + +[ext_resource type="PackedScene" uid="uid://ckh362yqjkpi0" path="res://scenes/Player.tscn" id="1_1w06w"] [node name="Game" type="Node2D"] + +[node name="Player" parent="." instance=ExtResource("1_1w06w")] +position = Vector2(800, 450) +scale = Vector2(0.6, 0.6) diff --git a/MB_FYP/script/player.cs b/MB_FYP/script/player.cs index 78d3048..f0fab1d 100644 --- a/MB_FYP/script/player.cs +++ b/MB_FYP/script/player.cs @@ -5,10 +5,12 @@ public partial class player : CharacterBody2D { [Export] public Vector2 ScreenSize; - [Export] - public int Speed { get; set; } = 10; - + public int MaxSpeed { get; set;} = 300; + [Export] + public int MainSpeed { get; set; } = 10; + [Export] + public int StrafeSpeed { get; set; } = 4; [Export] public float RotationSpeed { get; set; } = 2f; @@ -19,18 +21,34 @@ public partial class player : CharacterBody2D 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") + Transform.X * Input.GetAxis("down", "up")) * Speed; + Velocity += (Transform.Y * Input.GetAxis("strafe_left", "strafe_right") * StrafeSpeed) + (Transform.X * Input.GetAxis("down", "up") * MainSpeed); + Velocity = Velocity.LimitLength(MaxSpeed); + + 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 override void _Ready(){ // on init - GD.Print("afa"); + GD.Print(GetViewportRect().Size); ScreenSize = GetViewportRect().Size; } 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