aboutsummaryrefslogtreecommitdiffstats
path: root/godot/scenes/Die.gd
blob: 62d1d999290b7513b1a9512022fdf85b8dc850be (plain) (blame)
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
extends RigidBody

const Game = preload("res://scenes/Game.gd")

var camrot_h = 0
var camrot_v = 0
var camrot_v_locked_val = 0
var cam_v_min = -90
var cam_v_max = 90
var sensitivity = 0.5
var dice_is_moving = false
var last_frame_position = Vector3(1.0,1.0,1.0)
var dragging = false
var mouse_origin = Vector2(0,0)
var dice_launch_force = 0

var mat
var target_bounce = 1
var previous_bounciness = 0

var _game: Game = null

# Called when the node enters the scene tree for the first time.
func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	$CamRoot/Horizontal/Vertical/Camera.add_exception(self)
	$CamRoot.set_as_toplevel(true)
	_game = get_node("/root/Game")
	mat = get_physics_material_override()
	previous_bounciness = mat.get_bounce()
	
	ball()


func _input(event):	
	if event is InputEventKey:
		if event.scancode == KEY_TAB:
			if event.is_pressed():
				_game.open_scoreboard()
			else:
				_game.close_scoreboard()
	
	if event is InputEventMouseMotion:
		camrot_h += -event.relative.x * sensitivity
		camrot_v += -event.relative.y * sensitivity
	
	if dice_is_moving == false:
		if event is InputEventMouseMotion and dragging:
			# While dragging, move the sprite with the mouse.
			dice_launch_force = mouse_origin.distance_to(event.global_position)
			camrot_v = camrot_v_locked_val
		elif event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
			mouse_origin = event.global_position
			camrot_v_locked_val = camrot_v
			if not dragging and event.pressed:
				dragging = true
			# Stop dragging if the button is released.
			elif dragging and not event.pressed:
				dragging = false
				Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _physics_process(delta):
	camrot_v = clamp(camrot_v, cam_v_min, cam_v_max)
	
	$CamRoot/Horizontal.rotation_degrees.y = camrot_h
	$CamRoot/Horizontal/Vertical.rotation_degrees.x = camrot_v
	$CamRoot.translation.x = translation.x
	$CamRoot.translation.y = translation.y
	$CamRoot.translation.z = translation.z
	
	if last_frame_position != Vector3(translation.x, translation.y, translation.x):
		dice_is_moving = true
	else:
		dice_is_moving = false
		
		if angular_velocity.length() == 0:
			get_upwards_side()
	
	last_frame_position = Vector3(translation.x, translation.y, translation.x)


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass


"""
BUFFS
"""

func get_upwards_side():
	var forward = -global_transform.basis.z
	
	var up = forward.angle_to(Vector3.UP)
	var down = forward.angle_to(Vector3.DOWN)
	var left = forward.angle_to(Vector3.LEFT)
	var right = forward.angle_to(Vector3.RIGHT)
	var forw = forward.angle_to(Vector3.FORWARD)
	var back = forward.angle_to(Vector3.BACK)
	
	# 0 = ball, 1 = phase, 2 = bounce
	var dictionary = {
		up: 0,
		down: 1,
		left: 2,
		right: 0,
		forw: 1,
		back: 2
	}
	
	var smallest = dictionary.keys().min()
	return dictionary[smallest]


func extra_stroke():
	_game.revoke_stroke()


func bounciness():
	mat.set_bounce(target_bounce)


func bounciness_revert():
	mat.set_bounce(previous_bounciness)


func phase():
	set_collision_layer_bit(2, false)


func phase_revert():
	set_collision_layer_bit(2, true)


func ball():
	$BallShape.show()
	$BallShape.set_process(true)
	$BallShape.disabled = false


func ball_revert():
	$BallShape.hide()
	$BallShape.set_process(false)
	$BallShape.disabled = true