mirror of
https://codeberg.org/Melon-Bread/Comets.gd.git
synced 2024-11-24 21:18:21 -05:00
Ship & Projectile moves properly
This commit is contained in:
parent
2c90c6d318
commit
3d5e3cd8b8
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 |
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"]
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user