From edc8a4d16fbf1dc5f16b1f94f49e907b572a2bc9 Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Fri, 19 Mar 2021 17:21:30 +0100 Subject: [PATCH] Debug Textbox that shows current State --- src/Actor/Actor.gd | 8 +++--- src/Actor/Blobby.gd | 23 +++++++---------- src/Actor/Blobby.tscn | 18 +++++++++++-- src/Actor/PlayerStateMachine.gd | 43 +++++++++++++++++++++---------- src/StateMachines/StateMachine.gd | 32 +++++++++++------------ 5 files changed, 76 insertions(+), 48 deletions(-) diff --git a/src/Actor/Actor.gd b/src/Actor/Actor.gd index 4c217a9..c857aac 100644 --- a/src/Actor/Actor.gd +++ b/src/Actor/Actor.gd @@ -1,9 +1,9 @@ extends KinematicBody2D class_name Actor -const FLOOR_NORMAL: = Vector2.UP +const FLOOR_NORMAL := Vector2.UP -export var speed: = Vector2(300, 1000) -export var gravity: = 9800.0 +export var speed := Vector2(300, 1000) +export var gravity := 9800.0 -var _velocity: = Vector2.ZERO \ No newline at end of file +var _velocity := Vector2.ZERO diff --git a/src/Actor/Blobby.gd b/src/Actor/Blobby.gd index aa40d7a..f4569bd 100644 --- a/src/Actor/Blobby.gd +++ b/src/Actor/Blobby.gd @@ -1,6 +1,7 @@ extends Actor -export var stomp_impulse: = 1000.0 +export var stomp_impulse := 1000.0 + # TODO Move events to StateMachine 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: - var is_jump_interrupted: = Input.is_action_just_released("jump") and _velocity.y < 0.0 - var direction: = get_direction() + var is_jump_interrupted := Input.is_action_just_released("jump") and _velocity.y < 0.0 + var direction := get_direction() _velocity = calculate_move_velocity(_velocity, speed, direction, is_jump_interrupted) _velocity = move_and_slide(_velocity, FLOOR_NORMAL) func get_direction() -> 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 ) @@ -29,18 +30,15 @@ func handle_move_input(): return null -func apply_gravity(delta, velocity:Vector2): +func apply_gravity(delta, velocity: Vector2): velocity.y += gravity * delta return velocity func calculate_move_velocity( - linear_velocity: Vector2, - speed: Vector2, - direction: Vector2, - is_jump_interrupted: bool - ) -> Vector2: - var out: = linear_velocity + linear_velocity: Vector2, speed: Vector2, direction: Vector2, is_jump_interrupted: bool +) -> Vector2: + var out := linear_velocity out.x = speed.x * direction.x out = apply_gravity(get_physics_process_delta_time(), out) if direction.y == -1: @@ -51,7 +49,7 @@ func calculate_move_velocity( func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vector2: - var out: = linear_velocity + var out := linear_velocity out.y = -impulse return out @@ -59,4 +57,3 @@ func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vecto func die() -> void: queue_free() PlayerData.deaths += 1 - diff --git a/src/Actor/Blobby.tscn b/src/Actor/Blobby.tscn index faa89ab..71b1943 100644 --- a/src/Actor/Blobby.tscn +++ b/src/Actor/Blobby.tscn @@ -16,12 +16,13 @@ extents = Vector2( 30.9321, 24.5597 ) collision_mask = 8 script = ExtResource( 5 ) -[node name="player" type="Sprite" parent="."] -position = Vector2( 1.79366e-43, 5.72205e-06 ) +[node name="Player" type="Sprite" parent="."] +position = Vector2( 0, -32 ) scale = Vector2( 0.64, 0.64 ) texture = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 0, -32.2102 ) shape = SubResource( 1 ) [node name="RayCaster" type="Node2D" parent="CollisionShape2D"] @@ -49,9 +50,22 @@ collision_mask = 2 [node name="CollisionShape2D" type="CollisionShape2D" parent="EnemyDetector"] modulate = Color( 0.2, 0, 0.494118, 1 ) +position = Vector2( 0, -32.2102 ) shape = SubResource( 2 ) [node name="StateMachine" type="Node" parent="."] 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="body_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_body_entered"] diff --git a/src/Actor/PlayerStateMachine.gd b/src/Actor/PlayerStateMachine.gd index ad45f44..2aae43b 100644 --- a/src/Actor/PlayerStateMachine.gd +++ b/src/Actor/PlayerStateMachine.gd @@ -1,30 +1,47 @@ extends StateMachine + # Adds the intial states func _ready(): - add_state("idle") - add_state("run") - add_state("jump") - add_state("fall") - print_debug(states) - set_state(states.idle); + add_state("idle") + add_state("run") + add_state("jump") + add_state("fall") + print_debug(states) + set_state(states.idle) # Calls the parent behaviours according to state func _state_logic(delta): - parent.get_node("CollisionShape2D/RayCaster")._raycast(Vector2.DOWN, parent.get_node("CollisionShape2D").get_shape(), parent.collision_mask) - parent.apply_movement(delta) + # RayCasts for visual debugging + # 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 func _get_transition(delta): - - return null + parent.get_node("StateLable").text = self.state + return null func _enter_state(new_state, old_state): - pass - + pass + func _exit_state(old_state, new_state): - pass + pass diff --git a/src/StateMachines/StateMachine.gd b/src/StateMachines/StateMachine.gd index ec603bf..a98e0cb 100644 --- a/src/StateMachines/StateMachine.gd +++ b/src/StateMachines/StateMachine.gd @@ -10,33 +10,33 @@ onready var parent = get_parent() # Basic process flow for every SM func _physics_process(delta): - if state != null: - _state_logic(delta) - var transition = _get_transition(delta) - if transition != null: - set_state(transition) + if state != null: + _state_logic(delta) + var transition = _get_transition(delta) + if transition != null: + set_state(transition) # Game logic consequences of state func _state_logic(_delta): - pass + pass func _get_transition(_delta): - return null + return null func _enter_state(_new_state, _previous_state): - pass + pass func _exit_state(_previous_state, _new_state): - pass + pass func set_state(new_state): - previous_state = state - state = new_state + previous_state = state + state = new_state - if previous_state != null: - _exit_state(previous_state, new_state) - if new_state != null: - _enter_state(new_state, previous_state) + if previous_state != null: + _exit_state(previous_state, new_state) + if new_state != null: + _enter_state(new_state, previous_state) func add_state(state_name): - states[state_name] = state_name + states[state_name] = state_name