*: remove newly superfluous static types on attributes
[nit.git] / contrib / tinks / src / game / framework.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Structure of a client/server game, based on turns, events, orders and rules
16 module framework is serialize
17
18 import geometry
19 import realtime
20
21 # Main game object containing the world, players and tanks
22 #
23 # The game object is shared by both the client and the server.
24 # So a method using the game object as visitor is to be executed client-side.
25 class TGame
26
27 # `Story`, or rule book, with all the stats for this game
28 var story = new Story
29
30 # `Clock` timing the elapsed time between turns
31 private var clock = new Clock is noserialize
32
33 # Tick count of the last turn (The first turn as a tick of 0)
34 var tick = -1
35
36 # Execute the next turn and return it as a `TTurn`
37 #
38 # This is to be executed server-side only.
39 fun do_turn: TTurn
40 do
41 var dt = clock.lapse
42 tick += 1
43
44 var turn = new TTurn(self, tick, dt, ((dt-dt.floor)*1000.0).to_i)
45 return turn
46 end
47
48 # Apply `turn` locally by updating `tick` and applying all events
49 #
50 # This is to be executed client-side only.
51 fun apply_turn(turn: TTurn)
52 do
53 tick = turn.tick
54 for event in turn.events do event.apply self
55 end
56 end
57
58 # A single turn of a `TGame`
59 #
60 # The turn object is created and populated by the server (using `TGame::do_turn`).
61 # It is transmitted to the client but it cannot modify it.
62 # So methods using the turn object as visitor are to be executed server-side.
63 class TTurn
64
65 # `TGame` of which `self` is part of
66 var game: TGame
67
68 # Tick of this turn
69 var tick: Int
70
71 # Elapsed seconds since previous turn (as a `Float`)
72 var dts: Float
73
74 # Elapsed milliseconds since previous turn (as a `Int`)
75 var dt: Int
76
77 # `TEvent` that happened during this turn
78 #
79 # Events are added using `add`.
80 # This information is used to apply the turn client-side to update its game object.
81 # It is also used by effects on the UI and could be used by an AI.
82 var events: SequenceRead[TEvent] = new Array[TEvent]
83
84 # Add an `event` to `events` and apply it right away server-side
85 fun add(event: TEvent)
86 do
87 event.apply game
88 events.as(Array[TEvent]).add event
89 end
90 end
91
92 # Game event sent from the server to the client
93 class TEvent
94
95 # Executed client-side to apply this event on the `game`
96 fun apply(game: TGame) do end
97 end
98
99 # An order sent from the client to the server
100 class TOrder
101
102 # Apply order server-side on `turn`, usually spawns `GameEvent`
103 fun apply(turn: TTurn) do end
104 end
105
106 # An entity acting on each turn
107 class TTurnable
108
109 # Act on `turn`
110 fun do_turn(turn: TTurn) do end
111 end
112
113 # A collection of `Rule` guiding the game
114 #
115 # Changing the story could (in theory) be enough to completely change the context of the game.
116 # In _Tinks!_ however, we use this class lightly and fill it with static content.
117 class Story
118 end
119
120 # Metadata of in game `Ruled` entities, keep their stats and assets in a single place
121 class Rule
122
123 # `Story` to which `self` belongs
124 var story: Story
125 end
126
127 # A game entity with metadata in its `rule`
128 class Ruled
129
130 # Kind of `Rule` for `rule`
131 type R: Rule
132
133 # Metadata of this entity
134 var rule: R
135 end
136
137 # Should the game show more information for debugging?
138 fun debug: Bool do return false