Debian Codegraph @

scripts/game_flow.lua

raw symbols SCIP
1 local kernel = require 'dokidoki.kernel'
2 local v2 = require 'dokidoki.v2'
3
4 local blueprints = require 'blueprints'
5 local the_game = require 'the_game'
6
7 local LEFT = game.constants.screen_left
8 local RIGHT = game.constants.screen_right
9 local BOTTOM = game.constants.screen_bottom
10 local TOP = game.constants.screen_top
11 local CENTER = v2((LEFT + RIGHT) / 2, (TOP + BOTTOM) / 2)
12 local RADIUS = 300
13
14 local POSITIONS = {
15 false,
16 {CENTER + v2.unit(math.pi) * RADIUS,
17 CENTER + v2.unit(0) * RADIUS},
18 {CENTER + v2.unit(math.pi*5/6) * RADIUS,
19 CENTER + v2.unit(math.pi*1/6) * RADIUS,
20 CENTER + v2.unit(math.pi*9/6) * RADIUS},
21 {CENTER + v2.unit(math.pi*3/4) * RADIUS,
22 CENTER + v2.unit(math.pi*1/4) * RADIUS,
23 CENTER + v2.unit(math.pi*5/4) * RADIUS,
24 CENTER + v2.unit(math.pi*7/4) * RADIUS}
25 }
26
27 local state = 'init'
28 local selectors
29 local splash
30
31 local game_over_timer = 0
32
33 local function generate_positions(n)
34 local positions = {}
35 for i = 1, n do
36 positions[#positions+1] = v2(math.cos(i/n), math.sin(i/n)) * 200
37 end
38 return positions
39 end
40
41 local function start_game()
42 local players = {}
43 for i, selector in ipairs(selectors) do
44 if selector.selector.picked then
45 players[#players+1] = i
46 end
47 end
48
49 if #players > 0 then
50 game.actors.new(blueprints.fade_out, {'fade', callback=function ()
51 for i, selector in ipairs(selectors) do
52 selector.dead = true
53 end
54 splash.dead = true
55
56 local cpu_player
57
58 if #players == 1 then
59 cpu_player = players[1] == 1 and 2 or 1
60 players[#players+1] = cpu_player
61 end
62
63 local positions = generate_positions(#players)
64 for i, p in ipairs(players) do
65 local pos = POSITIONS[#players][i]
66 local facing = v2.norm(v2.rotate90(pos - CENTER))
67 if p == cpu_player then
68 game.actors.new(blueprints.easy_enemy_factory,
69 {'transform', pos=pos, facing=facing},
70 {'ship', player=p})
71 else
72 game.actors.new(blueprints.player_factory,
73 {'transform', pos=pos, facing=facing},
74 {'ship', player=p})
75 end
76 end
77
78 state = 'in_game'
79 game.actors.new(blueprints.fade_in)
80 end})
81
82 state = nil
83 end
84 end
85
86 function update()
87 if state == 'init' then
88 local dist = 130
89 local offset = v2(-75, 0)
90 selectors = {
91 game.actors.new(blueprints.selection_factory,
92 {'transform', pos=offset+v2(1024/2 - dist/2 - dist, 768/2), facing=v2(0, 1)},
93 {'selector', player=1}),
94 game.actors.new(blueprints.selection_factory,
95 {'transform', pos=offset+v2(1024/2 - dist/2, 768/2), facing=v2(0, 1)},
96 {'selector', player=2}),
97 game.actors.new(blueprints.selection_factory,
98 {'transform', pos=offset+v2(1024/2 + dist/2, 768/2), facing=v2(0, 1)},
99 {'selector', player=3}),
100 game.actors.new(blueprints.selection_factory,
101 {'transform', pos=offset+v2(1024/2 + dist + dist/2, 768/2), facing=v2(0, 1)},
102 {'selector', player=4})
103 }
104 splash = game.actors.new(blueprints.splash)
105
106 game.actors.new(blueprints.fade_in)
107
108 state = 'player_select'
109 elseif state == 'player_select' then
110 local player_selected = false
111 for _, s in ipairs(selectors) do
112 if s.selector.picked then
113 player_selected = true
114 end
115 end
116
117 if player_selected then
118 local c = game.actors.new(blueprints.countdown, {'countdown', callback=start_game}, {'transform', pos=v2(1024/2, 768/2 - 200)})
119 for _,s in ipairs(selectors) do
120 s.selector.callback = c.countdown.reset_counter
121 end
122 state = nil
123 end
124 elseif state == 'in_game' then
125 if #game.actors.get('factory') < 2 then
126 game_over_timer = game_over_timer + 1
127 if game_over_timer > 300 then
128 game.actors.new(blueprints.fade_out, {'fade', callback=function ()
129 game.log.print_stats()
130 kernel.switch_scene(the_game.make())
131 end})
132 end
133 end
134 else
135 -- no state
136 end
137 end