1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012-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 # Graphism for the Dino game
18 # Uses the Display interface from MNit.
21 import mnit
# for Display
26 fun to_screen
( d
: Display ) : ScreenPos
30 return new ScreenPos( x
.to_f
, y
.to_f
)
38 fun to_game
( d
: Display ) : GamePos
40 var x
= x
.to_i-d
.width
/2
41 var y
= d
.height
/2-y
.to_i
42 return new GamePos( x
, y
)
47 fun draw
( display
: Display, imgs
: ImageSet, turn
: Turn ) is abstract
51 redef fun draw
( display
, imgs
, turn
)
53 var spos
= pos
.to_screen
( display
)
58 img
= imgs
.dino_dead_img
60 display
.blit_centered
( img
, spos
.x
.to_i
, spos
.y
.to_i
)
65 redef fun draw
( display
, imgs
, turn
)
67 var man_pos
= pos
.to_screen
( display
)
72 else if is_afraid
( turn
) then
73 img
= imgs
.caveman_afraid_img
74 else if can_throw
( turn
) then
75 img
= imgs
.caveman_ready_img
77 img
= imgs
.caveman_img
80 display
.blit_centered
( img
, man_pos
.x
.to_i
, man_pos
.y
.to_i
)
85 redef fun draw
( display
, imgs
, turn
)
87 var spos
= pos
.to_screen
( display
)
89 display
.blit_rotated
( imgs
.javelin_img
, spos
.x
, spos
.y
, angle
)
94 redef fun draw
( display
, imgs
, turn
)
96 var spos
= pos
.to_screen
( display
)
97 var img
= imgs
.bush_img
98 display
.blit_centered
( img
, spos
.x
.to_i
, spos
.y
.to_i
)
103 var javelin_img
: Image
106 var dino_dead_img
: Image
108 var caveman_img
: Image
109 var caveman_afraid_img
: Image
110 var caveman_ready_img
: Image
111 var blood_img
: Image
115 var life_empty_img
: Image
117 var you_won_img
: Image
118 var you_lost_img
: Image
119 var start_over_img
: Image
120 fun start_over_path
: String is abstract
122 var numbers
: NumberImages
126 javelin_img
= app
.load_image
( "images/javelin.png" )
128 dino_img
= app
.load_image
( "images/dino.png" )
129 dino_dead_img
= app
.load_image
( "images/dino_dead.png" )
131 caveman_img
= app
.load_image
( "images/caveman.png" )
132 caveman_afraid_img
= app
.load_image
( "images/caveman_afraid.png" )
133 caveman_ready_img
= app
.load_image
( "images/caveman_ready.png" )
134 blood_img
= app
.load_image
( "images/blood.png" )
135 bush_img
= app
.load_image
( "images/bush.png" )
137 life_img
= app
.load_image
( "images/life.png" )
138 life_empty_img
= app
.load_image
( "images/life_empty.png" )
140 you_won_img
= app
.load_image
( "images/you_won.png" )
141 you_lost_img
= app
.load_image
( "images/you_lost.png" )
142 start_over_img
= app
.load_image
( start_over_path
)
144 numbers
= app
.load_numbers
("images/#.png")
149 fun draw
( display
: Display, imgs
: ImageSet, turn
: Turn )
151 display
.clear
(0.05, 0.45, 0.1)
153 # entities (dino, cavemen and javelins)
155 e
.draw
( display
, imgs
, turn
)
159 var life_out_of_ten
= 10 * dino
.life
/ dino
.total_life
160 if dino
.life
*10 % dino
.total_life
!= 0 then
164 for life
in [0..life_out_of_ten
[ do
165 display
.blit_centered
( imgs
.life_img
, display
.width
*(life
+1)/11, display
.top_offset
)
168 for empty
in [life_out_of_ten
..10[ do
169 display
.blit_centered
( imgs
.life_empty_img
, display
.width
*(empty
+1)/11, display
.top_offset
)
173 display
.blit_number
(imgs
.numbers
, score
.item
, display
.width
/11, display
.top_offset
+32)
177 var concl_img
: Image
179 concl_img
= imgs
.you_won_img
181 concl_img
= imgs
.you_lost_img
183 display
.blit_centered
( concl_img
, display
.width
/2, 60+display
.top_offset
)
185 if ready_to_start_over
then
186 display
.blit_centered
( imgs
.start_over_img
, display
.width
/2, 100+display
.top_offset
)
192 redef interface Display
193 fun top_offset
: Int do return 48