1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Player related and tank spawning logic
16 module players
is serialize
22 # All the known players in the game
23 var players
= new Array[Player]
28 for player
in players
do
37 # Range around the center of the world (0, 0) where a tank can spawn
38 var spawn_range
= 256.0
40 # Spawn a new tank for `player`
41 fun spawn_tank
(player
: Player)
43 var pos
= new Pos(spawn_range
.rand
, spawn_range
.rand
)
44 var tank
= new Tank(game
.story
.tanks
.rand
, pos
, 2.0*pi
.rand
)
46 if tank
.next_move_collisions
(self).not_empty
then
48 game
.world
.explode
(self, pos
, 3)
51 add
new TankSpawnEvent(tank
, player
)
55 # A player in the game
59 # Queue of orders to apply at the end of the turn
60 var orders
= new Array[TOrder]
62 # The tank controlled by this player, if any
63 var tank
: nullable Tank = null
65 # Index of the "unique" player stencil applied on all its tanks
66 var stencil_index
: Int do
67 var counter
= once
new Ref[Int](0)
68 var val
= counter
.item
69 counter
.item
= (counter
.item
+1) % 4
73 redef fun do_turn
(turn
)
75 # Apply orders if they are legal
76 for order
in orders
do
77 if order
.is_legal
(turn
.game
, self) then
79 else print
"Server Warning: Order {order} is now illegal"
87 # The player controlling this tank, if any
88 var player
: nullable Player = null
93 # Is this order (still) legal?
95 # This is executed client-side.
96 fun is_legal
(game
: TGame, issed_by
: Player): Bool do return true
99 redef abstract class TankOrder
100 redef fun is_legal
(game
, issed_by
) do return issed_by
== tank
.player
103 # A request to spawn a new tank
110 redef fun is_legal
(turn
, issed_by
) do return issed_by
== player
and player
.tank
== null
112 redef fun apply
(turn
)
114 turn
.spawn_tank player
118 # A new tank appeared
126 var player
: nullable Player
128 redef fun apply
(game
)
131 if player
!= null then player
.tank
= tank
137 redef class TankDeathEvent
139 redef fun apply
(game
)
143 # `player` has no tank anymore
144 var player
= tank
.player
145 if player
!= null then