Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- @export var walk_speed = 4.0
- const TILE_SIZE = 32
- signal player_entering_door_signal
- signal player_entered_door_signal
- signal player_is_running
- signal player_event_signal
- var initial_position = Vector2(0, 0)
- var input_direction = Vector2(0, 1)
- var is_moving = false
- var can_run = true
- var is_running = false
- var stop_input:bool = false
- var percented_moved_to_next_tile = 0.0
- var action_key = "Action-UiSelect"
- var animation_list = ["Idle", "Walk", "Turn", "Run"]
- @onready var anim_tree = $AnimationTree
- @onready var anim_state = anim_tree.get("parameters/playback")
- @onready var ray = $BlockRayCast2D
- @onready var tile_over_ray = $TileOverRayCast2D
- @onready var door_ray = $DoorRayCast2D
- @onready var map_edge_ray = $MapEdgeRayCast2D
- @onready var event_ray = $EventRayCast2D
- @onready var npc_ray = $NPCRayCast2D
- @onready var anim_player = $AnimationPlayer
- @onready var player_camera = $PlayerCamera
- @onready var current_collision_layer = self.get_collision_layer()
- @onready var current_collision_mask = self.ray.get_collision_mask()
- enum PlayerState {IDLE, TURNING, WALKING}
- enum FacingDirection {LEFT, RIGHT, UP, DOWN}
- var player_state = PlayerState.IDLE
- var facing_direction = FacingDirection.DOWN
- func _ready():
- $Sprite.visible = true
- anim_tree.active = true
- initial_position = position
- anim_tree.set("parameters/Idle/blend_position", Vector2(0,1))
- for item in animation_list:
- var parameter = "parameters/%s/blend_position" %item
- anim_tree.set(parameter, input_direction)
- func set_spawn(location: Vector2, direction: Vector2):
- for item in animation_list:
- var parameter = "parameters/%s/blend_position" %item
- anim_tree.set(parameter, direction)
- position = location
- func change_position(direction: Vector2):
- for item in animation_list:
- var parameter = "parameters/%s/blend_position" %item
- anim_tree.set(parameter, direction)
- func _physics_process(delta):
- if player_state == PlayerState.TURNING or stop_input:
- return
- elif is_moving == false:
- process_player_input()
- elif input_direction != Vector2.ZERO:
- if walk_speed == 4.0:
- anim_state.travel("Walk")
- elif is_running:
- anim_state.travel("Run")
- move(delta)
- else:
- anim_state.travel("Idle")
- is_moving = false
- func process_player_input():
- if input_direction.y == 0:
- input_direction.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
- if input_direction.x == 0:
- input_direction.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
- if can_run:
- if Input.is_action_pressed(action_key):
- walk_speed = 8.0
- is_running = true
- player_is_running.emit()
- else:
- walk_speed = 4.0
- is_running = false
- if input_direction != Vector2.ZERO:
- for item in animation_list:
- var parameter = "parameters/%s/blend_position" %item
- anim_tree.set(parameter, input_direction)
- if need_to_turn():
- player_state = PlayerState.TURNING
- anim_state.travel("Turn")
- else:
- initial_position = position
- is_moving = true
- else:
- anim_state.travel("Idle")
- #TutorialStuff
- func move(delta):
- var desired_step: Vector2 = input_direction * TILE_SIZE / 2
- ray.set_target_position(desired_step)
- ray.force_raycast_update()
- tile_over_ray.set_target_position(desired_step)
- tile_over_ray.force_raycast_update()
- door_ray.set_target_position(desired_step)
- door_ray.force_raycast_update()
- map_edge_ray.set_target_position(desired_step)
- map_edge_ray.force_raycast_update()
- event_ray.set_target_position(desired_step)
- event_ray.force_raycast_update()
- npc_ray.set_target_position(desired_step)
- npc_ray.force_raycast_update()
- #DoorStuff
- if door_ray.is_colliding():
- if percented_moved_to_next_tile == 0.0:
- $PlayerCamera.make_current()
- anim_state.travel("Idle")
- stop_input = true
- player_entering_door_signal.emit()
- percented_moved_to_next_tile += walk_speed * delta
- if percented_moved_to_next_tile >= 1:
- position = initial_position + (input_direction * TILE_SIZE)
- percented_moved_to_next_tile = 0.0
- is_moving = false
- anim_state.travel("Idle")
- else:
- position = initial_position + (input_direction * TILE_SIZE * percented_moved_to_next_tile)
- elif event_ray.is_colliding():
- percented_moved_to_next_tile += walk_speed * delta
- if percented_moved_to_next_tile >= 0.9:
- position = (initial_position + (input_direction * TILE_SIZE))
- player_event_signal.emit()
- is_moving = false
- percented_moved_to_next_tile = 0.0
- else:
- position = initial_position + (input_direction * TILE_SIZE * percented_moved_to_next_tile)
- elif !ray.is_colliding() || tile_over_ray.is_colliding():
- percented_moved_to_next_tile += walk_speed * delta
- if percented_moved_to_next_tile >= 1.0:
- position = initial_position + (TILE_SIZE * input_direction)
- percented_moved_to_next_tile = 0.0
- is_moving = false
- else:
- position = initial_position + (TILE_SIZE * input_direction * percented_moved_to_next_tile)
- else:
- percented_moved_to_next_tile = 0.0
- is_moving = false
- func need_to_turn():
- var new_facing_direction
- if input_direction.x < 0:
- new_facing_direction = FacingDirection.LEFT
- elif input_direction.x > 0:
- new_facing_direction = FacingDirection.RIGHT
- elif input_direction.y < 0:
- new_facing_direction = FacingDirection.UP
- elif input_direction.y > 0:
- new_facing_direction = FacingDirection.DOWN
- if facing_direction != new_facing_direction:
- facing_direction = new_facing_direction
- return true
- facing_direction = new_facing_direction
- return false
- func finished_turning():
- player_state = PlayerState.IDLE
- var desired_step: Vector2 = input_direction * TILE_SIZE / 2
- ray.set_target_position(desired_step)
- ray.force_raycast_update()
- tile_over_ray.set_target_position(desired_step)
- tile_over_ray.force_raycast_update()
- door_ray.set_target_position(desired_step)
- door_ray.force_raycast_update()
- map_edge_ray.set_target_position(desired_step)
- map_edge_ray.force_raycast_update()
- event_ray.set_target_position(desired_step)
- event_ray.force_raycast_update()
- npc_ray.set_target_position(desired_step)
- npc_ray.force_raycast_update()
- func entered_door():
- emit_signal("player_entered_door_signal")
- func disable_collision():
- self.collision_layer = 0
- self.ray.collision_mask = 0
- func enable_collision():
- self.collision_layer = current_collision_layer
- self.ray.collision_mask = current_collision_mask
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement