-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.gd
127 lines (104 loc) · 3.48 KB
/
player.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
extends CharacterBody2D
@export var SPEED = 120.0
@export var JUMP_VELOCITY = -300.0
enum PlayerState {IDLE, RUN, CAST, JUMP, FALLING, DASH, DEATH}
var state: PlayerState = PlayerState.IDLE
var spell_scene = preload("res://Scenes/Spells/spell.tscn")
var alternate_spell_scene = preload("res://Scenes/Spells/spell2.tscn")
@export var health = 3
func _enter_tree() -> void:
set_multiplayer_authority(name.to_int())
func _ready() -> void:
# Any setup that should happen once the node is added to the scene tree
pass
func _physics_process(delta: float) -> void:
if is_multiplayer_authority():
apply_physics(delta)
move_and_slide()
func apply_physics(delta: float) -> void:
# Add gravity
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump
if Input.is_action_just_pressed("ui_up") and is_on_floor():
set_state(PlayerState.JUMP)
velocity.y = JUMP_VELOCITY
# Handle movement/deceleration
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
# Determine vertical state
if velocity.y < 0:
set_state(PlayerState.JUMP)
elif velocity.y > 0:
set_state(PlayerState.FALLING)
# Determine horizontal state
if velocity.x == 0 and velocity.y == 0 and state != PlayerState.CAST and state != PlayerState.DEATH:
set_state(PlayerState.IDLE)
elif velocity.x != 0 and velocity.y == 0:
set_state(PlayerState.RUN)
update_animations()
func update_animations() -> void:
match state:
PlayerState.IDLE:
$AnimatedSprite2D.play("default")
PlayerState.RUN:
$AnimatedSprite2D.flip_h = velocity.x < 0
$AnimatedSprite2D.play("run")
PlayerState.CAST:
$AnimatedSprite2D.play("cast")
PlayerState.FALLING:
$AnimatedSprite2D.play("fall")
PlayerState.JUMP:
$AnimatedSprite2D.play("jump")
PlayerState.DASH:
$AnimatedSprite2D.play("dashup")
PlayerState.DEATH:
$AnimatedSprite2D.play("death")
func set_state(new_state: PlayerState) -> void:
if state != new_state:
state = new_state
update_animations()
if is_multiplayer_authority():
rpc("sync_state", new_state)
# Function to synchronize state across clients
@rpc("any_peer")
func sync_state(new_state: PlayerState) -> void:
state = new_state
update_animations()
func _input(event) -> void:
if event is InputEventMouseButton and event.pressed:
if event.button_index == MOUSE_BUTTON_LEFT:
cast_spell(event.position, false)
elif event.button_index == MOUSE_BUTTON_RIGHT:
cast_spell(event.position, true)
func cast_spell(position, is_alternate_spell: bool) -> void:
var world_position = get_global_mouse_position()
if is_multiplayer_authority():
spawn_spell(world_position, is_alternate_spell)
rpc("spawn_spell", world_position, is_alternate_spell)
# Function to spawn spell instance across clientsd
@rpc("any_peer")
func spawn_spell(world_position, is_alternate_spell: bool) -> void:
var spell_instance = alternate_spell_scene.instantiate() if is_alternate_spell else spell_scene.instantiate()
spell_instance.position = world_position
get_parent().add_child(spell_instance)
func hurt_player() -> void:
health -= 1
if is_multiplayer_authority():
rpc("sync_health", health)
# Function to synchronize health across clients
@rpc("any_peer")
func sync_health(new_health) -> void:
health = new_health
if health <= 0:
kill_player()
func kill_player() -> void:
velocity.x = 0
set_state(PlayerState.DEATH)
await get_tree().create_timer(0.6).timeout
game_over()
func game_over() -> void:
get_tree().quit()