'
' RUNNER
' +++


?debug = 1
?game_on = 0


'
?duree = 90
?pop_score = 15
?max_step = 1
?tint_a = #05a
?tint_b = #08f
?tint_c = #707

?smooth = 5
?parkour_ratio = 14
?speed_bonus = .007
?initial_speed = .5


' config playground
?tile = 32
?margin = 2
?sz = 8
?ghosting = 4
?half = ?tile / 2
?quad = ?tile / 4
?position_ratio = 0
?position_target = .95
?release = 0


'
goto "prepare_voxels"
goto "prepare_scene"

'
'	SCENE
'


'?v_angle = 0
?cube_angle = 0
?run = ?tile * 2
?offset = ?tile * 2
?past = ?sz
?seq = 1' iteration sz
?serie = 1
?serie_seq = 6
?ring_index = 0
?ring = 0


' player
?side = 0
?step = 0
?pos = 0
?move = 0
?vec = 0
?angle = 0

?vx = 0
?vy = 0
?va = 0
?flip = 0


goto "game_init"
goto "intro"
goto "run"


label "run"
	
	do
		
		if ?game_on = 0
			goto "wait_start"
		end
		
		goto "move_player"
		goto "infinite_tunnel"
		goto "manage_pops"
		goto "ui"
		
		update
		
	loop
	
return

'

label "wait_start"
	
	for 1 to 3 as ?s
		limit ?select_z min ?run + ?tile * 1.5
		move "select-?s" to 0, 24-?s * 12, ?select_z
	next
	
	if ?score = 0
		' seulement la 1ère fois
		?intro_ui = 0
	end
	
	
	if (key "space") = 1
		goto "start_game"
	end
	
return

'
'

label "reside"
	param ?reside
	
	?side + ?reside
	?angle + ?reside * 90
	
	' selon distance
	if (sign ?pos) = (sign ?reside)
		?pos = -?pos
	else
		?va + (?reside * 360)' ?flip =2
		?flip = 2
	end
	
	
	if ?side > 2
		?side - 4
	else if ?side < -1
		?side + 4
	end
	
	
return


''

label "move_player"
	
	' controle clavier
	if (press once "up") = 1
		goto "reside", 2
	else if (press once "left") = 1
		?step - 1
	else if (press once "right") = 1
		?step + 1
	end
	
	' limites
	if ?step < -?max_step
		?step = 1
		goto "reside", -1
	else if ?step > ?max_step
		?step = -1
		goto "reside", 1
	end
	
	?move = ?step * ?quad
	?pos + (?move - ?pos) / ?smooth
	
	' position
	if ?side = 0
		' bas
		?px = ?pos
		?py = -?half + 1
	else if ?side = 1
		' droite
		?px = ?half - 1
		?py = ?pos
	else if ?side = 2
		' haut
		?px = -?pos
		?py = ?half - 1
	else if ?side = -1
		' gauche
		?px = -?half + 1
		?py = -?pos
	end
	
	
	
	
	
	?dx = ?px - ?vx
	?dy = ?py - ?vy
	?flip + (1-?flip) / 10
	
	?va + (?angle - ?va) / (10 * ?flip)
	?vx + ?dx / ?smooth
	?vy + ?dy / ?smooth
	
	?run + ?run_speed
	
	?position_ratio + (?position_target - ?position_ratio) / 50
	?final_x = ?vx * ?position_ratio
	?final_y = ?vy * ?position_ratio
	move "player" to ?final_x, ?final_y, ?run
	turn "player" to 0, 0, ?va
	
	?s = 4
	for 1 to ?ghosting as ?g
		[ghost, ?g, x] + (?final_x - [ghost, ?g, x]) / ?s
		[ghost, ?g, y] + (?final_y - [ghost, ?g, y]) / ?s
		[ghost, ?g, a] + (?va - [ghost, ?g, a]) / ?s
		move "ghost?g" to [ghost, ?g, x], [ghost, ?g, y], ?run
		turn "ghost?g" to 0, 0, [ghost, ?g, a]
		?s + 2
	next
	
	'
	move "orb" to ?px * .7, ?py * .7, ?run
	'move light "far" to 0,0,?run + ?tile * 3
	
	?cube_angle + (?angle - ?cube_angle) / 30
	move "camera" to 0, 0, ?run - ?tile * .7
	turn "camera" to 0, 0, ?cube_angle
	
