Rewriting ships: Movement system

This commit is contained in:
2ndbeam 2024-04-28 18:53:09 +03:00
commit e2b9fa6c69
10 changed files with 208 additions and 0 deletions

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://mw4kwxoeqch3"]
[ext_resource type="Script" path="res://scripts/Ship/engine.gd" id="1_e4bt7"]
[node name="Engine" type="Node2D"]
script = ExtResource("1_e4bt7")

View file

@ -0,0 +1,16 @@
[gd_scene load_steps=3 format=3 uid="uid://bsu4eqwdfewwi"]
[ext_resource type="Script" path="res://scripts/Ship/hull.gd" id="1_7nfg8"]
[ext_resource type="Texture2D" uid="uid://beb76cssb3knp" path="res://sprites/ship.png" id="2_vhl4r"]
[node name="Hull" type="RigidBody2D"]
position = Vector2(0.5, 0)
gravity_scale = 0.0
angular_damp = 1.0
script = ExtResource("1_7nfg8")
[node name="Sprite" type="Sprite2D" parent="."]
texture = ExtResource("2_vhl4r")
[node name="Collision" type="CollisionPolygon2D" parent="."]
polygon = PackedVector2Array(-45.5, 14, -45.5, 64, -44.5, 64, 81.5, 1, 81.5, -1, -44.5, -64, -45.5, -64, -45.5, -14, -67.5, -14, -81.5, -28, -82.5, -28, -82.5, 28, -81.5, 28, -67.5, 14)

View file

@ -0,0 +1,21 @@
[gd_scene load_steps=5 format=3 uid="uid://dok3i8u5t1ka4"]
[ext_resource type="PackedScene" uid="uid://bjkshql8ut6hk" path="res://scenes/Ships/ship.tscn" id="1_6x7bu"]
[ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_dbojl"]
[ext_resource type="Script" path="res://scripts/Ship/player_input_controller.gd" id="3_0e84a"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_u1mh3"]
resource_local_to_scene = true
resource_name = "Shield Material"
shader = ExtResource("2_dbojl")
shader_parameter/wave_centers = PackedVector2Array()
shader_parameter/wave_distances = PackedFloat32Array(-1)
shader_parameter/color = Color(1, 0, 0, 1)
shader_parameter/max_distance = 20.0
shader_parameter/bublic_size = 20.0
[node name="PlayerShip" instance=ExtResource("1_6x7bu")]
material = SubResource("ShaderMaterial_u1mh3")
[node name="InputController" type="Node2D" parent="." index="2"]
script = ExtResource("3_0e84a")

14
scenes/Ships/ship.tscn Normal file
View file

@ -0,0 +1,14 @@
[gd_scene load_steps=5 format=3 uid="uid://bjkshql8ut6hk"]
[ext_resource type="Material" uid="uid://cw0827vwv7pc1" path="res://shaders/materials/ship_material.tres" id="1_ibfis"]
[ext_resource type="Script" path="res://scripts/Ship/ship.gd" id="2_n14ml"]
[ext_resource type="PackedScene" uid="uid://bsu4eqwdfewwi" path="res://scenes/Ships/Modules/Hulls/hull.tscn" id="3_upgeg"]
[ext_resource type="PackedScene" uid="uid://mw4kwxoeqch3" path="res://scenes/Ships/Modules/Engines/engine.tscn" id="4_adr14"]
[node name="Ship" type="Node2D"]
material = ExtResource("1_ibfis")
script = ExtResource("2_n14ml")
[node name="Hull" parent="." instance=ExtResource("3_upgeg")]
[node name="Engine" parent="." instance=ExtResource("4_adr14")]

26
scripts/Ship/engine.gd Normal file
View file

@ -0,0 +1,26 @@
extends Node2D
class_name ShipEngine
# TODO: implement dashes
# TODO: make particles and turbo
## Engine ID to use with Game.get_module
@export var id: String = "engine"
## Acceleration speed of the engine
@export var acceleration_speed: float = 50.0
## Rotation speed of the engine
@export var rotation_speed: float = 5000.0
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
## Acceleration control variable
var acceleration_axis: float = 0.0
## Rotation control variable
var rotation_axis: float = 0.0
func _physics_process(_delta):
# apply movement and rotation
ship.hull.apply_central_force(Vector2.from_angle(ship.hull.rotation) * acceleration_speed * acceleration_axis)
ship.hull.apply_torque(rotation_speed * rotation_axis)

27
scripts/Ship/hull.gd Normal file
View file

@ -0,0 +1,27 @@
extends RigidBody2D
class_name Hull
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
## Hull ID to use with the Game.get_module func
@export var id: String = "hull"
## Max HP of the hull
@export var max_hp: float = 30.0
## Maximum amount of ammunition to be carried
@export var max_ammunition: Dictionary = {
"n/a": 0, # don't change this k thx
"Laser Energy": 100,
"Rockets": 10,
}
## Current HP of the hull. If it reaches zero, it emits parent's destroyed signal
var hp: float = max_hp:
set(new_hp):
if new_hp > 0:
hp = new_hp
else:
ship.destroyed.emit()

View file

@ -0,0 +1,8 @@
extends Node2D
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
func _physics_process(_delta):
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright")

56
scripts/Ship/ship.gd Normal file
View file

@ -0,0 +1,56 @@
extends Node2D
class_name Ship
# TODO: add weapons
## Emits when hull hp reaches zero.
signal destroyed
## Reference to the engine module
@onready var engine: ShipEngine = $Engine
## Reference to the hull module
@onready var hull: Hull = $Hull
## Reference to the shield module
@onready var shield: Shield = $Shield
## Reference to primary weapon
#@onready var primary_weapon: Weapon = $PrimaryWeapon
## Reference to secondary weapon
#@onready var secondary_weapon: Weapon = $SecondaryWeapon
## Faction which this ship belongs to
var faction : Game.Faction = Game.Faction.Player
func _ready():
#shield.material = material
destroyed.connect(destroy)
## Reset all required variables
func destroy():
hull.hp = hull.max_hp
shield.capacity = shield.max_capacity
## Swaps old hull with the new one
func change_hull(new_hull_id: String):
var new_hull: Hull = Game.get_module(new_hull_id, 'hull').instantiate()
var old_hull: Hull = hull
add_child(new_hull)
hull = new_hull
hull.hp = old_hull.hp
get_tree().create_timer(0.05).timeout.connect(old_hull.queue_free)
## Swaps old engine with the new one
func change_engine(new_engine_id: String):
var new_engine: ShipEngine = Game.get_module(new_engine_id, 'engine').instantiate()
var old_engine: ShipEngine = engine
add_child(new_engine)
engine = new_engine
get_tree().create_timer(0.05).timeout.connect(old_engine.queue_free)
## Swaps old shield with the new one
func change_shield(new_shield_id: String):
var new_shield: Shield = Game.get_module(new_shield_id, 'shield').instantiate()
var old_shield: Shield = shield
add_child(new_shield)
shield = new_shield
get_tree().create_timer(0.05).timeout.connect(old_shield.queue_free)

BIN
sprites/ship.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

34
sprites/ship.png.import Normal file
View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://beb76cssb3knp"
path="res://.godot/imported/ship.png-4d5e64b785005468aa74f7ca14df1341.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://sprites/ship.png"
dest_files=["res://.godot/imported/ship.png-4d5e64b785005468aa74f7ca14df1341.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1