Ship & Projectile moves properly

This commit is contained in:
Rain Clark 2024-05-12 00:07:06 -04:00
parent 2c90c6d318
commit 3d5e3cd8b8
8 changed files with 49 additions and 101 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

@ -4,10 +4,10 @@
[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_jyq7a"] [ext_resource type="Script" path="res://scripts/bullet.gd" id="1_jyq7a"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5nukh"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_5nukh"]
size = Vector2(16, 4) size = Vector2(4, 16)
[node name="Bullet" type="Area2D" groups=["projectile"]] [node name="Bullet" type="Area2D" groups=["projectile"]]
position = Vector2(7, 1) position = Vector2(0, -8)
collision_mask = 2 collision_mask = 2
script = ExtResource("1_jyq7a") script = ExtResource("1_jyq7a")
@ -16,10 +16,10 @@ position = Vector2(1, 0)
texture = ExtResource("1_6rsjj") texture = ExtResource("1_6rsjj")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(1, -1)
shape = SubResource("RectangleShape2D_5nukh") shape = SubResource("RectangleShape2D_5nukh")
[node name="LifeTimer" type="Timer" parent="."] [node name="LifeTimer" type="Timer" parent="."]
wait_time = 0.5
one_shot = true one_shot = true
autostart = true autostart = true

View File

@ -19,5 +19,3 @@ color = Color(0, 0, 0, 1)
[node name="Ship" parent="." instance=ExtResource("1_esyqo")] [node name="Ship" parent="." instance=ExtResource("1_esyqo")]
z_index = 1 z_index = 1
position = Vector2(622, 309) position = Vector2(622, 309)
max_speed = null
steering_factor = null

View File

@ -6,20 +6,15 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_115lu"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_115lu"]
size = Vector2(35, 35) size = Vector2(35, 35)
[node name="Ship" type="RigidBody2D" groups=["ship"]] [node name="Ship" type="CharacterBody2D" groups=["ship"]]
collision_mask = 2 collision_mask = 2
gravity_scale = 0.0
linear_damp = 1.0
angular_damp = 2.0
script = ExtResource("1_japvq") script = ExtResource("1_japvq")
engine_power = null
spin_power = null
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
texture = ExtResource("1_ymcdl") texture = ExtResource("1_ymcdl")
[node name="CPUParticles2D" type="CPUParticles2D" parent="Sprite2D"] [node name="CPUParticles2D" type="CPUParticles2D" parent="Sprite2D"]
position = Vector2(-16, 0) position = Vector2(0, 18)
emission_shape = 1 emission_shape = 1
emission_sphere_radius = 3.0 emission_sphere_radius = 3.0
spread = 105.9 spread = 105.9
@ -34,8 +29,8 @@ wait_time = 2.0
one_shot = true one_shot = true
[node name="Gun" type="Marker2D" parent="."] [node name="Gun" type="Marker2D" parent="."]
position = Vector2(0, -33)
[node name="Projectiles" type="Node2D" parent="."] [node name="Projectiles" type="Node" parent="."]
position = Vector2(-33, 0)
[connection signal="timeout" from="ShotCooldown" to="." method="_on_shot_cooldown_timeout"] [connection signal="timeout" from="ShotCooldown" to="." method="_on_shot_cooldown_timeout"]

View File

@ -1,16 +1,17 @@
extends Area2D extends Area2D
@export var max_speed := 1400.00 @export var speed := 1400.00
var direction := Vector2.ZERO var direction := Vector2.UP
func _process(delta: float) -> void: func _physics_process(delta: float) -> void:
position += direction * max_speed * delta # Movement
#print("Bullet Pos: " + str(position)) position += direction.rotated(rotation) * speed * delta
#var viewport_size := get_viewport_rect().size # Screenwrap
#position.x = wrap(position.x, 0, viewport_size.x) var viewport_size := get_viewport_rect().size
#position.y = wrap(position.y, 0,viewport_size.y) position.x = wrap(position.x, 0, viewport_size.x)
position.y = wrap(position.y, 0,viewport_size.y)
func _on_life_timer_timeout() -> void: func _on_life_timer_timeout() -> void:
queue_free() queue_free()

View File

@ -1,41 +1,45 @@
extends RigidBody2D extends CharacterBody2D
@export var engine_power := 16000.0
@export var spin_power := 200000.0
@onready var screensize = self.get_viewport_rect().size
const BULLET = preload("res://scenes/bullet.tscn") const BULLET = preload("res://scenes/bullet.tscn")
var thrust := Vector2.ZERO @export var accleration := 90.0
var rotation_direction := 0 @export var max_speed := 350.0
var can_shoot := false @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() var bullet = BULLET.instantiate()
bullet.position = pos bullet.position = pos
bullet.direction = dir
bullet.rotation = rot bullet.rotation = rot
add_child(bullet) $Projectiles.add_child(bullet)
can_shoot = false can_shoot = false
$ShotCooldown.start() $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: func _on_shot_cooldown_timeout() -> void:
can_shoot = true can_shoot = true

View File

@ -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