d7e54a4e68822c125c9ef67a299960c0e9a2767a
[nit.git] / examples / mnit_dino / src / graphism.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Graphism for the Dino game
18 # Uses the Display interface from MNit.
19 module graphism
20
21 import mnit # for Display
22
23 import game_logic
24
25 redef class GamePos
26 fun to_screen( d : Display ) : ScreenPos
27 do
28 var x = x+d.width/2
29 var y = d.height/2-y
30 return new ScreenPos( x.to_f, y.to_f )
31 end
32 end
33
34 class ScreenPos
35 var x : Float
36 var y : Float
37
38 fun to_game( d : Display ) : GamePos
39 do
40 var x = x.to_i-d.width/2
41 var y = d.height/2-y.to_i
42 return new GamePos( x, y )
43 end
44 end
45
46 redef class Entity
47 fun draw( display : Display, imgs : ImageSet, turn : Turn ) is abstract
48 end
49
50 redef class Dino
51 # Notice: This method only implements the basic drawing feature, it is
52 # redefed in `fancy_dino`. If the `fancy_dino` module is imported by
53 # user methods, this code is dead and will never be executed.
54 redef fun draw( display, imgs, turn )
55 do
56 var spos = pos.to_screen( display )
57 var img : Image
58 if is_alive then
59 img = imgs.dino_img
60 else
61 img = imgs.dino_dead_img
62 end
63 display.blit_centered( img, spos.x.to_i, spos.y.to_i )
64 end
65 end
66
67 redef class Caveman
68 redef fun draw( display, imgs, turn )
69 do
70 var man_pos = pos.to_screen( display )
71 var img : Image
72
73 if not is_alive then
74 img = imgs.blood_img
75 else if is_afraid( turn ) then
76 img = imgs.caveman_afraid_img
77 else if can_throw( turn ) then
78 img = imgs.caveman_ready_img
79 else
80 img = imgs.caveman_img
81 end
82
83 display.blit_centered( img, man_pos.x.to_i, man_pos.y.to_i )
84 end
85 end
86
87 redef class Javelin
88 redef fun draw( display, imgs, turn )
89 do
90 var spos = pos.to_screen( display )
91 spos.y -= z.to_f/10.0
92 display.blit_rotated( imgs.javelin_img, spos.x, spos.y, angle )
93 end
94 end
95
96 redef class Bush
97 redef fun draw( display, imgs, turn )
98 do
99 var spos = pos.to_screen( display )
100 var img = imgs.bush_img
101 display.blit_centered( img, spos.x.to_i, spos.y.to_i )
102 end
103 end
104
105 class ImageSet
106 var javelin_img : Image
107
108 var dino_img : Image
109 var dino_dead_img : Image
110 var dino_shadow : Image
111
112 var caveman_img : Image
113 var caveman_afraid_img : Image
114 var caveman_ready_img : Image
115 var blood_img : Image
116 var bush_img : Image
117
118 var life_img : Image
119 var life_empty_img : Image
120
121 var you_won_img : Image
122 var you_lost_img : Image
123 var start_over_img : Image
124 fun start_over_path : String is abstract
125
126 var numbers: NumberImages
127
128 init ( app : App )
129 do
130 javelin_img = app.load_image( "images/javelin.png" )
131
132 dino_img = app.load_image( "images/dino.png" )
133 dino_dead_img = app.load_image( "images/dino_dead.png" )
134 dino_shadow = app.load_image( "images/shadow.png" )
135
136 caveman_img = app.load_image( "images/caveman.png" )
137 caveman_afraid_img = app.load_image( "images/caveman_afraid.png" )
138 caveman_ready_img = app.load_image( "images/caveman_ready.png" )
139 blood_img = app.load_image( "images/blood.png" )
140 bush_img = app.load_image( "images/bush.png" )
141
142 life_img = app.load_image( "images/life.png" )
143 life_empty_img = app.load_image( "images/life_empty.png" )
144
145 you_won_img = app.load_image( "images/you_won.png" )
146 you_lost_img = app.load_image( "images/you_lost.png" )
147 start_over_img = app.load_image( start_over_path )
148
149 numbers = app.load_numbers("images/#.png")
150 end
151 end
152
153 redef class Game
154 fun draw( display : Display, imgs : ImageSet, turn : Turn )
155 do
156 display.clear(0.05, 0.45, 0.1)
157
158 # entities (dino, cavemen and javelins)
159 for e in entities do
160 e.draw( display, imgs, turn )
161 end
162
163 # life
164 var life_out_of_ten = 10 * dino.life / dino.total_life
165 if dino.life*10 % dino.total_life != 0 then
166 life_out_of_ten += 1
167 end
168
169 for life in [0..life_out_of_ten[ do
170 display.blit_centered( imgs.life_img, display.width*(life+1)/11, display.top_offset )
171 end
172
173 for empty in [life_out_of_ten..10[ do
174 display.blit_centered( imgs.life_empty_img, display.width*(empty+1)/11, display.top_offset )
175 end
176
177 # display score
178 display.blit_number(imgs.numbers, score.item, display.width/11, display.top_offset+32)
179
180 # game over messages
181 if over then
182 var concl_img : Image
183 if won then
184 concl_img = imgs.you_won_img
185 else
186 concl_img = imgs.you_lost_img
187 end
188 display.blit_centered( concl_img, display.width/2, 60+display.top_offset )
189
190 if ready_to_start_over then
191 display.blit_centered( imgs.start_over_img, display.width/2, 100+display.top_offset )
192 end
193 end
194 end
195 end
196
197 redef interface Display
198 fun top_offset: Int do return 48
199 end