From 2c90c6d31811bc94b47524ee0d6b281e15650470 Mon Sep 17 00:00:00 2001 From: Melon Bread Date: Fri, 10 May 2024 20:42:22 -0400 Subject: [PATCH] Refactored all of ship movement --- assets/img/bullet.png | Bin 113 -> 4226 bytes project.godot | 12 ++++++++++ scenes/bullet.tscn | 6 +++-- scenes/game.tscn | 16 +++++++++++++ scenes/ship.tscn | 30 ++++++++++++++++------- scripts/bullet.gd | 11 +++++---- scripts/ship.gd | 54 ++++++++++++++++++++++++++---------------- scripts/ship.old.gd | 50 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 144 insertions(+), 35 deletions(-) create mode 100644 scripts/ship.old.gd diff --git a/assets/img/bullet.png b/assets/img/bullet.png index 831ed575c05376d18dc555f23458782318f5db5f..99cd184c30cf83bcd8df98ecebb8e0f448a774f7 100644 GIT binary patch literal 4226 zcmeHKYitx%6rSw^aCr!|*a}p(3ray|XJ((BNw-kD+jfC1O{(wIQ2}XI>Gqc;5l9(p)uhZP#Gv|Ki zyZ3(g+|$mgs><@=`Z0QfAclJ?T-DHzRNIh2@O%8tP6XX|xW?o6c%~3T;02Jn3__=3 zpy_(qX}WYG9el4r%K}@)sBMfcQ|sHn9-Fc^f?bfpq=C)o(um>EZ-r(CI~)2Bp-HbN z=Q<3w`V7&2D!@{iyZUmEx5{8PF|^522=TPp%F#BCp$&|cGqaq{4rtx>o)xJ~e{7ss z2Q15_($XqVX{jL+4f~}4CWyq=SJpc#j!qwUyzZqn)l-%>7wmj<2Y1fx9@jW}byfZu z{T}Y^m+xeH$85;XDew5=^5ql!*^cA2^9Ii=8F`OrYK~MZ>$5sHXTNwX5jnpu{@2s- z>%^k{U;Fk?((T@Y#cEIf=DN|Xb)#HEmgTyA{0GI8R!fPs`xf51)tYm)_TtqCZB2Z+eQ4IS==|o}Z^$Rnw9{?5=S~$hEw3qv zeR}@*$AR6)emKUs&lX&IPp-Xk=y>g(A8YS!ySL)X(gUBJifx%tfkw?7nLoWOZ?q5P zSAJBsdeyMsC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ES#X?%UB%b?!>U}oXkrG1_o_U7sn8b-sF@6O#lA>|1UrD{D~7MI(%iBfk0S~ lGwI+8#>H$sQU(z`K(z;06#n!neFSP_@O1TaS?83{1OV`B9Pj`D diff --git a/project.godot b/project.godot index 75f8411..218e0ea 100644 --- a/project.godot +++ b/project.godot @@ -15,8 +15,20 @@ run/main_scene="res://scenes/game.tscn" config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://icon.svg" +[dotnet] + +project/assembly_name="Comets.gd" + [input] +ui_accept={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194309,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194310,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":32,"physical_keycode":0,"key_label":0,"unicode":32,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":true,"script":null) +] +} ui_left={ "deadzone": 0.5, "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194319,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn index 13bdfd4..8d56c96 100644 --- a/scenes/bullet.tscn +++ b/scenes/bullet.tscn @@ -4,10 +4,11 @@ [ext_resource type="Script" path="res://scripts/bullet.gd" id="1_jyq7a"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_5nukh"] -size = Vector2(4, 16) +size = Vector2(16, 4) [node name="Bullet" type="Area2D" groups=["projectile"]] -collision_layer = 2 +position = Vector2(7, 1) +collision_mask = 2 script = ExtResource("1_jyq7a") [node name="Sprite2D" type="Sprite2D" parent="."] @@ -15,6 +16,7 @@ position = Vector2(1, 0) texture = ExtResource("1_6rsjj") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(1, -1) shape = SubResource("RectangleShape2D_5nukh") [node name="LifeTimer" type="Timer" parent="."] diff --git a/scenes/game.tscn b/scenes/game.tscn index c063b0b..6cca3ee 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -4,4 +4,20 @@ [node name="Game" type="Node2D"] +[node name="Background" type="CanvasLayer" parent="."] +layer = -1 + +[node name="ColorRect" type="ColorRect" parent="Background"] +z_index = -1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +color = Color(0, 0, 0, 1) + [node name="Ship" parent="." instance=ExtResource("1_esyqo")] +z_index = 1 +position = Vector2(622, 309) +max_speed = null +steering_factor = null diff --git a/scenes/ship.tscn b/scenes/ship.tscn index 5961273..b1f2e59 100644 --- a/scenes/ship.tscn +++ b/scenes/ship.tscn @@ -3,12 +3,17 @@ [ext_resource type="Script" path="res://scripts/ship.gd" id="1_japvq"] [ext_resource type="Texture2D" uid="uid://qcnuvee2jcp0" path="res://assets/img/ship.png" id="1_ymcdl"] -[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_hwy8o"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_115lu"] +size = Vector2(35, 35) -[node name="Ship" type="Area2D" groups=["ship"]] +[node name="Ship" type="RigidBody2D" groups=["ship"]] +collision_mask = 2 +gravity_scale = 0.0 +linear_damp = 1.0 +angular_damp = 2.0 script = ExtResource("1_japvq") -max_speed = null -steering_factor = null +engine_power = null +spin_power = null [node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("1_ymcdl") @@ -20,10 +25,17 @@ emission_sphere_radius = 3.0 spread = 105.9 gravity = Vector2(0, 0) -[node name="Gun" type="Marker2D" parent="Sprite2D"] -position = Vector2(32, 0) - [node name="CollisionShape2D" type="CollisionShape2D" parent="."] rotation = 1.54049 -scale = Vector2(1.36118, -2.39866) -shape = SubResource("CapsuleShape2D_hwy8o") +shape = SubResource("RectangleShape2D_115lu") + +[node name="ShotCooldown" type="Timer" parent="."] +wait_time = 2.0 +one_shot = true + +[node name="Gun" type="Marker2D" parent="."] + +[node name="Projectiles" type="Node2D" parent="."] +position = Vector2(-33, 0) + +[connection signal="timeout" from="ShotCooldown" to="." method="_on_shot_cooldown_timeout"] diff --git a/scripts/bullet.gd b/scripts/bullet.gd index d7162ac..441cd57 100644 --- a/scripts/bullet.gd +++ b/scripts/bullet.gd @@ -1,13 +1,16 @@ extends Area2D @export var max_speed := 1400.00 - var direction := Vector2.ZERO + func _process(delta: float) -> void: - var viewport_size := get_viewport_rect().size - position.x = wrap(position.x, 0, viewport_size.x) - position.y = wrap(position.y, 0,viewport_size.y) + position += direction * max_speed * delta + #print("Bullet Pos: " + str(position)) + + #var viewport_size := get_viewport_rect().size + #position.x = wrap(position.x, 0, viewport_size.x) + #position.y = wrap(position.y, 0,viewport_size.y) func _on_life_timer_timeout() -> void: queue_free() diff --git a/scripts/ship.gd b/scripts/ship.gd index 8cb3ce4..7aab87b 100644 --- a/scripts/ship.gd +++ b/scripts/ship.gd @@ -1,27 +1,41 @@ -extends Area2D +extends RigidBody2D -@export var max_speed := 1200.0 -@export var steering_factor := 3.0 +@export var engine_power := 16000.0 +@export var spin_power := 200000.0 -var velocity := Vector2.ZERO +@onready var screensize = self.get_viewport_rect().size -func _process(delta: float) -> void: - # TODO: Maybe look into tank controls like classic Astroids - var direction := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") - if direction.length() > 1.0: - direction.normalized() +const BULLET = preload("res://scenes/bullet.tscn") - var desired_velocity := max_speed * direction - var steering := desired_velocity - velocity - velocity += steering * steering_factor * delta - position += velocity * delta +var thrust := Vector2.ZERO +var rotation_direction := 0 +var can_shoot := false - if velocity.length() > 0.0: - # TODO: Have the CollisionShape toate with the sprite - $Sprite2D.rotation = velocity.angle() +func shoot_gun(pos : Vector2, dir : Vector2, rot : float) -> void: + var bullet = BULLET.instantiate() + bullet.position = pos + bullet.direction = dir + bullet.rotation = rot + add_child(bullet) + can_shoot = false + $ShotCooldown.start() - var viewport_size := get_viewport_rect().size - position.x = wrap(position.x, 0, viewport_size.x) - position.y = wrap(position.y, 0,viewport_size.y) +func get_input() -> void: + thrust = Vector2.ZERO + if Input.is_action_pressed("ui_up"): + thrust = self.transform.x * engine_power + rotation_direction = Input.get_axis("ui_left", "ui_right") -func fire_gun() -> void: +func _integrate_forces(state: PhysicsDirectBodyState2D) -> void: + var xform := state.transform + xform.origin.x = wrapf(xform.origin.x, 0 , screensize.x) + xform.origin.y = wrapf(xform.origin.y , 0, screensize.y) + state.transform = xform + +func _physics_process(delta: float) -> void: + get_input() + self.constant_force = thrust * delta + self.constant_torque = rotation_direction * spin_power * delta + +func _on_shot_cooldown_timeout() -> void: + can_shoot = true diff --git a/scripts/ship.old.gd b/scripts/ship.old.gd new file mode 100644 index 0000000..f3182a8 --- /dev/null +++ b/scripts/ship.old.gd @@ -0,0 +1,50 @@ +extends Area2D + +@export var max_speed := 1200.0 +@export var steering_factor := 3.0 + +const BULLET = preload("res://scenes/bullet.tscn") +var velocity := Vector2.ZERO +var gun_direction := Vector2.ZERO +var can_shoot := true + +func _process(delta: float) -> void: + # TODO: Maybe look into tank controls like classic Astroids + var direction := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") + if direction.length() > 1.0: + direction.normalized() + if direction.length() != 0.0: + gun_direction = direction + + + var desired_velocity := max_speed * direction + var steering := desired_velocity - velocity + velocity += steering * steering_factor * delta + position += velocity * delta + + if velocity.length() > 0.0: + # TODO: Have the CollisionShape toate with the sprite + $Sprite2D.rotation = velocity.angle() + print("Angle: " + str($Sprite2D.rotation)) + + + var viewport_size := get_viewport_rect().size + position.x = wrap(position.x, 0, viewport_size.x) + position.y = wrap(position.y, 0,viewport_size.y) + + + if Input.is_action_pressed("ui_accept") and can_shoot: + fire_gun($Gun.position, $Sprite2D.rotation, velocity.angle()) + + +func fire_gun(pos : Vector2, dir : Vector2, rot : float) -> void: + var bullet = BULLET.instantiate() + bullet.position = pos + bullet.direction = dir + bullet.rotation = rot + add_child(bullet) + can_shoot = false + $ShotCooldown.start() + +func _on_timer_timeout() -> void: + can_shoot = true