Added kamikaze states and star system border (both don't working properly)

This commit is contained in:
2ndbeam 2024-05-03 22:52:21 +03:00
commit ac263a1e65
6 changed files with 112 additions and 10 deletions

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=3 uid="uid://pev6k21vqaem"]
[gd_scene load_steps=8 format=3 uid="uid://pev6k21vqaem"]
[ext_resource type="Script" path="res://scripts/Ship/ship.gd" id="1_82bba"]
[ext_resource type="PackedScene" uid="uid://bkd4gyhlota7l" path="res://scenes/Ships/Modules/Hulls/kamikaze_hull.tscn" id="2_165nu"]
@ -6,6 +6,7 @@
[ext_resource type="PackedScene" uid="uid://bunboi5ouscw8" path="res://scenes/Ships/Modules/Shields/shield.tscn" id="4_aos8x"]
[ext_resource type="Script" path="res://scripts/State Machine/tree.gd" id="5_m8m3f"]
[ext_resource type="Script" path="res://scripts/State Machine/Kamikaze/WanderingState.gd" id="6_emcj1"]
[ext_resource type="Script" path="res://scripts/State Machine/Kamikaze/AttackState.gd" id="7_ilmkp"]
[node name="KamikazeShip" type="Node2D"]
script = ExtResource("1_82bba")
@ -32,4 +33,7 @@ script = ExtResource("6_emcj1")
[node name="UpdateDestination" type="Timer" parent="StateTree/WanderingState"]
wait_time = 10.0
[node name="AttackState" type="Node" parent="StateTree"]
script = ExtResource("7_ilmkp")
[connection signal="timeout" from="StateTree/WanderingState/UpdateDestination" to="StateTree/WanderingState" method="update_destination"]

View file

@ -13,7 +13,10 @@ height = 16384
scroll_offset = Vector2(681.667, 317.783)
stars_amount = 4000
[node name="Nebula" parent="Background" index="0" node_paths=PackedStringArray("tracked_node")]
[node name="SystemBorder" parent="Background" index="0"]
default_color = Color(1, 1, 1, 1)
[node name="Nebula" parent="Background" index="1" node_paths=PackedStringArray("tracked_node")]
tracked_node = NodePath("../../FactionPlayer/PlayerShip")
color_background = Color(0.276474, 0.0962249, 0.200656, 1)

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=3 uid="uid://bsnrcw64qr2hr"]
[gd_scene load_steps=10 format=3 uid="uid://bsnrcw64qr2hr"]
[ext_resource type="Script" path="res://scripts/star_system.gd" id="1_xx8w2"]
[ext_resource type="PackedScene" uid="uid://dpggye27ln436" path="res://scenes/Star Systems/Required Scenes/star_generator.tscn" id="2_iqrn0"]
@ -21,6 +21,23 @@ func _ready():
text += \" beta\\nclosed beta\"
"
[sub_resource type="GDScript" id="GDScript_r38d8"]
resource_name = "get_border_points"
script/source = "extends Line2D
@onready var star_system = get_parent().get_parent()
func _ready():
var halved_width = star_system.width / 2
var halved_height = star_system.height / 2
points = [
Vector2(-halved_width + width, -halved_height + width),
Vector2(halved_width - width, -halved_height + width),
Vector2(halved_width - width, halved_height - width),
Vector2(-halved_width + width, halved_height - width)
]
"
[node name="StarSystem" type="Node"]
process_mode = 3
script = ExtResource("1_xx8w2")
@ -48,6 +65,14 @@ script = SubResource("GDScript_4yoh5")
process_mode = 1
layer = -10
[node name="SystemBorder" type="Line2D" parent="Background"]
z_index = -99
points = PackedVector2Array(0, 0, 1280, 0, 1280, 720, 0, 720)
closed = true
width = 8.0
default_color = Color(1, 1, 1, 0.521569)
script = SubResource("GDScript_r38d8")
[node name="Nebula" type="ColorRect" parent="Background"]
z_index = -100
material = ExtResource("4_o0ld7")

View file