return

'

label "infinite_tunnel"
	
	if ?run - ?offset >= ?tile
		?offset + ?tile
		?past + 1
		
		if ?serie_seq < 1
			?serie_seq = (dice 1 to 6)
			?serie = (dice 1 to 4)
		else
			?serie_seq - 1
		end
		
		'?serie = (mod (round ?parkour / 9) by 3) + 1
		
		if ?pop_seq = 5 or ?pop_seq = 9
			' hidden...
		else
			move "cube-?serie-?seq" to 0, 0, ?past * ?tile
			
			if (dice 1 to 2) = 1
				?d = (dice -?half to ?half)
				move "line?seq" to ?d, ?d, ?past * ?tile
			end
		end
		
		?seq + 1
		if ?seq > ?sz
			?seq = 1
		end
		
		
		if ?game_on = 1
			?parkour + 1
			goto "pop"
		end
		
	end
	
	
return

'
'
'

label "manage_pops"
	
	' step 1 car ring_index peut>ring
	for ?ring_index to ?ring step 1 as ?r
		?item = "ring-?r"
		if (is ?item) = 1
			turn ?item by 3, 2, 1
			' garbage
			if [ring, ?r] < (?run - ?tile)
				if [ring, ?r, done] = 0
					?run_speed - ?speed_bonus / 2
				end
				delete ?item
			else if [ring, ?r, done] > 0
				zoom ?item to [ring, ?r, done]
				move ?item by 0, 0, ?run_speed * .9
				[ring, ?r, done] * .8
			else if (distance ?item to "player") < 4
				?score + (round ?pop_score * ?run_speed)
				?run_speed + ?speed_bonus
				[ring, ?r, done] = .5
			end
		else
			?ring_index + 1
		end
	next
	
return

'

label "pop"
	
	?pop_seq = (ceil ?parkour / ?parkour_ratio)
	
	if ?pop_seq = 0
		' pas encore...
	else
		
		if ?ring_len = 0
			
			if ?pop_seq = 1
				?ring_side = 0
				?ring_pos + 1
				if ?ring_pos > 1
					?ring_pos = -1
				end
				?ring_len = 3
			else if ?pop_seq = 2
				?ring_side = (dice 0 to 1 not ?ring_side)
				?ring_pos = 1-(?ring_side * 2)'0=1,1=-1
				?ring_len = (dice 2 to 3)
			else if ?pop_seq = 3
				?ring_side = 1
				?ring_pos = (dice 0 to 1 not ?ring_pos)
				?ring_len = (dice 1 to 4)
			else if ?pop_seq = 4
				?ring_side = (dice 0 to 1)
				?ring_pos = 1-(?ring_side * 2)'0=1,1=-1
				?ring_len = 1
			else if ?pop_seq = 5
				' black hole
				?ring_side = (dice -1 to 2 not ?ring_side)
				?ring_pos = 0
				?ring_len = (dice 2 to 4)
			else if ?pop_seq = 6
				?ring_side = 1'important
				?ring_pos = (dice 0 to 1 not ?ring_pos)
				?ring_len = (dice 1 to 2)
			else if ?pop_seq = 7
				?ring_side = -?ring_side
				?ring_pos = (dice -1 to 1 not ?ring_pos)
				?ring_len = (dice 1 to 3)
			else if ?pop_seq = 8
				?ring_side = 1
				?ring_pos = (dice -1 to 1)
				?ring_len = 1
			else if ?pop_seq = 9
				' black hole
				?ring_side = (dice -1 to 2)
				?ring_pos = (coin)
				?ring_len = 4
			else if ?pop_seq = 10
				?ring_pos + 1
				if ?ring_pos > 1
					?ring_pos = -1
					?ring_side + 1
				end
				if ?ring_side > 2
					?ring_side = -1
				end
				?ring_len = 1
			else if ?pop_seq = 11
				?ring_side = (coin)
				?ring_pos = 0
				?ring_len = (dice 1 to 2)
				
			else
				?ring_side = (dice -1 to 2)
				?ring_pos = (dice -1 to 1)
				?ring_len = 1
			end
		end
		
		
		?ring + 1
		?ring_len - 1
		[ring, ?ring] = ?past * ?tile + ?tile' ?half
		
		?item = "ring-?ring"
		'color (random 2 to 5)
		color (dice 3 to 5)
		create sprite 5 as ?item
		paint ?item to "neon"
		add ?item frame 1, 1, 1 to 5, 5, 5
		
		?p = ?ring_pos * ?quad
		if ?ring_side = 0
			?x = ?p
			?y = -?half
		else if ?ring_side = 1
			?x = ?half
			?y = ?p
		else if ?ring_side = 2
			?x = -?p
			?y = ?half
		else if ?ring_side = -1
			?x = -?half
			?y = -?p
		end
		
		move ?item to ?x * .8, ?y * .8, [ring, ?ring]
		zoom ?item to 0.50
		
	end
	
