|
|
|
@ -6,12 +6,12 @@ const SPEED = 100.0 |
|
|
|
const JUMP_VELOCITY = 200.0 |
|
|
|
const JUMP_VELOCITY = 200.0 |
|
|
|
|
|
|
|
|
|
|
|
const STATES= [ |
|
|
|
const STATES= [ |
|
|
|
"spawn", |
|
|
|
|
|
|
|
"idle", |
|
|
|
|
|
|
|
"walk", |
|
|
|
|
|
|
|
"climb", |
|
|
|
"climb", |
|
|
|
|
|
|
|
"die", |
|
|
|
|
|
|
|
"idle", |
|
|
|
"jump", |
|
|
|
"jump", |
|
|
|
"die" |
|
|
|
"spawn", |
|
|
|
|
|
|
|
"walk" |
|
|
|
] |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
var use_gravity = { |
|
|
|
var use_gravity = { |
|
|
|
@ -23,9 +23,23 @@ var use_gravity = { |
|
|
|
"walk": true |
|
|
|
"walk": true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var can_grab_obstacles = { |
|
|
|
|
|
|
|
"climb": false, |
|
|
|
|
|
|
|
"die": false, |
|
|
|
|
|
|
|
"idle": true, |
|
|
|
|
|
|
|
"jump": false, |
|
|
|
|
|
|
|
"spawn": false, |
|
|
|
|
|
|
|
"walk": true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@onready var grab_ray = get_node('grab_ray') |
|
|
|
|
|
|
|
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes. |
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes. |
|
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") |
|
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var grabbed_obstacle = null |
|
|
|
|
|
|
|
|
|
|
|
func _ready(): |
|
|
|
func _ready(): |
|
|
|
$fsm.set_states(STATES) |
|
|
|
$fsm.set_states(STATES) |
|
|
|
add_to_group("player") |
|
|
|
add_to_group("player") |
|
|
|
@ -37,18 +51,18 @@ func on_enter_idle_state(): |
|
|
|
pass |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
func idle_state(_delta): |
|
|
|
func idle_state(_delta): |
|
|
|
var direction = Input.get_axis("ui_left", "ui_right") |
|
|
|
var direction = Input.get_axis("player_left", "player_right") |
|
|
|
|
|
|
|
|
|
|
|
if is_on_floor(): |
|
|
|
if is_on_floor(): |
|
|
|
velocity.x = 0 |
|
|
|
velocity.x = 0 |
|
|
|
|
|
|
|
|
|
|
|
if direction: |
|
|
|
if direction: |
|
|
|
$fsm.set_next_state("walk") |
|
|
|
$fsm.set_next_state("walk") |
|
|
|
elif Input.is_action_just_pressed("ui_accept") and is_on_floor(): |
|
|
|
elif Input.is_action_just_pressed("player_jump") and is_on_floor(): |
|
|
|
$fsm.set_next_state("jump") |
|
|
|
$fsm.set_next_state("jump") |
|
|
|
|
|
|
|
|
|
|
|
func walk_state(_delta): |
|
|
|
func walk_state(_delta): |
|
|
|
var direction = Input.get_axis("ui_left", "ui_right") |
|
|
|
var direction = Input.get_axis("player_left", "player_right") |
|
|
|
|
|
|
|
|
|
|
|
if direction != 0: |
|
|
|
if direction != 0: |
|
|
|
velocity.x = direction * SPEED |
|
|
|
velocity.x = direction * SPEED |
|
|
|
@ -56,10 +70,10 @@ func walk_state(_delta): |
|
|
|
velocity.x = 0 |
|
|
|
velocity.x = 0 |
|
|
|
$fsm.set_next_state("idle") |
|
|
|
$fsm.set_next_state("idle") |
|
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): |
|
|
|
if Input.is_action_just_pressed("player_jump") and is_on_floor(): |
|
|
|
$fsm.set_next_state("jump") |
|
|
|
$fsm.set_next_state("jump") |
|
|
|
|
|
|
|
|
|
|
|
if is_on_wall() or (Input.is_action_pressed('ui_up') && is_on_ceiling()): |
|
|
|
if is_on_wall() or (Input.is_action_pressed('player_up') && is_on_ceiling()): |
|
|
|
velocity.y = 0 |
|
|
|
velocity.y = 0 |
|
|
|
$fsm.set_next_state("climb") |
|
|
|
$fsm.set_next_state("climb") |
|
|
|
|
|
|
|
|
|
|
|
@ -68,7 +82,7 @@ func on_enter_jump_state(): |
|
|
|
velocity.y = -JUMP_VELOCITY |
|
|
|
velocity.y = -JUMP_VELOCITY |
|
|
|
|
|
|
|
|
|
|
|
func jump_state(_delta): |
|
|
|
func jump_state(_delta): |
|
|
|
var direction = Input.get_axis("ui_left", "ui_right") |
|
|
|
var direction = Input.get_axis("player_left", "player_right") |
|
|
|
|
|
|
|
|
|
|
|
if(direction): |
|
|
|
if(direction): |
|
|
|
velocity.x = move_toward(velocity.x, direction * SPEED, SPEED) |
|
|
|
velocity.x = move_toward(velocity.x, direction * SPEED, SPEED) |
|
|
|
@ -80,7 +94,7 @@ func jump_state(_delta): |
|
|
|
velocity.x = 0 |
|
|
|
velocity.x = 0 |
|
|
|
$fsm.set_next_state("idle") |
|
|
|
$fsm.set_next_state("idle") |
|
|
|
|
|
|
|
|
|
|
|
if is_on_wall() || (is_on_ceiling() and Input.is_action_pressed("ui_up")): |
|
|
|
if is_on_wall() || (is_on_ceiling() and Input.is_action_pressed("player_up")): |
|
|
|
$fsm.set_next_state("climb") |
|
|
|
$fsm.set_next_state("climb") |
|
|
|
|
|
|
|
|
|
|
|
func on_enter_climb_state(): |
|
|
|
func on_enter_climb_state(): |
|
|
|
@ -92,20 +106,20 @@ func on_exit_climb_state(): |
|
|
|
|
|
|
|
|
|
|
|
func climb_state(_delta): |
|
|
|
func climb_state(_delta): |
|
|
|
if is_on_wall(): |
|
|
|
if is_on_wall(): |
|
|
|
var direction = Input.get_axis("ui_up", "ui_down") |
|
|
|
var direction = Input.get_axis("player_up", "player_down") |
|
|
|
velocity = Vector2(0, direction * SPEED) |
|
|
|
velocity = Vector2(0, direction * SPEED) |
|
|
|
|
|
|
|
|
|
|
|
if is_on_ceiling(): |
|
|
|
if is_on_ceiling(): |
|
|
|
var direction = Input.get_axis("ui_left", "ui_right") |
|
|
|
var direction = Input.get_axis("player_left", "player_right") |
|
|
|
velocity = Vector2(direction * SPEED, 0) |
|
|
|
velocity = Vector2(direction * SPEED, 0) |
|
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_wall(): |
|
|
|
if Input.is_action_just_pressed("player_jump") and is_on_wall(): |
|
|
|
var wall_position = get_slide_collision(0).get_normal().x |
|
|
|
var wall_position = get_slide_collision(0).get_normal().x |
|
|
|
var jump_normal = Vector2(wall_position, -0.5).normalized() |
|
|
|
var jump_normal = Vector2(wall_position, -0.5).normalized() |
|
|
|
velocity = jump_normal * JUMP_VELOCITY |
|
|
|
velocity = jump_normal * JUMP_VELOCITY |
|
|
|
|
|
|
|
|
|
|
|
$fsm.set_next_state("jump") |
|
|
|
$fsm.set_next_state("jump") |
|
|
|
if is_on_ceiling() && Input.is_action_just_pressed("ui_down"): |
|
|
|
if is_on_ceiling() && Input.is_action_just_pressed("player_down"): |
|
|
|
velocity.y = 0 |
|
|
|
velocity.y = 0 |
|
|
|
$fsm.set_next_state("idle") |
|
|
|
$fsm.set_next_state("idle") |
|
|
|
|
|
|
|
|
|
|
|
@ -119,6 +133,30 @@ func after_state(delta): |
|
|
|
if use_gravity[$fsm.current_state] == true: |
|
|
|
if use_gravity[$fsm.current_state] == true: |
|
|
|
velocity.y += gravity * delta |
|
|
|
velocity.y += gravity * delta |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if can_grab_obstacles[$fsm.current_state] == true: |
|
|
|
|
|
|
|
handle_grab() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func handle_grab(): |
|
|
|
|
|
|
|
if Input.is_action_just_pressed('player_action'): |
|
|
|
|
|
|
|
if grabbed_obstacle == null: |
|
|
|
|
|
|
|
var collision = grab_ray.get_collider() |
|
|
|
|
|
|
|
print('collision', collision) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if collision: |
|
|
|
|
|
|
|
var obstacle = collision |
|
|
|
|
|
|
|
print('obstacle', obstacle) |
|
|
|
|
|
|
|
if obstacle.is_in_group("obstacle"): |
|
|
|
|
|
|
|
do_grab(obstacle) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
drop_obstacle() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func do_grab(obstacle): |
|
|
|
|
|
|
|
grabbed_obstacle = obstacle |
|
|
|
|
|
|
|
obstacle.grabbed(self) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func drop_obstacle(): |
|
|
|
|
|
|
|
grabbed_obstacle.dropped() |
|
|
|
|
|
|
|
grabbed_obstacle = null |
|
|
|
|
|
|
|
|
|
|
|
func die(): |
|
|
|
func die(): |
|
|
|
$fsm.set_next_state("die") |
|
|
|
$fsm.set_next_state("die") |
|
|
|
|