-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo2.gd
More file actions
69 lines (49 loc) · 1.45 KB
/
demo2.gd
File metadata and controls
69 lines (49 loc) · 1.45 KB
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
63
64
65
66
67
68
69
extends CharacterBody2D
@export var move_speed := 200
@export var run_speed := 350
@export var jump_force := 450
@export var gravity := 1200
@export var max_jumps := 2
var jumps_left := 0
var is_running := false
var is_facing_right := true
@onready var sprite := $AnimatedSprite2D
@onready var jump_sound := $JumpSound
@onready var land_sound := $LandSound
func _ready() -> void:
jumps_left = max_jumps
func _physics_process(delta: float) -> void:
handle_input()
apply_gravity(delta)
handle_movement(delta)
update_animation()
func handle_input() -> void:
is_running = Input.is_action_pressed("ui_accept")
if is_on_floor():
jumps_left = max_jumps
if Input.is_action_just_pressed("ui_up") and jumps_left > 0:
velocity.y = -jump_force
jumps_left -= 1
jump_sound.play()
func apply_gravity(delta: float) -> void:
if not is_on_floor():
velocity.y += gravity * delta
else:
if velocity.y > 0:
velocity.y = 0
land_sound.play()
func handle_movement(_delta: float) -> void:
var direction := Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
var speed := run_speed if is_running else move_speed
velocity.x = direction * speed
if direction != 0:
is_facing_right = direction > 0
sprite.flip_h = not is_facing_right
move_and_slide()
func update_animation() -> void:
if not is_on_floor():
sprite.play("jump")
elif abs(velocity.x) > 10:
sprite.play("run" if is_running else "walk")
else:
sprite.play("idle")