return

'
'
'

label "ui"
	
	if ?debug = 1
		color 21
		clear screen
		trace text "pop_seq =?pop_seq " to -100, 100
		trace text "parkour=?parkour" to -100, 80
		
		trace text "fps=" + (fps) to -100, 70
		trace text "index=?ring_index" to -100, 60
		trace text "speed=?run_speed" to -100, 50
	end
	
	if ?score = 0 or ?game_on = 0
		?timer = (timer)
	end
	
	if ?score > ?display_score
		?display_score + 1
	end
	
	'color #00ff0088
	color #0ff
	create text ?display_score as "score"
	paint "score" to "ui"
	zoom "score" to 0.50, 0.50, 0.10
	goto "ui_voxel", "score", 90
	
	?t = ?timer - (timer) + ?duree
	?text = (round ?t to 1) + "'"
	if ?t < 0
		goto "game_over"
		?text = "STOP"
	end
	
	'?t = ?cube_angle
	rgb 100, 100 * (?t / ?duree), 0
	
	create text ?text as "timer"
	paint "timer" to "ui"
	zoom "timer" to 0.30, 0.30, 0.10
	goto "ui_voxel", "timer", -90
	
	color #808
	?s = (round ?run_speed * 100) + "%"
	
return

'

label "ui_voxel"
	param ?text
	param ?angle_offset
	
	?intro_ui + (1.5-?intro_ui) / 80
	
	?a = ?cube_angle + ?angle_offset
	move ?text to -?quad * 5 * (sin -?a), -?quad * 5 * (cos -?a), ?run + ?quad * ?intro_ui
	turn ?text to 0, 0, ?cube_angle
	
return

'
'
'

label "intro"
	
	color #0cf
	?select_z = ?run + ?tile * 5
	create text "RUNNER" as "select-1"
	paint "select-1" to "ui"
	'move "select-1" to 0,?half,?tile * 5
	
	color #f0f
	create text "Press SPACE" as "select-2"
	paint "select-2" to "ui"
	'move "select-2" to 0,-?half / 2,?tile * 7
	
	color #fff
	create text "to start" as "select-3"
	paint "select-3" to "ui"
	'move "select-3" to 0,-?half / 2,?tile * 8
	
	
	
return

'

label "game_over"
	
	' partie fine
	
	if ?game_on = 1
		?game_on = 0
		goto "intro"
	end
	
	?run_speed * .994
	?t = 0
	?text = "STOP"
	
	?angle = (round ?angle / 360) * 360
	?side = 0
	
return

'

label "start_game"
	
	goto "game_init"
	?game_on = 1
	
	color #f0f
	create text "START" as "start"
	paint "start" to "neon"
	move "start" to 0, 0, ?run + ?tile * 7
	zoom "start" to 0.80
	
return

'

label "game_init"
	
	?game_on = 0
	?score = 0
	?display_score = 0
	
	?pop_seq = 0
	?ring_side = 0
	?ring_pos = -1
	?ring_len = 5
	
	'
	?run_speed = ?initial_speed
	?parkour = 0
	
return

'
'
'

