Refactored a bit. Punching improved, for example
This commit is contained in:
1 parent
1e24e0a59a
commit
f1d7c8968a
8 files changed
+138
-65
No files matched your search
+59
-41
@@ -1,24 +1,36 @@
|
||||
class_name Player
|
||||
extends CharacterBody2D
|
||||
## Character controller.
|
||||
## TODO: fix bullet cooldown timer
|
||||
##
|
||||
## Controls the player character in this 2D side-scrolling platformer
|
||||
|
||||
enum FaceDirection {
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
||||
enum State {
|
||||
IDLE,
|
||||
RUN,
|
||||
JUMP,
|
||||
FALL,
|
||||
SHOOT_STILL,
|
||||
SHOOT_RUN,
|
||||
PUNCH
|
||||
}
|
||||
|
||||
const SPEED = 200.0
|
||||
const JUMP_VELOCITY = -500.0
|
||||
const SPEED = 200.0
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
enum State{IDLE, WALK, JUMP, FALLING, SHOOT_STILL, SHOOT_RUN, SHOVE}
|
||||
|
||||
@export var BUMP_POWER := 50
|
||||
@export var SHOVE_POWER := 300
|
||||
@export var BUMP_POWER := 50 ## Power from bumping into objects
|
||||
@export var PUNCH_POWER := 300 ## Power from shoving objects
|
||||
@export var ACCELERATION := 10 ## Amount of "slide-y-ness" in side-to-side-movement
|
||||
@export var HARD_GRAVITY := 2
|
||||
@export var HARD_GRAVITY := 2 ## Factor applied during FALL state
|
||||
|
||||
var current_state: State = State.IDLE
|
||||
var facing : FaceDirection = FaceDirection.RIGHT
|
||||
var direction : float = 0.0
|
||||
var push_target : RigidBody2D
|
||||
var push_enabled : bool = false
|
||||
var punch_target : RigidBody2D
|
||||
var punch_enabled : bool = false
|
||||
var jump_buffer_timer : Timer
|
||||
var shoot_cooldown_timer : Timer
|
||||
|
||||
@@ -62,18 +74,19 @@ func handle_input() -> void:
|
||||
facing = FaceDirection.RIGHT
|
||||
|
||||
# Handle shoving.
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
print_debug("Shoving!")
|
||||
if push_enabled == true:
|
||||
var shove_direction : int
|
||||
if Input.is_action_just_pressed("punch"):
|
||||
print_debug("Trying to punch")
|
||||
if punch_enabled == true:
|
||||
print_debug("Punching enabled")
|
||||
var punch_direction : int
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
shove_direction = 1
|
||||
punch_direction = 1
|
||||
FaceDirection.LEFT:
|
||||
shove_direction = -1
|
||||
print_debug("Shoving %s" % push_target.name)
|
||||
current_state = State.SHOVE
|
||||
push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER)
|
||||
punch_direction = -1
|
||||
print_debug("Punching %s" % punch_target.name)
|
||||
current_state = State.PUNCH
|
||||
punch_target.apply_central_impulse(Vector2(punch_direction, 0) * PUNCH_POWER)
|
||||
|
||||
# Handle shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
@@ -118,10 +131,10 @@ func handle_movement(delta) -> void:
|
||||
# Character is jumping; apply normal gravity
|
||||
velocity += get_gravity() * delta
|
||||
if velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
current_state = State.FALL
|
||||
else:
|
||||
# Character falling; apply hard gravity
|
||||
current_state = State.FALLING
|
||||
current_state = State.FALL
|
||||
velocity += get_gravity() * HARD_GRAVITY * delta
|
||||
|
||||
func handle_collisions() -> void:
|
||||
@@ -132,46 +145,51 @@ func handle_collisions() -> void:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_POWER)
|
||||
|
||||
if right_cast.is_colliding() and facing == FaceDirection.RIGHT:
|
||||
#print_debug("Colliding with something to the right")
|
||||
var collider = right_cast.get_collider()
|
||||
# check if this is OK
|
||||
if collider is Node and collider is RigidBody2D and collider.is_in_group("pushable"):
|
||||
push_target = collider
|
||||
push_enabled = true
|
||||
#print_debug("Colliding with %s " % collider.name)
|
||||
if collider is Node and collider is RigidBody2D and collider.is_in_group("punchable"):
|
||||
print_debug("We have a punch target")
|
||||
punch_target = collider
|
||||
punch_enabled = true
|
||||
|
||||
if left_cast.is_colliding() and facing == FaceDirection.LEFT:
|
||||
#print_debug("Colliding with something to the left")
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is Node and collider is RigidBody2D and collider.is_in_group("pushable"):
|
||||
push_target = collider
|
||||
push_enabled = true
|
||||
#print_debug("Colliding with %s " % collider.name)
|
||||
if collider is Node and collider is RigidBody2D and collider.is_in_group("punchable"):
|
||||
print_debug("We have a punch target")
|
||||
punch_target = collider
|
||||
punch_enabled = true
|
||||
|
||||
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
||||
push_enabled = false
|
||||
punch_enabled = false
|
||||
|
||||
func update_state() -> void:
|
||||
match current_state:
|
||||
# If player is moving left or right
|
||||
State.IDLE when velocity.x !=0:
|
||||
current_state = State.WALK
|
||||
current_state = State.RUN
|
||||
|
||||
# If player stops walking, or starts falling
|
||||
State.WALK:
|
||||
State.RUN:
|
||||
# If not moving left or right
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
# If falling
|
||||
if not is_on_floor() and velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
current_state = State.FALL
|
||||
|
||||
# When jump peaks, we start to fall
|
||||
State.JUMP when velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
current_state = State.FALL
|
||||
|
||||
# Player lands, either still or moving
|
||||
State.FALLING when is_on_floor():
|
||||
State.FALL when is_on_floor():
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
else:
|
||||
current_state = State.WALK
|
||||
current_state = State.RUN
|
||||
|
||||
# Player shooting
|
||||
State.SHOOT_STILL:
|
||||
@@ -181,10 +199,10 @@ func update_state() -> void:
|
||||
# Player shooting while moving
|
||||
State.SHOOT_RUN:
|
||||
await player_sprite.animation_finished
|
||||
current_state = State.WALK
|
||||
current_state = State.RUN
|
||||
|
||||
# Player shoving
|
||||
State.SHOVE:
|
||||
State.PUNCH:
|
||||
await player_sprite.animation_finished
|
||||
current_state = State.IDLE
|
||||
|
||||
@@ -197,18 +215,18 @@ func update_animation() -> void:
|
||||
match current_state:
|
||||
State.IDLE:
|
||||
player_sprite.play("idle")
|
||||
State.WALK:
|
||||
State.RUN:
|
||||
player_sprite.play("run")
|
||||
State.JUMP:
|
||||
player_sprite.play("jump")
|
||||
State.FALLING:
|
||||
State.FALL:
|
||||
player_sprite.play("fall")
|
||||
State.SHOOT_STILL:
|
||||
player_sprite.play("shoot_still")
|
||||
State.SHOOT_RUN:
|
||||
player_sprite.play("shoot_run")
|
||||
State.SHOVE:
|
||||
State.PUNCH:
|
||||
player_sprite.play("punch")
|
||||
|
||||
func update_debug():
|
||||
%StateLabel.text = "Current state: %s" % current_state
|
||||
%StateLabel.text = "Current state: %s" % State.keys()[current_state]
|
||||
Reference in new issue
Block a user