From a7bc6c63491b6e234874ebbe744321cc57c0368e Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Mon, 27 May 2024 16:51:38 -0700 Subject: [PATCH] Switch to transformed based flip on kinimatic body instead of sprite based one so gun and positions offset appropriately. I wish I knew this when I started. More improvements can be done here so I don't have to update the code in every state. --- Main.tscn | 2 +- project.godot | 2 -- src/playerC/Player-KinematicBody2D.gd | 28 ++++++++++++++++----------- src/playerC/states/fall.gd | 8 +++++--- src/playerC/states/idle.gd | 7 +++++++ src/playerC/states/jump.gd | 8 +++++--- src/playerC/states/move.gd | 21 +++++++++++++++++--- 7 files changed, 53 insertions(+), 23 deletions(-) diff --git a/Main.tscn b/Main.tscn index 8d296ea..d22d2a9 100644 --- a/Main.tscn +++ b/Main.tscn @@ -144,7 +144,7 @@ __meta__ = { } [node name="Player" parent="." instance=ExtResource( 2 )] -position = Vector2( 81, 27 ) +position = Vector2( 136, 35 ) [node name="CameraControl" type="Position2D" parent="."] process_priority = 1 diff --git a/project.godot b/project.godot index 12c613a..a5629f3 100644 --- a/project.godot +++ b/project.godot @@ -92,14 +92,12 @@ ui_accept={ move_left_1={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"physical_scancode":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) ] } move_right_1={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null) ] } diff --git a/src/playerC/Player-KinematicBody2D.gd b/src/playerC/Player-KinematicBody2D.gd index f5403a4..b4a9030 100644 --- a/src/playerC/Player-KinematicBody2D.gd +++ b/src/playerC/Player-KinematicBody2D.gd @@ -1,7 +1,7 @@ extends KinematicBody2D var velocity = Vector2(0,0) -var direction = Vector2(0,0) +var direction = Vector2(-1,0) export var player_number: int = 1 @@ -29,16 +29,22 @@ func _physics_process(delta: float) -> void: # #TODO: So much hack # if direction.x != 0: # gun.set_scale( Vector2(direction.x,1)) - if direction.x < 0: - var t = Transform2D() - # Translation - t.origin = Vector2(gun.offset_position.x * -1, gun.offset_position.y) - gun.transform = t - elif direction.x > 0: - var t = Transform2D() - # Translation - t.origin = Vector2(gun.offset_position.x, gun.offset_position.y) - gun.transform = t + + # Commented out to try parent transforms instead +# if direction.x < 0: +# var t = Transform2D() +# # Translation +# t.origin = Vector2(gun.offset_position.x * -1, gun.offset_position.y) +# gun.transform = t +# elif direction.x > 0: +# var t = Transform2D() +# # Translation +# t.origin = Vector2(gun.offset_position.x, gun.offset_position.y) +# gun.transform = t + + # Attempting a Kinematic2D level scale for flip operations instead. + #self.transform + #self.scale.x = -1 func _process(delta: float) -> void: movement_state_machine.process_frame(delta) diff --git a/src/playerC/states/fall.gd b/src/playerC/states/fall.gd index 77dc706..31d31e3 100644 --- a/src/playerC/states/fall.gd +++ b/src/playerC/states/fall.gd @@ -30,10 +30,12 @@ func process_physics(delta: float) -> State: parent.direction.x = -1 - if move_component.flipped_sprite == true: - animations.flip_h = parent.direction.x > 0 + if move_component.flipped_sprite == false: + #parent.scale.x = parent.direction.x * -1 + parent.transform.x.x = parent.direction.x * -1 # -1 else: - animations.flip_h = parent.direction.x < 0 + #parent.scale.x = parent.direction.x + parent.transform.x.x = parent.direction.x # 1 if parent.is_on_floor(): if parent.velocity.x == 0: diff --git a/src/playerC/states/idle.gd b/src/playerC/states/idle.gd index 6703fba..f05c0f5 100644 --- a/src/playerC/states/idle.gd +++ b/src/playerC/states/idle.gd @@ -1,5 +1,7 @@ extends State +var debugTimeTracker := 0.0 + export (NodePath) var jump_node export (NodePath) var fall_node export (NodePath) var move_node @@ -35,6 +37,11 @@ func process_input(_event: InputEvent) -> State: return null func process_physics(delta: float) -> State: + debugTimeTracker += delta + if debugTimeTracker > 2: + debugTimeTracker = 0.0 + print("DEBUG TRANSFORM:", parent.transform) + parent.velocity.y += gravity * delta #parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) parent.velocity.x = move_component.desired_movement_vector.x * move_speed diff --git a/src/playerC/states/jump.gd b/src/playerC/states/jump.gd index 92e170d..e09f4cb 100644 --- a/src/playerC/states/jump.gd +++ b/src/playerC/states/jump.gd @@ -34,10 +34,12 @@ func process_physics(delta: float) -> State: elif parent.velocity.x < 0: parent.direction.x = -1 - if move_component.flipped_sprite == true: - animations.flip_h = parent.direction.x > 0 + if move_component.flipped_sprite == false: + #parent.scale.x = parent.direction.x * -1 + parent.transform.x.x = parent.direction.x * -1 # -1 else: - animations.flip_h = parent.direction.x < 0 + #parent.scale.x = parent.direction.x + parent.transform.x.x = parent.direction.x # 1 if parent.is_on_floor(): if parent.velocity.x == 0: diff --git a/src/playerC/states/move.gd b/src/playerC/states/move.gd index 4d313d1..46b3662 100644 --- a/src/playerC/states/move.gd +++ b/src/playerC/states/move.gd @@ -1,5 +1,7 @@ extends State +var debugTimeTracker := 0.0 + export (NodePath) var jump_node export (NodePath) var fall_node export (NodePath) var idle_node @@ -22,6 +24,10 @@ func process_input(_event: InputEvent) -> State: func process_physics(delta: float) -> State: # if move_component.wants_jump() and parent.is_on_floor(): # return jump_state + debugTimeTracker += delta + if debugTimeTracker > 2: + debugTimeTracker = 0.0 + print("DEBUG TRANSFORM:", parent.transform) parent.velocity.y += gravity * delta @@ -46,10 +52,19 @@ func process_physics(delta: float) -> State: parent.direction.x = -1 - if move_component.flipped_sprite == true: - animations.flip_h = parent.direction.x > 0 +# if move_component.flipped_sprite == true: +# animations.flip_h = parent.direction.x > 0 +# else: +# animations.flip_h = parent.direction.x < 0 + # Attempting to flip based on scale instead of sprite flip so that + # all children nodes get flipped as well: + if move_component.flipped_sprite == false: + #parent.scale.x = parent.direction.x * -1 + parent.transform.x.x = parent.direction.x * -1 # -1 else: - animations.flip_h = parent.direction.x < 0 + #parent.scale.x = parent.direction.x + parent.transform.x.x = parent.direction.x # 1 + # Another approach of changing state based on actual achieved velocity