1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Provides basic game logic utilities using buckets to coordinate and
18 # optimize actions on game turn ends. Supports both action at each
19 # end of turn as well as actions on some end of turns.
21 # Allows for fast support of a large number of entities with rare actions,
22 # such as a forest with many individual trees.
25 # Something acting on the game
26 class Turnable[G
: Game]
28 # Execute `turn` for this instance.
29 fun do_turn
(turn
: GameTurn[G
]) is abstract
32 # Something acting on the game from time to time
33 class Bucketable[G
: Game]
35 private var act_at
: nullable Int = null
37 # Cancel the previously registered acting turn
39 # Once called, `self.do_turn` will not be invoked until `GameTurn::act_next`
40 # or `GameTurn::act_in` are called again.
41 fun cancel_act
do act_at
= null
44 # Optimized organization of `Bucketable` instances
45 class Buckets[G
: Game]
48 # Bucket type used in this implementation.
49 type BUCKET: HashSet[Bucketable[G
]]
51 private var next_bucket
: nullable BUCKET = null
52 private var current_bucket_key
: Int = -1
54 # Number of `buckets`, default at 100
56 # Must be set prior to using any other methods of this class.
59 private var buckets
: Array[BUCKET] =
60 [for b
in n_buckets
.times
do new HashSet[Bucketable[G
]]] is lazy
62 # Add the Bucketable event `e` at `at_tick`.
63 fun add_at
(e
: Bucketable[G
], at_tick
: Int)
65 var at_key
= key_for_tick
(at_tick
)
67 if at_key
== current_bucket_key
then
68 next_bucket
.as(not null).add
(e
)
70 buckets
[at_key
].add
(e
)
76 private fun key_for_tick
(at_tick
: Int): Int
78 return at_tick
% buckets
.length
81 redef fun do_turn
(turn
: GameTurn[G
])
83 current_bucket_key
= key_for_tick
(turn
.tick
)
84 var current_bucket
= buckets
[current_bucket_key
]
86 var next_bucket
= new HashSet[Bucketable[G
]]
87 buckets
[current_bucket_key
] = next_bucket
88 self.next_bucket
= next_bucket
90 for e
in current_bucket
do
92 if act_at
!= null then
93 if turn
.tick
== act_at
then
95 else if act_at
> turn
.tick
and
96 key_for_tick
(act_at
) == current_bucket_key
108 # Apply `self` to `game` logic.
109 fun apply
( game
: ThinGame ) is abstract
112 # Event raised at the first turn
117 # Game logic on the client
120 # Game tick when `self` should act.
123 var tick
: Int = 0 is protected writable
126 # Game turn on the client
127 class ThinGameTurn[G
: ThinGame]
129 # Game tick when `self` should act.
130 var tick
: Int is protected writable
132 # List of game events occured for `self`.
133 var events
= new List[GameEvent] is protected writable
136 # Game turn on the full logic
137 class GameTurn[G
: Game]
138 super ThinGameTurn[G
]
140 # Game that `self` belongs to.
143 # Create a new game turn for `game`.
144 init (game
: G
) is old_style_init
do
149 # Insert the Bucketable event `e` to be executed at next tick.
150 fun act_next
(e
: Bucketable[G
]) do game
.buckets
.add_at
(e
, tick
+ 1)
152 # Insert the Bucketable event `e` to be executed at tick `t`.
153 fun act_in
(e
: Bucketable[G
], t
: Int) do game
.buckets
.add_at
(e
, tick
+ t
)
155 # Add and `apply` a game `event`.
156 fun add_event
( event
: GameEvent )
167 # Game type used in this implementation.
170 # Bucket list in this game.
171 var buckets
: Buckets[G
] = new Buckets[G
]
173 # Last turn executed in this game
174 # Can be used to consult the latest events (by the display for example),
175 # but cannot be used to add new Events.
176 var last_turn
: nullable ThinGameTurn[G
] = null
178 # Execute and return a new GameTurn.
180 # This method calls `do_pre_turn` before executing the GameTurn
181 # and `do_post_turn` after.
182 fun do_turn
: GameTurn[G
]
184 var turn
= new GameTurn[G
](self)
187 buckets
.do_turn
(turn
)
197 # Execute something before executing the current GameTurn.
199 # Should be redefined by clients to add a pre-turn behavior.
200 # See `Game::do_turn`.
201 fun do_pre_turn
(turn
: GameTurn[G
]) do end
203 # Execute something after executing the current GameTurn.
205 # Should be redefined by clients to add a post-turn behavior.
206 # See `Game::do_turn`.
207 fun do_post_turn
(turn
: GameTurn[G
]) do end