aboutsummaryrefslogtreecommitdiff
path: root/client/player/player.gd
blob: b47216d6374ca2f03424427ddd1b0440bfd7f24a (plain)
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
128
129
130
131
132
133
134
135
136
# Undercooked - a game about cooking
# Copyright 2024 nokoe
# Copyright 2024 metamuffin
# Copyright 2024 tpart
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
class_name Player
extends Node3D

const PLAYER_SIZE: float = 0.4
const SPEED: float = 25.

var game: Game
var rotation_ = 0.
var rotation_anim = 0.
var position_ = Vector2(0, 0)
var position_anim = Vector2(0, 0)

var character: Character = preload("res://player/character/character.tscn").instantiate()
var chat_bubble: ChatBubble = preload("res://player/chat_bubble.tscn").instantiate()
var item_bubble: ItemBubble = preload("res://player/item_bubble.tscn").instantiate()
var effect: Effect = preload("res://player/effect.tscn").instantiate()

var clear_timer: Timer = Timer.new()

var hand: Item = null
var hand_base: Node3D = Node3D.new()
var character_idx: int

var _anim_angle: float = 0.0

const HAND_BASE_POSITION: Vector3 = Vector3(0, .25, .4)

func _init(_id: int, new_name: String, pos: Vector2, new_character_idx: int, new_game: Game):
	add_child(character)
	position_ = pos
	position_anim = pos
	name = new_name
	game = new_game

	hand_base.position = HAND_BASE_POSITION
	add_child(hand_base)

	add_child(chat_bubble)
	add_child(item_bubble)
	add_child(effect)

	clear_timer.one_shot = true
	clear_timer.wait_time = 5.
	add_child(clear_timer)

	character_idx = new_character_idx


func _ready():
	character.select_hairstyle(character_idx)
	clear_timer.timeout.connect(clear_message)

func update_position(new_position: Vector2, new_rotation: float):
	position_ = new_position
	rotation_ = new_rotation

func set_item(i: Item):
	i.owned_by = hand_base
	if hand != null:
		hand.queue_free()
	if i == null:
		push_error("tile is null")
	hand = i
	character.holding = true

func remove_item() -> Item:
	var i = hand
	if i == null:
		push_error("holding nothing")
	hand = null
	character.holding = false
	return i

func take_item(tile: Tile):
	if hand != null:
		push_error("already holding an item")
	var i = tile.take_item()
	set_item(i)

func put_item(tile: Tile):
	var i = remove_item()
	tile.put_item(i)

func _process(delta):
	_anim_angle = fmod(_anim_angle + delta, TAU)
	hand_base.position.y = HAND_BASE_POSITION.y + 0.05 * sin(_anim_angle * 3)
	position_anim = lerp(position_anim, position_, delta * 10)
	rotation_anim = lerp_angle(rotation_anim, rotation_, delta * 10)
	position.x = position_anim.x
	position.z = position_anim.y
	rotation.y = rotation_anim
	character.walking = position_.distance_squared_to(position_anim) > 0.001


func clear_message():
	item_bubble.remove_item()
	chat_bubble.remove_text()
	effect.clear_effect()

func item_message(item_name: String, persist: bool):
	item_bubble.set_item(item_name)
	if persist:
		clear_timer.stop()
	else:
		clear_timer.start()

func text_message(m: String, persist: bool):
	chat_bubble.set_text(m)
	if persist:
		clear_timer.stop()
	else:
		clear_timer.start()

func effect_message(effect_name: String, persist: bool):
	effect.set_effect(effect_name)
	if persist:
		clear_timer.stop()
	else:
		clear_timer.start()