@ -0,0 +1,50 @@
extends State
## This amount of force will be applied if target can be stabbed
@export var additional_stab_force: float = 200.0
## If target is further than this distance, transit to WanderingState
@export var target_loss_distance: float = 1536.0
## Shortcut to get_parent().get_parent()
@onready var ship = get_parent().get_parent()
## Node which is being targeted by this ship
var target: Node2D
## Ship will strive to achieve this angle
var destination_degrees: float = 0:
set(value):
if value > 180:
destination_degrees = value - 360
elif value < -180:
destination_degrees = 360 + value
else:
destination_degrees = value
## Difference between current rotation and destination angle, in degrees. Sign determines torque direction.
var current_destination_difference: float = 0:
set(value):
if value < 0:
current_destination_difference = 180 + value
if current_destination_difference > 180:
current_destination_difference = value - 180
## Delta to destination_degrees
var destination_difference: float = 5.0
func enter(message: Dictionary):
if message.has("target"):
target = message["target"]
else:
get_parent().transit("WanderingState")
func process(_delta):
# checking if need to apply torque
destination_degrees = rad_to_deg(ship.global_position.angle_to_point(target.global_position))
current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
var rotation_sign = sign(current_destination_difference)
if abs(current_destination_difference) > destination_difference:
ship.engine.rotation_axis = rotation_sign * (current_destination_difference / 180)
else:
ship.engine.rotation_axis = 0.0
ship.hull.apply_central_force(Vector2.from_angle(ship.hull.rotation) * additional_stab_force)
# making ship always accelerate
ship.engine.acceleration_axis = 1.0
var distance_to_target = ship.global_position.distance_to(target.global_position)
if distance_to_target > target_loss_distance:
get_parent().transit("WanderingState")

View file

@ -1,9 +1,13 @@
extends State
## Shortcut to get_parent().get_parent()
@onready var ship = get_parent().get_parent()
## Shortcut to current scene
@onready var star_system = get_tree().current_scene
## Timer which randomizes destination
@onready var update_timer = $UpdateDestination
## Radius in which ship will transit to attack state
@export var search_distance: float = 1024.0
## Ship will strive to achieve this angle
var destination_degrees: float = 0:
set(value):
@ -13,10 +17,19 @@ var destination_degrees: float = 0:
destination_degrees = 360 + value
else:
destination_degrees = value
## Difference between current rotation and destination angle, in degrees. Sign determines torque direction.
var current_destination_difference: float = 0:
set(value):
if value < 0:
current_destination_difference = 180 + value
if current_destination_difference > 180:
current_destination_difference = value - 180
## Delta to destination_degrees
var destination_difference: float = 15.0
## available map bounds (use with absolute position)
var available_bounds: Vector2
# for testing purposes only
var player_ship: PlayerShip = null
func _ready():
available_bounds = Vector2(star_system.width / 2, star_system.height / 2)
@ -24,16 +37,13 @@ func _ready():
func enter(_message):
update_timer.start()
get_tree().create_timer(0.02).timeout.connect(get_player_ship)
update_destination()
func process(_delta):
# checking if need to apply torque
var current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
current_destination_difference = destination_degrees - ship.hull.global_rotation_degrees
var rotation_sign = sign(current_destination_difference)
if current_destination_difference < 0:
current_destination_difference = 180 - current_destination_difference
if current_destination_difference > 180:
current_destination_difference -= 180
if abs(current_destination_difference) > destination_difference:
ship.engine.rotation_axis = rotation_sign * (current_destination_difference / 180)
else:
@ -43,6 +53,13 @@ func process(_delta):
# if ship is out of star system bounds, set destination angle to center
if abs(ship.global_position.x) > available_bounds.x or abs(ship.global_position.y) > available_bounds.y:
destination_degrees = rad_to_deg(ship.global_position.angle_to_point(Vector2.ZERO))
# if distance to player is less than scan radius
# TODO: rewrite this to more complex behavior
if player_ship != null:
var distance_to_player = ship.global_position.distance_to(player_ship.position)
if distance_to_player <= search_distance:
get_parent().transit("AttackState", {"target": player_ship})
func exit():
update_timer.stop()
@ -50,3 +67,6 @@ func exit():
## Set new random direction
func update_destination():
destination_degrees += randi_range(-180, 180)
func get_player_ship():
player_ship = star_system.player_ship

View file

@ -16,7 +16,7 @@ var player_ship: PlayerShip
var pause_controller: Control
func _ready():
player_ship = get_node_or_null("PlayerShip")
player_ship = get_node_or_null("FactionPlayer/PlayerShip")
pause_controller = pause_controller_scene.instantiate()