From 3d5e3cd8b8089feab4cd9c9fa6818b3c3ecf65eb Mon Sep 17 00:00:00 2001 From: Melon Bread Date: Sun, 12 May 2024 00:07:06 -0400 Subject: [PATCH] Ship & Projectile moves properly --- assets/img/bullet.png | Bin 4226 -> 4459 bytes assets/img/ship.png | Bin 4619 -> 4882 bytes scenes/bullet.tscn | 6 ++-- scenes/game.tscn | 2 -- scenes/ship.tscn | 13 +++------ scripts/bullet.gd | 17 ++++++------ scripts/ship.gd | 62 ++++++++++++++++++++++-------------------- scripts/ship.old.gd | 50 ---------------------------------- 8 files changed, 49 insertions(+), 101 deletions(-) delete mode 100644 scripts/ship.old.gd diff --git a/assets/img/bullet.png b/assets/img/bullet.png index 99cd184c30cf83bcd8df98ecebb8e0f448a774f7..528641eeb8c9517d4ee46dc9e8222ad2b6081571 100644 GIT binary patch delta 710 zcmZote62L0nUQ&7i=mG>69ajEu)q_WCRyVI!;HYb`u(r_L<&U;$_?)=5| zVmfRrE{|C1jeTZUG*_fuHnVsy>>e2z)_J~D-Dj7=N9F6H{r4@V$B0#=T{)BXtaI^^ zq?O%M_0m#f+RXESY{zr@@s>J5j)4K}@X*0VOT$Gl@W?BAAiYMsauO_A;c zza?vfb$-glIzNI%=P{7EI zhgnrudcL6#FzKjyx;TbdoK8+T!1V9`|Nrtc&z}H-24(}p01h`^3Dblt4F{7L7_<+t UC^T%FW&}!Yp00i_>zopr0D~?A=l}o! delta 647 zcmaE@)TB6}nUQH?i=mGP0|Nsi5W{G9Pu~CqBRxX{J!1<71|tI_6DtE#D?MD~h+7+O9g=?+V zjF?}nzEYD!#N7{WeemFT($#}UBg5Il9R>a{6qXePl}r@+KV2m1NJYV&{_^i~ zcaE;fS#jj@5kuFj8u#ZHM?TzhG;-~=$lvq+v_5Rww&_giEM<>GL2m&?2RB*KkVHkl z)oxv#Ja5%){~Yd%SDEg!j!VK)ETEa|Rm_)#czef6H*>vwOjKfmSoZ^n}TqU8&`WJ4Jk zcq=kPB1$5BeXNo<&tz(3Q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0Ffkp3qdqQIv z-PBh*x$S(Y$7b~Myo!PH3U>~Dg_MimudvkHj`xmsD%tKnt2C+3#dXDT zUctT%9KU8R-LQIN`sV|Bf8SU=2sJM_^DT4r7j3QudtV>+vFq3p{>}5^?&vdjvgLkx zrM}-Bf~ ov>ZX5*E4Rqa&%j)FBf|xe*5{1_@3QL7eEp2>FVdQ&MBb@0EXx3FaQ7m delta 1087 zcmV-F1i<@}CW|DHg8>4OgfUD=000010000;000010000-Nlj27GA=PNE;BX&05UK# zG&(RfIxsjOGBG+aH##vk00962paTE_00001000000000NmwBH600D)Q0Ra_%J4nM& z6o&t%ma0?~>>%Qhp*q>Y2jW<(P=pGhR%q412Oo=72N!2u9b5%L z@B!lN;H2mxC7zoUTEu$bazFli{-1l#1%!H;X;xPZ&~)2OCE{WxyDA1>5kL=o7{G+g zEMrcRlIU1p_tZ&s7v))<`~IwdJ~d}Cz$X&Nm|@z)8^kl4w!wLyILwN&N_V>lA~G&9FfKDTlMDt{ z3NScSGBG+aH##wsYz8C>FgR2)F*-3fIx(}F1`+|Yj|gB07P%B2AOHXW24YJ`L;(K) z{{a7>y{D6t1rZ>B2>}rh%!jJ>0004}NklE8>5F`MZL9hU11R(;D3B)-gti4Cd2)H0*&kVmQN3BC>!0im0A|=pd#6Ly=gkMGht3`ZQ9Q40O1P24iMA(x@D4D0Rl7}vd+|&AVL7J z1ff>ht*qcQL8w-BtIJpo5TOBRfKV@Kcb@DdAi%=l?sGm1gq0x=%?{GNrK7tg>w;vlb3*1=zF;vT%H8IJ)Br}JM8tv z2s;9-p2_SwNTEgIiy*wa1f-!KNw{hfj41pu0;;`A8%D14k!O1dpeHVy@V_OXRqjUn zG!e2Rq#gm%+0XE43abkLXsP~j5~~U)0jThK8mkJw#tU72ka!n`I3EB2002ovPDHLk FV1ksCx+(ww diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn index 8d56c96..cb0e651 100644 --- a/scenes/bullet.tscn +++ b/scenes/bullet.tscn @@ -4,10 +4,10 @@ [ext_resource type="Script" path="res://scripts/bullet.gd" id="1_jyq7a"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_5nukh"] -size = Vector2(16, 4) +size = Vector2(4, 16) [node name="Bullet" type="Area2D" groups=["projectile"]] -position = Vector2(7, 1) +position = Vector2(0, -8) collision_mask = 2 script = ExtResource("1_jyq7a") @@ -16,10 +16,10 @@ 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="."] +wait_time = 0.5 one_shot = true autostart = true diff --git a/scenes/game.tscn b/scenes/game.tscn index 6cca3ee..6059089 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -19,5 +19,3 @@ 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 b1f2e59..b0c76f4 100644 --- a/scenes/ship.tscn +++ b/scenes/ship.tscn @@ -6,20 +6,15 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_115lu"] size = Vector2(35, 35) -[node name="Ship" type="RigidBody2D" groups=["ship"]] +[node name="Ship" type="CharacterBody2D" groups=["ship"]] collision_mask = 2 -gravity_scale = 0.0 -linear_damp = 1.0 -angular_damp = 2.0 script = ExtResource("1_japvq") -engine_power = null -spin_power = null [node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("1_ymcdl") [node name="CPUParticles2D" type="CPUParticles2D" parent="Sprite2D"] -position = Vector2(-16, 0) +position = Vector2(0, 18) emission_shape = 1 emission_sphere_radius = 3.0 spread = 105.9 @@ -34,8 +29,8 @@ wait_time = 2.0 one_shot = true [node name="Gun" type="Marker2D" parent="."] +position = Vector2(0, -33) -[node name="Projectiles" type="Node2D" parent="."] -position = Vector2(-33, 0) +[node name="Projectiles" type="Node" parent="."] [connection signal="timeout" from="ShotCooldown" to="." method="_on_shot_cooldown_timeout"] diff --git a/scripts/bullet.gd b/scripts/bullet.gd index 441cd57..9ee019d 100644 --- a/scripts/bullet.gd +++ b/scripts/bullet.gd @@ -1,16 +1,17 @@ extends Area2D -@export var max_speed := 1400.00 -var direction := Vector2.ZERO +@export var speed := 1400.00 +var direction := Vector2.UP -func _process(delta: float) -> void: - position += direction * max_speed * delta - #print("Bullet Pos: " + str(position)) +func _physics_process(delta: float) -> void: + # Movement + position += direction.rotated(rotation) * speed * delta - #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) + # Screenwrap + 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 7aab87b..ef1a755 100644 --- a/scripts/ship.gd +++ b/scripts/ship.gd @@ -1,41 +1,45 @@ -extends RigidBody2D - -@export var engine_power := 16000.0 -@export var spin_power := 200000.0 - -@onready var screensize = self.get_viewport_rect().size +extends CharacterBody2D const BULLET = preload("res://scenes/bullet.tscn") -var thrust := Vector2.ZERO -var rotation_direction := 0 -var can_shoot := false +@export var accleration := 90.0 +@export var max_speed := 350.0 +@export var rotation_speed := 200.0 -func shoot_gun(pos : Vector2, dir : Vector2, rot : float) -> void: +var can_shoot := true + +func _physics_process(delta: float) -> void: + # Controls + var accleration_direction := Vector2(0, Input.get_axis("ui_up", "ui_down")) + var rotation_direction := Input.get_axis("ui_left", "ui_right") + + if Input.is_action_pressed("ui_accept") and can_shoot: + fire_gun($Gun.global_position, self.global_rotation) + + # Movement Logic + ## Accleration + velocity += accleration_direction.rotated(rotation) * accleration * delta + velocity.limit_length(max_speed) + ### Slowly stops movement if no accleration + if accleration_direction.y == 0: + velocity = velocity.move_toward(Vector2.ZERO, 1) + + ## Rotation + rotate(deg_to_rad(rotation_direction * rotation_speed * delta)) + move_and_slide() + + # Screen Wrap + 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 fire_gun(pos : Vector2, rot : float) -> void: var bullet = BULLET.instantiate() bullet.position = pos - bullet.direction = dir bullet.rotation = rot - add_child(bullet) + $Projectiles.add_child(bullet) can_shoot = false $ShotCooldown.start() -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 _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 deleted file mode 100644 index f3182a8..0000000 --- a/scripts/ship.old.gd +++ /dev/null @@ -1,50 +0,0 @@ -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