summaryrefslogtreecommitdiff
path: root/Scenes/Player.gd
blob: 21d182e7e3bdae84029cabc40f022f68d7b7f162 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
extends Entity

class_name Player

# Cube Layout:
# 0
# 1
# 5 3
#   4
# 	2

# Cardinals
#  0
# 1 3
#  2

const chart_side = [
	[4, 2, 1, 3],
	[0, 2, 5, 3],
	[4, 5, 1, 0],
	[1, 5, 4, 0],
	[3, 5, 2, 0],
	[1, 2, 4, 3],
]

const chart_spin = [
	[3, 0, 0, 2],
	[0, 1, 0, 1],
	[0, 2, 3, 0],
	[3, 0, 0, 2],
	[0, 1, 0, 1],
	[0, 2, 3, 0],
]

onready var mesh = $Mesh

var symbols = ['1', '3', '5', '2', '4', '6']

var side = 0
var spin = 0

func get_top() -> String:
	return symbols[side]

func set_bottom(symbol : String):
	symbols[5 - side] = symbol

func roll(direction : Vector2):
	var cardinal = (get_cardinal(-direction) - spin) % 4

	spin = chart_spin[side][cardinal] + spin
	side = chart_side[side][cardinal]

func get_cardinal(direction: Vector2) -> int:
	if direction.y > 0:
		return 0

	if direction.x < 0:
		return 1

	if direction.y < 0:
		return 2

	if direction.x > 0:
		return 3

	return -1

func get_upper_face() -> String:
	return get_top()

func cor_move(args : Array): # args = [Vector3, float]
	var pos_a = translation
	var pos_b = args[0]

	var dir = pos_b - pos_a

	var basis_a = mesh.transform.basis
	var basis_b = basis_a.rotated(Vector3(dir.z, 0, -dir.x), PI / 2)

	var duration = args[1]
	var time = 0

	while true:
		time = min(time + get_process_delta_time(), duration)
		var weight = time / duration

		translation = pos_a + dir * weight
		mesh.transform.basis = basis_a.slerp(basis_b, weight)
		mesh.translation = Vector3(0, 0.5 + sin(weight * PI) / 4, 0)

		if time >= duration:
			break

		yield(get_tree(), "idle_frame")

func cor_half_move(args : Array): # args = [Vector3, float]
	var pos_original = translation
	var pos_a = translation
	var pos_b = args[0]

	var dir = pos_b - pos_a

	var basis_a = mesh.transform.basis
	var basis_b = basis_a.rotated(Vector3(dir.z, 0, -dir.x), PI / 2)

	var duration = args[1]
	var time = 0

	while true:
		time = min(time + get_process_delta_time(), duration)
		var weight = time / (duration)

		translation = pos_a + dir * weight
		mesh.transform.basis = basis_a.slerp(basis_b, weight)
		mesh.translation = Vector3(0, 0.5 + sin(weight * PI) / 4, 0)

		if time >= (duration):
			break

		yield(get_tree(), "idle_frame")

	pos_b = pos_original
	pos_a = translation
	dir = pos_b - pos_a
	time = 0

	while true:
		time = min(time + get_process_delta_time(), duration)
		var weight = time / (duration)

		translation = pos_a + dir * weight

		if time >= (duration):
			break

		yield(get_tree(), "idle_frame")

func cor_shake(args : Array):
	var duration = args[0]
	var time = 0

	var zero = Vector3(0, 0.5, 0)
	var intensity = 0.03

	while true:
		time = min(time + get_process_delta_time(), duration)

		mesh.translation = zero + (2 * Vector3(randf(), randf(), randf()) - Vector3.ONE) * intensity

		if time >= duration:
			mesh.translation = zero
			break

		yield(get_tree(), "idle_frame")