aboutsummaryrefslogtreecommitdiff
path: root/src/menu/game.moon
blob: 9b871a49b5e1e29a8a7d8a091c062b7899da15da (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
settings = require("settings")
poems = require("poems")
net = require("net")
world = require("world")
log = require("log")
ui = require("ui")
win = require("window")
task = require("task")
abilities = require("abilities")

x = {}

x.node = am.group!
net.register_message("RequestRole", {})
net.register_message("RespondRole", {
	required: {
		youare: "string"
		start: "number"
		time: "number"
	}
	optional: {
		hint: "string"
		poem: "string"
	}
})
x.slide_and_fade = (node, delay) ->
	delay = delay or 0
	tween = am.ease.sine
	ease_color = (node) ->
		end_color = node.color
		node.color = end_color({a:0})
		node\action(am.series({
			am.delay(delay),
			am.tween(
				0.5,
				{
					color: end_color
				},
				tween
			)
		}))
	for _, text in pairs(node\all("text",true))
		ease_color(text)
	for _, sprite in pairs(node\all("sprites",true))
		ease_color(sprite)

	translate = node\all("translate", true)
	for _, translate_node in pairs(translate)
		print("Examining translate node:", translate_node)
		end_y = translate_node.y
		translate_node.y += 50
		translate_node\action(am.series({
			am.delay(delay),
			am.tween(
				0.5,
				{
					y: end_y
				},
				tween
			)
		}))

-- data contains
-- > data.youare --("pawn" or "unmasked")
-- and then either
-- > data.hint -- (string)
-- or
-- > data.poem -- (text)
x.create_graphic = (data) ->
	print("Creating mask on screen for data:" .. tostring(data))
	timer = ui.text(0,400,win.width-40, 100, "00:00")
	timer.node\tag("timer")
	alert_minute = false
	alert_oot = false
	timer.node\action(() ->
		now = am.eval_js("Date.now()")
		end_time = data.start + (data.time * 1000)
		countdown = math.floor((end_time - now) / 1000)
		minutes = math.floor(countdown/60)
		seconds = countdown % 60
		time_txt = string.format("%02d : %02d",minutes, seconds)
		timer.node("text").text = time_txt -- assume only 1 line?
		if countdown < 60 and not alert_minute-- 1 minute 
			log.info("1 minute alert", {"client"})
			alert_minute = true
			timer.node\action(am.play(17962709,false,1,1))
		if countdown < 0 and not alert_oot
			-- Alert sound
			log.info("out of time alert", {"client"})
			alert_oot = true
			timer.node\action(am.play(96446209,false,1,1))
		return
	)
	click = (n) ->
		return () ->
			log.info("Click " .. tostring(n), {"client"})
			return true
	timer.node\action(am.series({
		am.parallel({
			am.play(68962308,false,1,1)
			am.delay(1)
		}),
		am.parallel({
			am.play(68962308,false,1,0.75)
			am.delay(1)
		}),
		am.parallel({
			am.play(68962308,false,1,0.25)
			am.delay(1)
		})
	}))
	if data.youare == "unmasked"
		-- Do unmasked stuff
		youare = ui.text(0,300,win.width-40, 100, "You wear no mask")
		x.slide_and_fade(youare.node)
		fools = ui.text(0,200,win.width-40, 100, "These fools, they created an order based on ")
		x.slide_and_fade(fools.node,0.5)
		hint = ui.text(0,0,win.width-40, 100, data.hint)
		x.slide_and_fade(hint.node, 1)
		keep_looking = ui.text(0,-200,win.width-40,100,"Keep looking 5")
		keep_looking.node\tag("keep_looking")
		keep_looking.node\action(() ->
			now = am.eval_js("Date.now()")
			end_time = data.start + (5 * 1000)
			if now < end_time
				keep_looking.node("text").text = "Keep looking " .. math.ceil((end_time - now) / 1000)
			else
				ui.delete(keep_looking)

		)
	else
		assert(abilities[data.youare], "No ability hint for role: " .. tostring(data.youare))
		print("Going into else branch for create_graphic")
		print("ui.text was", ui.text)
		print(debug.getinfo(ui.text))
		youare = ui.text(0,332,win.width-40,100,"You are " .. data.youare)
		x.slide_and_fade(youare.node)
		--ability = ui.text(0,264,win.width-40,100,abilities[data.youare])
		--x.slide_and_fade(ability.node)
		assert(youare, "Failed to get a node from ui.text")
		text = ui.text(0,200,win.width-40,100,"You remember the words we spoke at the founding, they were:")
		x.slide_and_fade(text.node, 0.5)
		poem = ui.text(0,0,win.width-40,100,data.poem)
		x.slide_and_fade(poem.node, 1)
	log.info("Finished creating graphic",{"client"})
		

x.create = () ->
	if world.hub
		all_peers = world.hub.clients
		peers = {} -- masked players
		for clientid,connection in pairs(all_peers)
			table.insert(peers, clientid)
		unmasked = {} -- unmasked players
		for i = 1, settings.n_unmasked
			rng = math.random(#peers)
			table.insert(unmasked, table.remove(peers, rng))
		poem = poems[math.random(#poems)]
		hint = poem.hints[math.random(#poem.hints)]
		start_time = am.eval_js("Date.now()")
		client_data = {}
		for _, clientid in ipairs(peers)
			client_data[clientid] = {
				youare: "a pawn"
				poem: poem.text
				start: start_time
				time: settings.game_time
			}
			--world.hub\send(clientid, "Begin", {
				--youare: "a pawn"
				--poem: poem.text
				--start: start_time
				--time: settings.game_time
			--})
		for _, clientid in ipairs(unmasked)
			client_data[clientid] = {
				youare: "unmasked"
				hint: hint
				start: start_time
				time: settings.game_time
			}
			--world.hub\send(clientid, "Begin", {
				--youare: "unmasked"
				--hint: hint
				--start: start_time
				--time: settings.game_time
			--})
		world.level_sync.client_data = client_data
		--world.hub\listen("RequestRole","Request role", (clientid, _) ->
			--log.info("Responding with role:" .. tostring(client_data[clientid]), {"net","server"})
			--world.hub\send(clientid, "RespondRole", client_data[clientid])
		--)
	world.network\listen("RespondRole", "Respond role", (_, data) ->
		log.info("Got role from server:" .. tostring(data), {"net","client"})
		x.create_graphic(data)
		am.save_state("gameplay", data)
	)
	world.network\send("RequestRole",{})
	--world.network\listen("Begin","Begin game", (hubid, data) ->
		--log.info("Staring game, data: " .. tostring(data), {"net","client"})
		--role = data.youare
		--time_pos = am.translate(vec2(100,20))
		--x.create_graphic(data)
		--am.save_state("gameplay", data)
		--log.info("Finished saving data:" .. tostring(am.load_state("gameplay")), {"net","client"})
	--)

x