Turret state machine stub and default state

This commit is contained in:
Jakob Feldmann 2022-06-04 21:14:49 +02:00
parent 71cf76e9ef
commit c3ed976783
12 changed files with 97 additions and 18 deletions

View File

@ -4,7 +4,7 @@ export var jump_buffer_filled := false
onready var wall_touch_direction = 0
onready var left_wall_raycasts = $WallRaycasts/LeftWallRaycast
onready var right_wall_raycasts = $WallRaycasts/RightWallRaycast
onready var player_state_machine = $PlayerStateMachine
onready var player_state_machine = $BlobbyStateMachine
onready var init_boost = player_state_machine.init_boost
onready var init_boost_type = player_state_machine.init_boost_type
onready var camera_tween = $Camera2D/ShiftTween
@ -162,7 +162,7 @@ func calculate_deceleration_force(_gravity: float, mass: float) -> float:
func calculate_jump_velocity(
linear_velocity: Vector2, delta: float, direction: Vector2
) -> Vector2:
var state = self.get_node("PlayerStateMachine").state
var state = player_state_machine.state
var walljumping = is_correct_walljump_input(direction)
var additive_jump_force = velocity_jump_boost_ratio * abs(velocity.x) * mass

View File

@ -2,7 +2,7 @@
[ext_resource path="res://assets/blobby/blobby1.png" type="Texture" id=1]
[ext_resource path="res://src/Actors/Blobby/Camera2D.gd" type="Script" id=2]
[ext_resource path="res://src/Actors/PlayerStateMachine.gd" type="Script" id=3]
[ext_resource path="res://src/Actors/Blobby/BlobbyStateMachine.gd" type="Script" id=3]
[ext_resource path="res://src/Actors/Blobby/Blobby.gd" type="Script" id=4]
[ext_resource path="res://assets/meta/new_dynamicfont.tres" type="DynamicFont" id=5]
@ -46,14 +46,14 @@ collision_mask = 126
scale = Vector2( 1.04, 1.04 )
polygon = PoolVector2Array( -6.7, -3.311, -2.676, -7.3, 3.939, -7.3, 8, -1.863, 8, 4.912, 4.944, 8.5, -1.03623, 8.5, -4.213, 8.5, -6.7, 6.089 )
[node name="PlayerStateMachine" type="Node" parent="."]
[node name="BlobbyStateMachine" type="Node" parent="."]
script = ExtResource( 3 )
[node name="JumpBufferTimer" type="Timer" parent="PlayerStateMachine"]
[node name="JumpBufferTimer" type="Timer" parent="BlobbyStateMachine"]
wait_time = 0.067
one_shot = true
[node name="CoyoteTimer" type="Timer" parent="PlayerStateMachine"]
[node name="CoyoteTimer" type="Timer" parent="BlobbyStateMachine"]
wait_time = 0.067
one_shot = true
@ -101,5 +101,5 @@ collision_mask = 9
[connection signal="area_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_area_entered"]
[connection signal="body_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_body_entered"]
[connection signal="got_grounded" from="PlayerStateMachine" to="." method="_on_Blobby_got_grounded"]
[connection signal="timeout" from="PlayerStateMachine/JumpBufferTimer" to="." method="_on_JumpBufferTimer_timeout"]
[connection signal="got_grounded" from="BlobbyStateMachine" to="." method="_on_Blobby_got_grounded"]
[connection signal="timeout" from="BlobbyStateMachine/JumpBufferTimer" to="." method="_on_JumpBufferTimer_timeout"]

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://assets/enemy/enemy.png" type="Texture" id=1]
[ext_resource path="res://src/Actors/Enemy/Enemy.gd" type="Script" id=2]
[ext_resource path="res://src/Actors/Enemies/Beings/Enemy.gd" type="Script" id=2]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 2.72463, 1.17848 )

View File

@ -4,6 +4,7 @@ extends KinematicBody2D
# var a: int = 2
# var b: String = "text"
onready var raycast: RayCast2D = $RayCast2D
onready var turret_state_machine = $TurretStateMachine
# Called when the node enters the scene tree for the first time.
@ -11,8 +12,7 @@ func _ready() -> void:
$AnimationPlayer.play("Turret Rotation")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(_delta: float) -> void:
func searching():
if raycast.is_colliding():
# The collider returns not the area or body it hit, but the parent of them
if raycast.get_collider().is_in_group("player"):

View File

