Reworked player controller
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
class_name AbstractShip
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
@onready var sprite := $Sprite2D
|
||||
@onready var collision := $CollisionShape2D
|
||||
|
||||
|
||||
@export_range(0, 250) var acceleration : int = 0
|
||||
@export_range(0, 250) var deceleration : int = 0
|
||||
@export_range(0, 250) var max_speed : int = 0
|
||||
|
||||
@export var weapon_positions: Array[Vector2]
|
||||
|
||||
|
||||
var _weapons : Array[AbstractWeapon]
|
||||
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
var was_collided := move_and_slide()
|
||||
if was_collided:
|
||||
var normal := get_wall_normal()
|
||||
velocity -= normal.abs() * velocity
|
||||
|
||||
|
||||
func accelerate(direction: Vector2, delta: float) -> void:
|
||||
var accel : Vector2 = direction * acceleration * delta
|
||||
var decel : float = deceleration * delta
|
||||
|
||||
velocity.x = _get_new_speed(accel.x, decel, velocity.x)
|
||||
velocity.y = _get_new_speed(accel.y, decel, velocity.y)
|
||||
|
||||
if velocity.length() > max_speed:
|
||||
velocity = velocity.normalized() * max_speed
|
||||
|
||||
|
||||
func shoot(weapon_index: int) -> void:
|
||||
if weapon_index >= _weapons.size(): return
|
||||
|
||||
_weapons[weapon_index].shoot(velocity)
|
||||
|
||||
|
||||
func reload(weapon_index: int) -> void:
|
||||
if weapon_index >= _weapons.size(): return
|
||||
|
||||
_weapons[weapon_index].reload()
|
||||
|
||||
|
||||
func _get_new_speed(accel: float, decel: float, current_speed: float) -> float:
|
||||
if is_zero_approx(accel):
|
||||
if absf(current_speed) < decel:
|
||||
return 0.0
|
||||
return current_speed + (decel if current_speed < 0 else -decel)
|
||||
else:
|
||||
return current_speed + accel
|
||||
|
||||
|
||||
func _on_health_depleted() -> void:
|
||||
queue_free()
|
||||
@@ -0,0 +1 @@
|
||||
uid://cesibaqtrgotl
|
||||
@@ -0,0 +1,25 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://jvyagshykmgb"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cesibaqtrgotl" path="res://game/entities/ships/abstract_ship.gd" id="1_6isjb"]
|
||||
[ext_resource type="Script" uid="uid://d3g4xbq45qmpr" path="res://game/health_system/health.gd" id="2_n766o"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_xxtvk"]
|
||||
|
||||
[node name="AbstractShip" type="CharacterBody2D"]
|
||||
disable_mode = 1
|
||||
motion_mode = 1
|
||||
wall_min_slide_angle = 3.1415927
|
||||
script = ExtResource("1_6isjb")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("CircleShape2D_xxtvk")
|
||||
|
||||
[node name="Health" type="Node" parent="."]
|
||||
script = ExtResource("2_n766o")
|
||||
max_shield = 1000
|
||||
max_armor = 1000
|
||||
max_hull = 1000
|
||||
|
||||
[connection signal="depleted" from="Health" to="." method="_on_health_depleted"]
|
||||
@@ -0,0 +1,35 @@
|
||||
class_name PlayerShip
|
||||
extends AbstractShip
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@warning_ignore("unused_local_constant")
|
||||
const CANNON = preload("res://game/entities/weapons/cannon/cannon.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const GATLING = preload("res://game/entities/weapons/gatling/gatling.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const LASER = preload("res://game/entities/weapons/laser/laser.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const LAUNCHER = preload("res://game/entities/weapons/launcher/launcher.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const MINELAYER = preload("res://game/entities/weapons/minelayer/minelayer.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const PLASMA = preload("res://game/entities/weapons/plasma/plasma.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const RAILGUN = preload("res://game/entities/weapons/railgun/railgun.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const SHRAPNEL = preload("res://game/entities/weapons/shrapnel/shrapnel.tscn")
|
||||
@warning_ignore("unused_local_constant")
|
||||
const TESLA = preload("res://game/entities/weapons/tesla/tesla.tscn")
|
||||
|
||||
var weapons := [
|
||||
GATLING.instantiate(),
|
||||
RAILGUN.instantiate(),
|
||||
]
|
||||
|
||||
for index in weapons.size():
|
||||
var weapon : Node2D = weapons[index]
|
||||
if index < weapon_positions.size():
|
||||
weapon.position = weapon_positions[index]
|
||||
add_child(weapons[index])
|
||||
_weapons.append(weapon)
|
||||
@@ -0,0 +1 @@
|
||||
uid://ruxw1n03iq4i
|
||||
@@ -0,0 +1,33 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://br074cqcnul3d"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://jvyagshykmgb" path="res://game/entities/ships/abstract_ship.tscn" id="1_6otxb"]
|
||||
[ext_resource type="Script" uid="uid://ruxw1n03iq4i" path="res://game/entities/ships/player_ship.gd" id="2_625ti"]
|
||||
[ext_resource type="Script" uid="uid://dgevigih7owxd" path="res://game/controllers/player_controller.gd" id="3_dj8f1"]
|
||||
|
||||
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_dj8f1"]
|
||||
size = Vector2(48, 32)
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_dj8f1"]
|
||||
height = 20.0
|
||||
|
||||
[node name="PlayerShip" instance=ExtResource("1_6otxb")]
|
||||
collision_layer = 3
|
||||
script = ExtResource("2_625ti")
|
||||
acceleration = 92
|
||||
deceleration = 46
|
||||
max_speed = 92
|
||||
weapon_positions = Array[Vector2]([Vector2(0, 8), Vector2(0, -8)])
|
||||
|
||||
[node name="Sprite2D" parent="." index="0"]
|
||||
texture = SubResource("PlaceholderTexture2D_dj8f1")
|
||||
|
||||
[node name="CollisionShape2D" parent="." index="1"]
|
||||
shape = SubResource("CapsuleShape2D_dj8f1")
|
||||
|
||||
[node name="PlayerController" type="Node" parent="." index="3"]
|
||||
script = ExtResource("3_dj8f1")
|
||||
metadata/_custom_type_script = "uid://dgevigih7owxd"
|
||||
|
||||
[connection signal="accelerate" from="PlayerController" to="." method="accelerate"]
|
||||
[connection signal="reload" from="PlayerController" to="." method="reload"]
|
||||
[connection signal="shoot" from="PlayerController" to="." method="shoot"]
|
||||
Reference in New Issue
Block a user