diff --git a/MB_FYP/assets/CollisionShapes/asteroid_cshape_big.tres b/MB_FYP/assets/CollisionShapes/asteroid_cshape_big.tres new file mode 100644 index 0000000..08e8c9f --- /dev/null +++ b/MB_FYP/assets/CollisionShapes/asteroid_cshape_big.tres @@ -0,0 +1,4 @@ +[gd_resource type="CircleShape2D" format=3 uid="uid://dcjv2ug3d1h17"] + +[resource] +radius = 49.0408 diff --git a/MB_FYP/assets/CollisionShapes/asteroid_cshape_med.tres b/MB_FYP/assets/CollisionShapes/asteroid_cshape_med.tres new file mode 100644 index 0000000..ca0caba --- /dev/null +++ b/MB_FYP/assets/CollisionShapes/asteroid_cshape_med.tres @@ -0,0 +1,4 @@ +[gd_resource type="CircleShape2D" format=3 uid="uid://bfwsa0ttu7l88"] + +[resource] +radius = 21.0238 diff --git a/MB_FYP/assets/CollisionShapes/asteroid_cshape_medcircle.tres b/MB_FYP/assets/CollisionShapes/asteroid_cshape_medcircle.tres new file mode 100644 index 0000000..6510ded --- /dev/null +++ b/MB_FYP/assets/CollisionShapes/asteroid_cshape_medcircle.tres @@ -0,0 +1,4 @@ +[gd_resource type="CircleShape2D" format=3 uid="uid://1hqufsk4g1lq"] + +[resource] +radius = 21.0238 diff --git a/MB_FYP/assets/CollisionShapes/asteroid_cshape_small.tres b/MB_FYP/assets/CollisionShapes/asteroid_cshape_small.tres new file mode 100644 index 0000000..369eeed --- /dev/null +++ b/MB_FYP/assets/CollisionShapes/asteroid_cshape_small.tres @@ -0,0 +1,4 @@ +[gd_resource type="CapsuleShape2D" format=3 uid="uid://chg4p8m1h5xbf"] + +[resource] +radius = 14.0 diff --git a/MB_FYP/assets/CollisionShapes/asteroid_cshape_smallcircle.tres b/MB_FYP/assets/CollisionShapes/asteroid_cshape_smallcircle.tres new file mode 100644 index 0000000..66ed380 --- /dev/null +++ b/MB_FYP/assets/CollisionShapes/asteroid_cshape_smallcircle.tres @@ -0,0 +1,4 @@ +[gd_resource type="CircleShape2D" format=3 uid="uid://iatbcsveyeh0"] + +[resource] +radius = 14.0357 diff --git a/MB_FYP/scenes/asteroid.tscn b/MB_FYP/scenes/asteroid.tscn new file mode 100644 index 0000000..0ba5883 --- /dev/null +++ b/MB_FYP/scenes/asteroid.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=4 format=3 uid="uid://dnvcics1ni4pa"] + +[ext_resource type="Script" path="res://script/asteroid.cs" id="1_rxx5v"] +[ext_resource type="Texture2D" uid="uid://bai5xn2p7e5pd" path="res://assets/PNG/Meteors/meteorBrown_small1.png" id="2_rmklb"] +[ext_resource type="Shape2D" uid="uid://iatbcsveyeh0" path="res://assets/CollisionShapes/asteroid_cshape_smallcircle.tres" id="3_pxcia"] + +[node name="Asteroid" type="Area2D"] +collision_layer = 4 +collision_mask = 11 +script = ExtResource("1_rxx5v") + +[node name="Sprite2D" type="Sprite2D" parent="."] +texture = ExtResource("2_rmklb") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(1, -1) +shape = ExtResource("3_pxcia") diff --git a/MB_FYP/scenes/game.tscn b/MB_FYP/scenes/game.tscn index 5004c23..5cc409c 100644 --- a/MB_FYP/scenes/game.tscn +++ b/MB_FYP/scenes/game.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=3 format=3 uid="uid://635xs5haibcn"] +[gd_scene load_steps=4 format=3 uid="uid://635xs5haibcn"] [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"] +[ext_resource type="PackedScene" uid="uid://dnvcics1ni4pa" path="res://scenes/asteroid.tscn" id="3_b8wlr"] [node name="Game" type="Node2D"] script = ExtResource("1_dukjm") @@ -12,4 +13,28 @@ scale = Vector2(0.6, 0.6) [node name="Lasers" type="Node" parent="."] -[connection signal="LaserShot" from="Player" to="." method="_on_player_laser_shot"] +[node name="Asteroids" type="Node" parent="."] + +[node name="Asteroid" parent="Asteroids" instance=ExtResource("3_b8wlr")] +position = Vector2(1228, 195) + +[node name="Asteroid2" parent="Asteroids" instance=ExtResource("3_b8wlr")] +position = Vector2(332, 661) +size = 1 + +[node name="Asteroid3" parent="Asteroids" instance=ExtResource("3_b8wlr")] +position = Vector2(1450, 641) +size = 2 + +[node name="Asteroid4" parent="Asteroids" instance=ExtResource("3_b8wlr")] +position = Vector2(769, 202) + +[node name="Asteroid5" parent="Asteroids" instance=ExtResource("3_b8wlr")] +position = Vector2(387, 230) + +[connection signal="LaserShot" from="Player" to="." method="OnPlayerLaserShot"] +[connection signal="Exploded" from="Asteroids/Asteroid" to="." method="OnAsteroidExploded"] +[connection signal="Exploded" from="Asteroids/Asteroid2" to="." method="OnAsteroidExploded"] +[connection signal="Exploded" from="Asteroids/Asteroid3" to="." method="OnAsteroidExploded"] +[connection signal="Exploded" from="Asteroids/Asteroid4" to="." method="OnAsteroidExploded"] +[connection signal="Exploded" from="Asteroids/Asteroid5" to="." method="OnAsteroidExploded"] diff --git a/MB_FYP/scenes/laser.tscn b/MB_FYP/scenes/laser.tscn index 7e60e1f..df7a0ca 100644 --- a/MB_FYP/scenes/laser.tscn +++ b/MB_FYP/scenes/laser.tscn @@ -9,6 +9,8 @@ height = 56.0 [node name="Laser" type="Area2D"] scale = Vector2(0.6, 0.6) +collision_layer = 8 +collision_mask = 5 script = ExtResource("1_ctmji") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] @@ -20,4 +22,5 @@ texture = ExtResource("2_nq6d6") [node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."] scale = Vector2(0.5, 2.75) +[connection signal="area_entered" from="." to="." method="OnAreaEntered"] [connection signal="screen_exited" from="VisibleOnScreenNotifier2D" to="." method="_on_visible_on_screen_notifier_2d_screen_exited"] diff --git a/MB_FYP/script/Laser.cs b/MB_FYP/script/Laser.cs index ac89dd0..b99be99 100644 --- a/MB_FYP/script/Laser.cs +++ b/MB_FYP/script/Laser.cs @@ -9,10 +9,21 @@ public partial class Laser : Area2D public Vector2 MovementVector { get; set; } = new Vector2(0, -1); - private void _on_visible_on_screen_notifier_2d_screen_exited(){ + private void _on_visible_on_screen_notifier_2d_screen_exited() + { QueueFree(); } - public override void _PhysicsProcess(double delta){ + public override void _PhysicsProcess(double delta) + { GlobalPosition += MovementVector.Rotated(Rotation) * Speed * (float)delta; } + private void OnAreaEntered(Area2D area) + { + if (area is asteroid) + { + asteroid a = (asteroid)area; + a.Explode(); + QueueFree(); + } + } } diff --git a/MB_FYP/script/asteroid.cs b/MB_FYP/script/asteroid.cs new file mode 100644 index 0000000..e0b75ca --- /dev/null +++ b/MB_FYP/script/asteroid.cs @@ -0,0 +1,101 @@ +using Godot; +using System; + +public partial class asteroid : Area2D +{ + [Signal] + public delegate void ExplodedEventHandler(Vector2 pos, int size); + + public enum AsteroidSize {LARGE, MEDIUM, SMALL}; + + private string[] LargeAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorBrown_big1.png", + "res://assets/PNG/Meteors/meteorBrown_big4.png", + "res://assets/PNG/Meteors/meteorGrey_big4.png"}; + + private string[] MediumAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorGrey_med1.png", + "res://assets/PNG/Meteors/meteorBrown_med3.png", + "res://assets/PNG/Meteors/meteorBrown_med1.png"}; + + private string[] SmallAsteroidTexture = new string[] {"res://assets/PNG/Meteors/meteorBrown_small1.png", + "res://assets/PNG/Meteors/meteorBrown_small2.png", + "res://assets/PNG/Meteors/meteorGrey_small1.png"}; + + + [Export] + public AsteroidSize size; + public int Speed = 200; + + public Vector2 MovementVector; + + public Sprite2D Sprite = new Sprite2D(); + + public CollisionShape2D ColShape = null; + + public override void _Ready() + { + Sprite = GetNode("Sprite2D"); + ColShape = GetNode("CollisionShape2D"); + var rand = new Random(); + Rotation = (float)rand.NextDouble(); + Rotation = (float)((Rotation * 2) * 3.14); + MovementVector = new Vector2(0, -1); + + + CircleShape2D ColShapeL; + switch (size) + { + case AsteroidSize.LARGE: + Speed = rand.Next(50, 100); + Sprite.Texture = GD.Load(LargeAsteroidTexture[rand.Next(3)]); + ColShapeL = GD.Load("res://assets/CollisionShapes/asteroid_cshape_big.tres"); + ColShape.SetDeferred("shape", ColShapeL); + break; + + case AsteroidSize.MEDIUM: + Speed = rand.Next(100, 150); + Sprite.Texture = GD.Load(MediumAsteroidTexture[rand.Next(3)]); + ColShapeL = GD.Load("res://assets/CollisionShapes/asteroid_cshape_medcircle.tres"); + ColShape.SetDeferred("shape", ColShapeL); + break; + + case AsteroidSize.SMALL: + Speed = rand.Next(150, 200); + Sprite.Texture = GD.Load(SmallAsteroidTexture[rand.Next(3)]); + ColShapeL = GD.Load("res://assets/CollisionShapes/asteroid_cshape_smallcircle.tres"); + ColShape.SetDeferred("shape", ColShapeL); + break; + } + } + + public void Explode() + { + EmitSignal(SignalName.Exploded, GlobalPosition, (int)size); + QueueFree(); + } + + + public override void _PhysicsProcess(double delta) + { + GlobalPosition += MovementVector.Rotated(Rotation) * Speed * (float)delta; + CircleShape2D circleshape = (CircleShape2D)ColShape.Shape; + var radius = circleshape.Radius; + var ScreenSize = GetViewportRect().Size; + + if (GlobalPosition.Y + radius < 0) + { + GlobalPosition = new Vector2(GlobalPosition.X, ScreenSize.Y + radius); + } + else if (GlobalPosition.Y - radius > ScreenSize.Y) + { + GlobalPosition = new Vector2(GlobalPosition.X, -radius); + } + if (GlobalPosition.X + radius < 0) + { + GlobalPosition = new Vector2(ScreenSize.X + radius, GlobalPosition.Y); + } + else if (GlobalPosition.X - radius > ScreenSize.X) + { + GlobalPosition = new Vector2(-radius, GlobalPosition.Y); + } + } +} diff --git a/MB_FYP/script/game.cs b/MB_FYP/script/game.cs index b04dd67..d44b1fd 100644 --- a/MB_FYP/script/game.cs +++ b/MB_FYP/script/game.cs @@ -5,33 +5,57 @@ public partial class game : Node2D { public Node Lasers = null; public CharacterBody2D Player = null; - //public Area2D Laser = null; + public Node Asteroids = null; + + private readonly PackedScene AsteroidScene = GD.Load("res://scenes/asteroid.tscn"); public override void _Ready() { + Asteroids = GetNode("Asteroids"); + var a = new asteroid(); + for (int i = 0; i == Asteroids.GetChildCount() - 1; i++){ + a = (asteroid)Asteroids.GetChild(i); + a.Exploded += OnAsteroidExploded; + } + Lasers = GetNode("Lasers"); Player = GetNode("Player"); - //Laser = GetNode("Laser"); - //Callable OnLaserShot = new Callable(this, "_on_player_laser_shot"); - //Player.Connect("LaserShot", ); var p = new player(); - //var l = new Laser(); - p.LaserShot += _on_player_laser_shot; + p.LaserShot += OnPlayerLaserShot; - - - - - } - public void _on_player_laser_shot(Area2D Laser) + public void SpawnAsteroid(Vector2 position, int size) + { + var a = new asteroid(); + a = AsteroidScene.Instantiate(); + a.GlobalPosition = position; + a.size = (asteroid.AsteroidSize)size; + a.Exploded += OnAsteroidExploded; + Asteroids.CallDeferred("add_child", a); + } + +//Signals and Connections + public void OnPlayerLaserShot(Area2D Laser) { Lasers.AddChild(Laser); GD.Print(Laser.Position); GD.Print(Player.Position); } + public void OnAsteroidExploded(Vector2 pos, int size) + { + GD.Print(size); + for(int i = 0; i < 2; i++){ + if (size == 0){ + SpawnAsteroid(pos, (int)asteroid.AsteroidSize.MEDIUM); + } + else if (size == 1){ + SpawnAsteroid(pos, (int)asteroid.AsteroidSize.SMALL); + } + } + } + } diff --git a/MB_FYP/script/player.cs b/MB_FYP/script/player.cs index 43114a0..310ac8e 100644 --- a/MB_FYP/script/player.cs +++ b/MB_FYP/script/player.cs @@ -32,6 +32,7 @@ public partial class player : CharacterBody2D 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); } @@ -52,8 +53,6 @@ public partial class player : CharacterBody2D Rotation = Rotation }; EmitSignal(SignalName.LaserShot, Laser); - //GetParent().AddChild(Laser); - //AddChild(Laser); } public override void _Ready()