-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_body_2d.gd
62 lines (43 loc) · 1.72 KB
/
character_body_2d.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
extends CharacterBody2D
const SPEED = 180.0
const JUMP_VELOCITY = -300.0
var isJumping = true
@onready var animated_sprite_2d = $AnimatedSprite2D
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor() and Values.is_alive == true:
isJumping = true
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("ui_accept") and not is_on_floor() and isJumping and Values.is_alive == true:
isJumping = false
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("respawn"):
Values.is_alive = true
get_tree().reload_current_scene()
Values.time = 0
if Values.is_alive == false:
animated_sprite_2d.play("die")
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if Input.is_action_just_pressed("open menu"):
get_tree().change_scene_to_file("res://meny.tscn")
if direction and Values.is_alive == true:
animated_sprite_2d.play("run")
velocity.x = direction * SPEED
else:
if Values.is_alive == true:
animated_sprite_2d.play("idel")
velocity.x = move_toward(velocity.x, 0, SPEED)
if direction == -1 and Values.is_alive == true:
animated_sprite_2d.flip_h = true
else: animated_sprite_2d.flip_h = false
move_and_slide()
func _on_animated_sprite_2d_animation_finished():
if animated_sprite_2d.animation == "die":
get_tree().change_scene_to_file("res://death_screen.tscn")