Debug Textbox that shows current State

This commit is contained in:
Jakob Feldmann 2021-03-19 17:21:30 +01:00
parent 67f2466f8c
commit edc8a4d16f
5 changed files with 76 additions and 48 deletions

View File

@ -1,9 +1,9 @@
extends KinematicBody2D extends KinematicBody2D
class_name Actor class_name Actor
const FLOOR_NORMAL: = Vector2.UP const FLOOR_NORMAL := Vector2.UP
export var speed: = Vector2(300, 1000) export var speed := Vector2(300, 1000)
export var gravity: = 9800.0 export var gravity := 9800.0
var _velocity: = Vector2.ZERO var _velocity := Vector2.ZERO

View File

@ -1,6 +1,7 @@
extends Actor extends Actor
export var stomp_impulse: = 1000.0 export var stomp_impulse := 1000.0
# TODO Move events to StateMachine # TODO Move events to StateMachine
func _on_EnemyDetector_area_entered(area: Area2D) -> void: func _on_EnemyDetector_area_entered(area: Area2D) -> void:
@ -12,15 +13,15 @@ func _on_EnemyDetector_body_entered(body: Node) -> void:
func apply_movement(delta: float) -> void: func apply_movement(delta: float) -> void:
var is_jump_interrupted: = Input.is_action_just_released("jump") and _velocity.y < 0.0 var is_jump_interrupted := Input.is_action_just_released("jump") and _velocity.y < 0.0
var direction: = get_direction() var direction := get_direction()
_velocity = calculate_move_velocity(_velocity, speed, direction, is_jump_interrupted) _velocity = calculate_move_velocity(_velocity, speed, direction, is_jump_interrupted)
_velocity = move_and_slide(_velocity, FLOOR_NORMAL) _velocity = move_and_slide(_velocity, FLOOR_NORMAL)
func get_direction() -> Vector2: func get_direction() -> Vector2:
return Vector2( return Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"), Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
-1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 1.0 -1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 1.0
) )
@ -29,18 +30,15 @@ func handle_move_input():
return null return null
func apply_gravity(delta, velocity:Vector2): func apply_gravity(delta, velocity: Vector2):
velocity.y += gravity * delta velocity.y += gravity * delta
return velocity return velocity
func calculate_move_velocity( func calculate_move_velocity(
linear_velocity: Vector2, linear_velocity: Vector2, speed: Vector2, direction: Vector2, is_jump_interrupted: bool
speed: Vector2, ) -> Vector2:
direction: Vector2, var out := linear_velocity
is_jump_interrupted: bool
) -> Vector2:
var out: = linear_velocity
out.x = speed.x * direction.x out.x = speed.x * direction.x
out = apply_gravity(get_physics_process_delta_time(), out) out = apply_gravity(get_physics_process_delta_time(), out)
if direction.y == -1: if direction.y == -1:
@ -51,7 +49,7 @@ func calculate_move_velocity(
func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vector2: func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vector2:
var out: = linear_velocity var out := linear_velocity
out.y = -impulse out.y = -impulse
return out return out
@ -59,4 +57,3 @@ func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vecto
func die() -> void: func die() -> void:
queue_free() queue_free()
PlayerData.deaths += 1 PlayerData.deaths += 1

View File

@ -16,12 +16,13 @@ extents = Vector2( 30.9321, 24.5597 )
collision_mask = 8 collision_mask = 8
script = ExtResource( 5 ) script = ExtResource( 5 )
[node name="player" type="Sprite" parent="."] [node name="Player" type="Sprite" parent="."]
position = Vector2( 1.79366e-43, 5.72205e-06 ) position = Vector2( 0, -32 )
scale = Vector2( 0.64, 0.64 ) scale = Vector2( 0.64, 0.64 )
texture = ExtResource( 1 ) texture = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, -32.2102 )
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="RayCaster" type="Node2D" parent="CollisionShape2D"] [node name="RayCaster" type="Node2D" parent="CollisionShape2D"]
@ -49,9 +50,22 @@ collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="EnemyDetector"] [node name="CollisionShape2D" type="CollisionShape2D" parent="EnemyDetector"]
modulate = Color( 0.2, 0, 0.494118, 1 ) modulate = Color( 0.2, 0, 0.494118, 1 )
position = Vector2( 0, -32.2102 )
shape = SubResource( 2 ) shape = SubResource( 2 )
[node name="StateMachine" type="Node" parent="."] [node name="StateMachine" type="Node" parent="."]
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="StateLable" type="Label" parent="."]
margin_left = -31.0
margin_top = -80.0
margin_right = 31.0
margin_bottom = -64.0
text = "Coochie"
align = 1
valign = 1
__meta__ = {
"_edit_use_anchors_": false
}
[connection signal="area_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_area_entered"] [connection signal="area_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_area_entered"]
[connection signal="body_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_body_entered"] [connection signal="body_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_body_entered"]

View File

@ -1,30 +1,47 @@
extends StateMachine extends StateMachine
# Adds the intial states # Adds the intial states
func _ready(): func _ready():
add_state("idle") add_state("idle")
add_state("run") add_state("run")
add_state("jump") add_state("jump")
add_state("fall") add_state("fall")
print_debug(states) print_debug(states)
set_state(states.idle); set_state(states.idle)
# Calls the parent behaviours according to state # Calls the parent behaviours according to state
func _state_logic(delta): func _state_logic(delta):
parent.get_node("CollisionShape2D/RayCaster")._raycast(Vector2.DOWN, parent.get_node("CollisionShape2D").get_shape(), parent.collision_mask) # RayCasts for visual debugging
parent.apply_movement(delta) # TODO Global context switch for debug/build mode
# \ is for new line in multiline statements
parent.get_node("CollisionShape2D/RayCaster")._raycast(
Vector2.DOWN,
parent.get_node("CollisionShape2D").get_shape(),
parent.collision_mask
)
match self.state:
"idle":
parent.apply_movement(delta)
"run":
parent.apply_movement(delta)
"jump":
parent.apply_movement(delta)
"fall":
parent.apply_movement(delta)
# Determines which state should be active at the moment # Determines which state should be active at the moment
func _get_transition(delta): func _get_transition(delta):
parent.get_node("StateLable").text = self.state
return null return null
func _enter_state(new_state, old_state): func _enter_state(new_state, old_state):
pass pass
func _exit_state(old_state, new_state): func _exit_state(old_state, new_state):
pass pass

View File

@ -10,33 +10,33 @@ onready var parent = get_parent()
# Basic process flow for every SM # Basic process flow for every SM
func _physics_process(delta): func _physics_process(delta):
if state != null: if state != null:
_state_logic(delta) _state_logic(delta)
var transition = _get_transition(delta) var transition = _get_transition(delta)
if transition != null: if transition != null:
set_state(transition) set_state(transition)
# Game logic consequences of state # Game logic consequences of state
func _state_logic(_delta): func _state_logic(_delta):
pass pass
func _get_transition(_delta): func _get_transition(_delta):
return null return null
func _enter_state(_new_state, _previous_state): func _enter_state(_new_state, _previous_state):
pass pass
func _exit_state(_previous_state, _new_state): func _exit_state(_previous_state, _new_state):
pass pass
func set_state(new_state): func set_state(new_state):
previous_state = state previous_state = state
state = new_state state = new_state
if previous_state != null: if previous_state != null:
_exit_state(previous_state, new_state) _exit_state(previous_state, new_state)
if new_state != null: if new_state != null:
_enter_state(new_state, previous_state) _enter_state(new_state, previous_state)
func add_state(state_name): func add_state(state_name):
states[state_name] = state_name states[state_name] = state_name