Refactored all of ship movement

This commit is contained in:
Rain Clark 2024-05-10 20:42:22 -04:00
parent c13e23cdd3
commit 2c90c6d318
8 changed files with 144 additions and 35 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 B

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

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

View File

@ -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="."]

View File

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

View File

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

View File

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

View File

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

50
scripts/ship.old.gd Normal file
View File

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