-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.erl
101 lines (84 loc) · 2.16 KB
/
clock.erl
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
%%%-------------------------------------------------------------------
%%% File : clock.erl
%%% Author : ROAM
%%% Description : Just a clock ticker
%%%
%%% Created : 19 Mar 2010 by Osei Poku <opoku>
%%%-------------------------------------------------------------------
-module(clock).
-export([start/0,start/1,init/1,stop/0,set_tick/1,pause/0,resume/0,resume/1]).
-include("common.hrl").
-include("game_state.hrl").
-define(CLOCK_TIME, 200).
-define(FOOD_GENERATION_INTERVAL, 10).
start() ->
start(?CLOCK_TIME).
start(TimeOut) ->
spawn(clock, init, [TimeOut]).
init(TimeOut) ->
register(game_clock, self()),
loop(TimeOut).
stop() ->
game_clock ! {self(), stop},
receive
_ ->
ok
end.
loop(Time) ->
loop(Time, 1).
%% loop(_, 10) ->
%% done;
get_new_foods(Tick) ->
case Tick rem ?FOOD_GENERATION_INTERVAL of
1 -> food:get_new_foods();
_ -> []
end.
pause() ->
game_clock ! {pause},
ok.
resume() ->
resume(?CLOCK_TIME).
resume(TimeOut) ->
game_clock ! {resume, TimeOut},
ok.
get_new_player_positions() ->
NewPos = game_logic:get_new_player_position(),
%%?LOG("DEBUG: in get_new_player_positions, before return NewPos -->~n ~p~n", [NewPos]),
NewPos.
set_tick(NewTick) ->
game_clock ! {set_tick, NewTick},
ok.
loop(Time, Tick) ->
receive
{Pid, stop} ->
Pid ! ok,
?LOG("Game Clock stopping~n",[]);
{forcetick} ->
game_manager:broadcast_tick(Tick),
loop(Time, Tick+1);
{set_tick, T} ->
loop(Time, T);
{pause} ->
loop(infinity, Tick);
{resume, TimeOut} ->
loop(TimeOut, Tick)
after
Time ->
case game_manager:is_leader() of
true ->
%% Generate food every FOOD_GENERATION_INTERVAL.
Options = case get_new_player_positions() of % each is {SnakeId, Position(a list of coords)}
[] -> [];
L -> [{newpos, L}]
end,
Options1 = case get_new_foods(Tick) of
[] -> Options;
NewFoods -> [{food, NewFoods}|Options]
end,
game_manager:broadcast_tick(Tick, Options1);
_Else ->
%% TODO: maybe grab the current tick value from the gamelogic
nothing
end,
loop(Time, Tick+1)
end.