2022-07-17 14:24:15 +02:00
|
|
|
extends "res://objects/characters/character.gd"
|
|
|
|
|
|
|
|
const SPEED = 400
|
|
|
|
const JUMP_HEIGHT = 1000
|
|
|
|
const STRIKED_HEIGHT = 400
|
|
|
|
export var background_scene : Resource
|
|
|
|
|
|
|
|
onready var level = get_parent()
|
|
|
|
onready var main = level.get_parent()
|
|
|
|
|
|
|
|
const ANIMATIONS = {
|
|
|
|
"idle": {
|
|
|
|
"animation": "idle",
|
|
|
|
"transistion": "idle",
|
|
|
|
"blocks": []
|
|
|
|
},
|
|
|
|
"run": {
|
|
|
|
"animation": "run",
|
|
|
|
"transistion": "run",
|
|
|
|
"blocks": []
|
|
|
|
},
|
|
|
|
"jump_start": {
|
|
|
|
"animation": "jump_start",
|
|
|
|
"transistion": "jump",
|
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "fall_end"]
|
|
|
|
},
|
|
|
|
"jump": {
|
|
|
|
"animation": "jump",
|
|
|
|
"transistion": "jump",
|
2022-07-18 12:58:38 +02:00
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "fall_end", "jump_start"]
|
2022-07-17 14:24:15 +02:00
|
|
|
},
|
|
|
|
"jump_end": {
|
|
|
|
"animation": "jump_end",
|
|
|
|
"transistion": "idle",
|
2022-07-18 12:58:38 +02:00
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "fall_end", "jump"]
|
2022-07-17 14:24:15 +02:00
|
|
|
},
|
|
|
|
"fall_start": {
|
|
|
|
"animation": "fall_start",
|
|
|
|
"transistion": "fall",
|
|
|
|
"blocks": ["idle", "run", "fall", "fall_end", "jump_start", "jump", "jump_end"]
|
|
|
|
},
|
|
|
|
"fall": {
|
|
|
|
"animation": "fall",
|
|
|
|
"transistion": "fall",
|
|
|
|
"blocks": ["idle", "run", "fall_start", "jump_start", "jump", "jump_end"]
|
|
|
|
},
|
|
|
|
"fall_end": {
|
|
|
|
"animation": "fall_end",
|
|
|
|
"transistion": "idle",
|
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "jump_start", "jump", "jump_end"]
|
|
|
|
},
|
|
|
|
"striked_start": {
|
|
|
|
"animation": "jump_start",
|
|
|
|
"transistion": "striked",
|
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "fall_end", "jump_start", "jump", "jump_end"]
|
|
|
|
},
|
|
|
|
"striked": {
|
|
|
|
"animation": "jump",
|
|
|
|
"transistion": "striked_end",
|
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "fall_end", "jump_start", "jump", "jump_end"]
|
|
|
|
},
|
|
|
|
"striked_end": {
|
|
|
|
"animation": "hurt",
|
|
|
|
"transistion": "idle",
|
|
|
|
"blocks": ["idle", "run", "fall_start", "fall", "fall_end", "jump_start", "jump", "jump_end"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var player_state = "idle"
|
|
|
|
var invincible_hit = false
|
|
|
|
|
|
|
|
var score = 0
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
$Camera2D.add_child(background_scene.instance())
|
|
|
|
$Sprite.connect("animation_finished", self, "_next_player_state")
|
|
|
|
$Sprite.animation = ANIMATIONS[player_state]['animation']
|
2022-07-18 12:58:38 +02:00
|
|
|
$Camera2D.limit_left = level.camera_limit_left
|
|
|
|
$Camera2D.limit_right = level.camera_limit_right
|
|
|
|
$Camera2D.limit_top = level.camera_limit_top
|
|
|
|
$Camera2D.limit_bottom = level.camera_limit_bottom
|
2022-07-17 14:24:15 +02:00
|
|
|
|
|
|
|
func _physics_process(delta):
|
2022-07-18 03:28:10 +02:00
|
|
|
if velocity.y > 1 and not is_on_floor():
|
2022-07-17 14:24:15 +02:00
|
|
|
_set_player_state("fall_start")
|
2022-07-18 03:28:10 +02:00
|
|
|
|
2022-07-17 14:24:15 +02:00
|
|
|
if Input.is_action_pressed("move_left") and not Input.is_action_pressed("move_right") \
|
|
|
|
and not player_state in ["striked_start", "striked", "striked_end"]:
|
|
|
|
move_left(SPEED)
|
|
|
|
elif Input.is_action_pressed("move_right") and not Input.is_action_pressed("move_left") \
|
|
|
|
and not player_state in ["striked_start", "striked", "striked_end"]:
|
|
|
|
move_right(SPEED)
|
|
|
|
elif player_state in ["idle", "run", "jump", "jump_end", "fall_end", "striked_end"]:
|
|
|
|
move_stop()
|
|
|
|
|
|
|
|
if is_on_floor():
|
2022-07-18 03:28:10 +02:00
|
|
|
snap = SNAP
|
2022-07-17 14:24:15 +02:00
|
|
|
_end_animation()
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("jump"):
|
|
|
|
if $RayBottomLeft.is_colliding() or $RayBottomRight.is_colliding():
|
|
|
|
jump()
|
|
|
|
|
|
|
|
func _next_player_state():
|
|
|
|
player_state = ANIMATIONS[player_state]["transistion"]
|
|
|
|
$Sprite.animation = ANIMATIONS[player_state]['animation']
|
|
|
|
|
|
|
|
func _set_player_state(new_state):
|
|
|
|
if not new_state in ANIMATIONS[player_state]["blocks"]:
|
|
|
|
player_state = new_state
|
|
|
|
$Sprite.animation = ANIMATIONS[player_state]['animation']
|
|
|
|
return true
|
|
|
|
return false
|
|
|
|
|
|
|
|
func move_right(delta):
|
|
|
|
_set_player_state("run")
|
|
|
|
get_node("Sprite").set_flip_h(false)
|
2022-07-18 03:28:10 +02:00
|
|
|
velocity.x = delta
|
2022-07-17 14:24:15 +02:00
|
|
|
|
|
|
|
func move_left(delta):
|
|
|
|
_set_player_state("run")
|
|
|
|
get_node("Sprite").set_flip_h(true)
|
2022-07-18 03:28:10 +02:00
|
|
|
velocity.x = -delta
|
2022-07-17 14:24:15 +02:00
|
|
|
|
|
|
|
func move_stop():
|
|
|
|
_set_player_state("idle")
|
2022-07-18 03:28:10 +02:00
|
|
|
velocity.x = 0
|
2022-07-17 14:24:15 +02:00
|
|
|
|
|
|
|
func jump():
|
|
|
|
if _set_player_state("jump_start"):
|
2022-07-18 03:28:10 +02:00
|
|
|
snap = Vector2.ZERO
|
2022-07-17 14:24:15 +02:00
|
|
|
if get_floor_velocity().y < 0:
|
|
|
|
position.y += get_floor_velocity().y * get_physics_process_delta_time() \
|
|
|
|
- GRAVITY * get_physics_process_delta_time() - 3
|
2022-07-18 03:28:10 +02:00
|
|
|
velocity.y -= JUMP_HEIGHT
|
2022-07-17 14:24:15 +02:00
|
|
|
|
|
|
|
func _on_GroundCheck_body_entered(body):
|
|
|
|
_end_animation()
|
|
|
|
|
|
|
|
func _end_animation():
|
2022-07-18 03:28:10 +02:00
|
|
|
if velocity.y > 0 and player_state in ["jump_start", "jump"]:
|
2022-07-17 14:24:15 +02:00
|
|
|
_set_player_state("jump_end")
|
2022-07-18 03:28:10 +02:00
|
|
|
if velocity.y > 0 and player_state in ["fall_start", "fall"]:
|
2022-07-17 14:24:15 +02:00
|
|
|
_set_player_state("fall_end")
|
2022-07-18 03:28:10 +02:00
|
|
|
if velocity.y > 0 and player_state in ["striked_start", "striked"]:
|
2022-07-17 14:24:15 +02:00
|
|
|
_set_player_state("striked_end")
|
|
|
|
|
|
|
|
func monster_hit(direction : int, damage: int):
|
|
|
|
if not invincible_hit:
|
|
|
|
invincible_hit = true
|
|
|
|
_set_player_state("striked_start")
|
2022-07-18 03:28:10 +02:00
|
|
|
velocity.y = -STRIKED_HEIGHT
|
|
|
|
velocity.x = direction * 5
|
2022-07-17 14:24:15 +02:00
|
|
|
yield(get_tree().create_timer(1.0), "timeout")
|
|
|
|
invincible_hit = false
|
|
|
|
|
|
|
|
func _on_Enemy_Entered(body):
|
|
|
|
body.stomped_on_head()
|
|
|
|
_set_player_state("jump")
|
|
|
|
|
|
|
|
func coin_collected(value):
|
|
|
|
main.add_score(value)
|
|
|
|
|
|
|
|
func _on_screen_exited():
|
2022-07-18 03:28:10 +02:00
|
|
|
if velocity.y > 1:
|
2022-07-17 14:24:15 +02:00
|
|
|
health = 0
|
|
|
|
main.player_died()
|