@ -1,7 +1,9 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=7 format=2]
[ext_resource path="res://assets/contraption/bumper.png" type="Texture" id=1]
[ext_resource path="res://src/Actors/Enemy/Turret.gd" type="Script" id=2]
[ext_resource path="res://assets/meta/new_dynamicfont.tres" type="DynamicFont" id=2]
[ext_resource path="res://src/Actors/Enemies/Machines/Turret.gd" type="Script" id=3]
[ext_resource path="res://src/Actors/Enemies/Machines/TurretStateMachine.gd" type="Script" id=4]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 108.869, 37.2448 )
@ -26,7 +28,7 @@ tracks/0/keys = {
[node name="Turret" type="KinematicBody2D"]
collision_layer = 2
collision_mask = 3
script = ExtResource( 2 )
script = ExtResource( 3 )
[node name="Sprite" type="Sprite" parent="."]
scale = Vector2( 0.612294, 1 )
@ -44,3 +46,17 @@ cast_to = Vector2( 0, 1e+07 )
pause_mode = 2
playback_process_mode = 0
"anims/Turret Rotation" = SubResource( 2 )
[node name="TurretStateMachine" type="Node" parent="."]
script = ExtResource( 4 )
[node name="StateLabel" type="Label" parent="."]
margin_left = -35.7329
margin_top = -52.7099
margin_right = 37.2671
margin_bottom = -38.7099
custom_colors/font_color = Color( 0, 0, 0, 1 )
custom_fonts/font = ExtResource( 2 )
text = "Ihre Werbung"
align = 1
valign = 1

View File

@ -0,0 +1,61 @@
extends StateMachine
signal got_grounded
# Adds the intial states
func _ready():
add_state("deactivated")
add_state("searching")
add_state("hunting")
add_state("locking")
add_state("shooting")
state = states.searching
set_state(states.searching)
# Calls the parent behaviours according to state
func _state_logic(delta):
var state_action_ref = "handle_deactivated_state"
match self.state:
"deactivated":
state_action_ref = funcref(self, "handle_deactivated_state")
"searching":
state_action_ref = funcref(self, "handle_searching_state")
"locking":
state_action_ref = funcref(self, "handle_locking_state")
"shooting":
state_action_ref = funcref(self, "handle_shooting_state")
_:
state_action_ref = funcref(self, "handle_deactivated_state")
print("don't panik")
state_action_ref.call_func()
func handle_searching_state():
parent.searching()
# Determines which state should be active at the moment
func _get_transition(_delta):
print_debug(self.state)
# TODO why only this way?
parent.get_node("StateLabel").text = self.state
var new_state
if new_state != self.state:
return new_state
return null
func _enter_state(new_state, old_state):
if old_state == "idle" && (new_state == "walk" || new_state == "run"):
pass
# TODO This may be hard to keep track of if many states are added
if !["run", "walk", "idle"].has(old_state) && parent.is_on_floor():
emit_signal("got_grounded")
func _exit_state(old_state, new_state):
pass

View File

@ -18,6 +18,8 @@ func _ready() -> void:
# TODO extensively playtest to find problems
# TODO Shock doesn't get triggered when doing some smaller jumps
# TODO There is a limit to how high you can go(can't repeatedly increase hight)
# Called every frame. 'delta' is the elapsed time since the previous frame.

View File

@ -2,7 +2,7 @@
[ext_resource path="res://src/Actors/Blobby/Blobby.tscn" type="PackedScene" id=1]
[ext_resource path="res://assets/meta/tileset.tres" type="TileSet" id=2]
[ext_resource path="res://src/Actors/Enemy/Enemy.tscn" type="PackedScene" id=3]
[ext_resource path="res://src/Actors/Enemies/Beings/Enemy.tscn" type="PackedScene" id=3]
[ext_resource path="res://assets/environment/background/background.png" type="Texture" id=4]
[ext_resource path="res://src/NeutralObjects/Coin.tscn" type="PackedScene" id=5]
[ext_resource path="res://src/UserInterface/Buttons/UserInterface.tscn" type="PackedScene" id=6]

View File

@ -7,7 +7,7 @@
[ext_resource path="res://src/Contraptions/Platform/Track.tscn" type="PackedScene" id=5]
[ext_resource path="res://src/HarmfulObjects/Bullet.tscn" type="PackedScene" id=6]
[ext_resource path="res://src/UserInterface/Buttons/UI.tscn" type="PackedScene" id=7]
[ext_resource path="res://src/Actors/Enemy/Turret.tscn" type="PackedScene" id=8]
[ext_resource path="res://src/Actors/Enemies/Machines/Turret.tscn" type="PackedScene" id=8]
[sub_resource type="NavigationPolygon" id=1]
vertices = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 )
@ -69,11 +69,11 @@ tile_data = PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0,
position = Vector2( 50.7867, 604.063 )
[node name="Spring4" parent="." instance=ExtResource( 3 )]
position = Vector2( 331.785, 601.665 )
position = Vector2( 170, 600.198 )
scale = Vector2( 1.88002, 1 )
[node name="Track" parent="." instance=ExtResource( 5 )]
position = Vector2( 422.501, 601.665 )
position = Vector2( 291.104, 535.161 )
scale = Vector2( 2.83999, 0.56 )
[node name="KinematicBody2D" parent="Track" index="0"]