forked from kanium/starcheese
I should commit more often
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
@export var acceleration: float = 400.0
|
||||
@export var max_speed: float = 1500.0
|
||||
@export var rotation_speed: float = 3.0
|
||||
@export var friction: float = 0.99
|
||||
|
||||
var weapons_system: WeaponsSystem
|
||||
var event_bus: EventBus
|
||||
func _ready():
|
||||
weapons_system = get_node("/root/Main/WeaponsSystem")
|
||||
event_bus = get_node("/root/Main/MainEventBus")
|
||||
event_bus.subscribe_to(get_node("/root/Main/InputEventScope"), "KeyboardInputEvent", Callable(self, "handle_keyboard_input"))
|
||||
|
||||
|
||||
func get_spaceship_orientation_vector() -> Vector2:
|
||||
return -global_transform.y
|
||||
|
||||
func handle_movement(delta: float) -> void:
|
||||
var thrust = Vector2.ZERO
|
||||
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
thrust = Vector2(1, 0).rotated(rotation) * acceleration * delta
|
||||
velocity += thrust
|
||||
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
thrust = Vector2(-1, 0).rotated(rotation) * acceleration * delta
|
||||
velocity += thrust
|
||||
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
rotation -= rotation_speed * delta
|
||||
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
rotation += rotation_speed * delta
|
||||
|
||||
# Clamp velocity and apply friction
|
||||
velocity = velocity.clamp(Vector2(-max_speed, -max_speed), Vector2(max_speed, max_speed))
|
||||
velocity *= friction
|
||||
|
||||
var collision = move_and_collide(velocity * delta)
|
||||
|
||||
if collision:
|
||||
print("Collided with: ", collision.get_collider().name)
|
||||
|
||||
var collision_normal = collision.get_normal()
|
||||
velocity = velocity.slide(collision_normal)
|
||||
|
||||
#func _unhandled_input(event: InputEvent) -> void:
|
||||
# if not event.is_action("fire_weapon"):
|
||||
# return
|
||||
#laser.is_casting = event.is_action_pressed("fire_weapon")
|
||||
# pass
|
||||
|
||||
func handle_keyboard_input(event: KeyboardInputEvent):
|
||||
if( event.action == "ui_fire" ):
|
||||
if( event.pressed ):
|
||||
weapons_system.fire_all()
|
||||
else:
|
||||
weapons_system.cease_fire_all()
|
||||
|
||||
pass
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
handle_movement(delta)
|
||||
@@ -0,0 +1,20 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://b1goask3ynem3"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://cran7fr1i2qou" path="res://images/spaceship-placeholder.png" id="1_2uldm"]
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_3vtwu"]
|
||||
radius = 48.0
|
||||
height = 102.0
|
||||
|
||||
[node name="spaceship" type="CharacterBody2D"]
|
||||
position = Vector2(956, 623)
|
||||
collision_layer = 2
|
||||
motion_mode = 1
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
z_index = 1
|
||||
rotation = 1.5708
|
||||
texture = ExtResource("1_2uldm")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CapsuleShape2D_3vtwu")
|
||||
Reference in New Issue
Block a user