src: transform all old writable in annotations
[nit.git] / examples / leapfrog / leapfrog.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 # game of leapfrog: be a sheep and avoid the duck to grab apples
16 #
17 # This module is an example of a simple game using a curses backend
18 module leapfrog
19
20 import scene2d
21
22 # A falling apple
23 # If the sheep grab it, it scores one point.
24 class Apple
25 super Sprite
26 end
27
28 # Common class for the sheep and the duck
29 class Animal
30 super Sprite
31
32 # Is the sprite stunned?
33 # 0 if no, >0 if stunned.
34 # The value indicate the number of step that remain to be stunt
35 #
36 # If a animal is stunned, it cannot move horizontally
37 var stunt_ttl: Int = 0 is writable
38
39 # Common update for animal
40 # handle stunt and edge collision
41 redef fun update
42 do
43 if stunt_ttl > 0 then
44 stunt_ttl -= 1
45 else
46 self.x += self.vx
47 end
48 self.y += self.vy
49
50 if self.x + self.width > 7900 then
51 self.vx = -self.vx
52 self.x = 7900 - self.width
53 else if self.x < 0 then
54 self.vx = -self.vx
55 self.x = 0
56 end
57 end
58 end
59
60 # The player sprite
61 class Sheep
62 super Animal
63
64 # Which frame to show when walking
65 var leg_state: Int = 0
66
67 # Is the sheep currently jumping (or falling)
68 var is_jumping = false
69
70 init
71 do
72 self.y = 2000 - 200
73 self.vx = 100
74 self.width = 600
75 self.height = 200
76 end
77
78 redef fun update
79 do
80 super
81
82 if is_jumping then
83 if self.y + self.height > 2000 then
84 is_jumping = false
85 self.vy = 0
86 self.y = 2000 - self.height
87 else
88 self.vy += 10 # gravity
89 end
90 end
91 if not is_jumping then
92 self.leg_state = 1 - self.leg_state # change leg
93 end
94 end
95
96 # Try to jump is possible (ie if walking and not stunt)
97 fun jump
98 do
99 if is_jumping or stunt_ttl > 0 then return
100 self.vy = -100
101 self.is_jumping = true
102 end
103 end
104
105 # The nemesis of the sheep.
106 # It just go back and forth
107 class Duck
108 super Animal
109 init
110 do
111 self.y = 2000 - 200
112 self.x = 7900 - 400
113 self.vx = -80
114 self.width = 400
115 self.height = 200
116 end
117 end
118
119 class PlayScene
120 super Scene
121
122 var apples = new LiveGroup[Apple]
123 var duck = new Duck
124 var sheep = new Sheep
125
126 var score = 0
127
128 var sprites = new LiveGroup[LiveObject]
129
130 init
131 do
132 sprites.add(apples)
133 sprites.add(duck)
134 sprites.add(sheep)
135 end
136
137 redef fun update
138 do
139
140 # Call update on all sprites
141 sprites.update
142
143 # Need a new apple
144 if 10.rand < 2 then
145 var a = new Apple
146 a.x = (60.rand + 10) * 100
147 a.y = 0
148 a.vx = 0
149 a.vy = 70.rand + 30
150 apples.add(a)
151 end
152
153 # Eat apple or fallen apple
154 for a in apples do
155 if not a.exists then continue
156 if a.overlaps(sheep) then
157 score += 1
158 a.exists = false
159 end
160 if a.y > 2000 then
161 a.exists = false
162 end
163 end
164
165 # Sheep vs duck
166 if sheep.overlaps(duck) then
167 if sheep.is_jumping and sheep.vy > 0 then
168 duck.stunt_ttl = 5
169 sheep.vy = -150
170 else
171 if sheep.x < duck.x then
172 sheep.x = duck.x - sheep.width
173 else
174 sheep.x = duck.x + duck.width
175 end
176 sheep.vx = - sheep.vx
177 duck.vx = - duck.vx
178 end
179 end
180 end
181
182 end
183