label "prepare_scene"
	
	fov 80
	free "camera"
	
	show fog #024 to ?tile * ?sz
	delete "sun"
	
	power "glow" to 35
	create orb as "orb"
	
return

'
'
'

label "prepare_voxels"
	
	
	create sprite 6 as "player"
	color 1
	paint "player" to "metal"
	add "player" frame 1, 1, 1 to 6, 1, 6
	zoom "player" to 0.50, 0.10, 0.50
	'center "player" to 4,0,4
	
	'
	' FX ghost player
	'
	?alpha = 50
	for 1 to ?ghosting as ?g
		copy "player" as "ghost?g"
		paint "ghost?g" to "neon"' glass
		rgba 0, 50, 100, ?alpha
		fill "ghost?g"
		?alpha / 2
	next
	
	'
	'
	'
	
	?quad = ?tile / 4
	create sprite ?tile as "cube-1"
	'show "cube" paint metal
	color ?tint_a
	add "cube-1" frame 1, 1, 1 to ?tile, ?tile, ?tile
	color ?tint_b
	add "cube-1" frame ?quad, 1, ?quad to ?quad * 3, 1, ?quad * 3
	add "cube-1" frame ?quad, ?tile, ?quad to ?quad * 3, ?tile, ?quad * 3
	add "cube-1" frame 1, ?quad, ?quad to 1, ?quad * 3, ?quad * 3
	add "cube-1" frame ?tile, ?quad, ?quad to ?tile, ?quad * 3, ?quad * 3
	
	
	'
	color ?tint_a
	create sprite ?tile as "cube-2"
	'add "cube-2" frame 1,1,1 to ?tile,?tile,?tile
	add "cube-2" frame 1, 1, ?quad * 2 to ?tile, 1, ?quad * 3
	add "cube-2" frame 1, ?tile, ?quad * 2 to ?tile, ?tile, ?quad * 3
	add "cube-2" frame 1, 1, ?quad * 0 to 1, ?tile, ?quad * 2
	add "cube-2" frame ?tile, 1, ?quad * 0 to ?tile, ?tile, ?quad * 2
	
	
	create sprite ?tile as "cube-3"
	color ?tint_b
	add "cube-3" frame 1, 1, ?quad * 1 to ?tile, 1, ?quad * 3
	add "cube-3" frame 1, ?tile, ?quad * 1 to ?tile, ?tile, ?quad * 3
	color ?tint_a
	add "cube-3" frame 1, ?quad, 1 to 1, ?quad * 3, ?tile
	add "cube-3" frame ?tile, ?quad, 1 to ?tile, ?quad * 3, ?tile
	
	
	?quad = ?tile / 4
	create sprite ?tile as "cube-4"
	color ?tint_b
	add "cube-4" frame 1, 1, 1 to ?tile, ?tile, ?tile
	color ?tint_a
	add "cube-4" frame ?quad, 1, ?quad to ?quad * 3, 1, ?quad * 3
	add "cube-4" frame ?quad, ?tile, ?quad to ?quad * 3, ?tile, ?quad * 3
	add "cube-4" frame 1, ?quad, ?quad * 2 to 1, ?quad * 3, ?quad * 4
	add "cube-4" frame ?tile, ?quad, ?quad * 2 to ?tile, ?quad * 3, ?quad * 4
	
	for 1 to 4 as ?s
		center "cube-?s" to ?half, ?half, ?tile' car Z dans le mauvais sens !!
		hide "cube-?s"
	next
	
	
	'
	' deco
	'
	color ?tint_c
	create sprite ?tile * 2 as "line"
	'show "line" paint metal
	add "line" frame 1, 1, 1 to ?tile * 2, ?tile * 2, 1
	'zoom "line" to 3
	hide "line"
	'move "line" to -?tile,0,300
	
	
	
	'
	'	Clones
	'
	
	for 1 to ?sz as ?i
		clone "cube-1" as "cube-1-?i"
		clone "cube-2" as "cube-2-?i"
		clone "cube-3" as "cube-3-?i"
		clone "cube-4" as "cube-4-?i"
		'move "cube-1-?i" to 0,0,-?tile * 2
		clone "line" as "line?i"
	next
	
return
x