0a486b3c16caf518c17f09d1d2736358a4828325
[nit.git] / examples / mnit_dino / src / fancy_dino.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 # Improves dino appearance
18 module fancy_dino
19
20 import graphism
21
22 redef class Dino
23 var turning_angle = 0.0
24 var turning = false
25 var was_going_left = false
26
27 var turning_step = 0.1
28
29 redef fun do_turn( turn )
30 do
31 super
32
33 if not turning then
34 if going_left != was_going_left then
35 turning = true
36
37 if going_left then
38 turning_angle = 0.0
39 else # going right
40 turning_angle = pi
41 end
42 end
43 end
44
45 was_going_left = going_left
46 end
47
48 redef fun draw( display, imgs, turn )
49 do
50 var spos = pos.to_screen( display )
51 var img : Image
52 if is_alive then
53 img = imgs.dino_img
54 else
55 img = imgs.dino_dead_img
56 end
57 var img_radius = (img.width/2).to_f
58
59 display.blit_centered( imgs.dino_shadow, spos.x.to_i, spos.y.to_i )
60
61 if turning then
62 if going_left then
63 turning_angle += turning_step
64 else
65 turning_angle -= turning_step
66 end
67
68 var adx = turning_angle.cos * img_radius
69 var ady = turning_angle.sin * img_radius # * 0.3
70
71 var dx = spos.x - adx
72 var dy = spos.y - ady - (img.height/2).to_f
73 var ax = spos.x + adx
74 var ay = spos.y + ady - (img.height/2).to_f
75 var bx = ax
76 var by = ay + img.height.to_f
77 var cx = dx
78 var cy = dy + img.height.to_f
79
80 display.blit_stretched( img, ax, ay, bx, by, cx, cy, dx, dy )
81
82 if (going_left and turning_angle >= pi) or
83 (going_right and turning_angle <= 0.0)then
84 turning = false
85 end
86 else
87 var dx = spos.x + img_radius
88 var dy = spos.y - (img.height/2).to_f
89 var ax = spos.x - img_radius
90 var ay = dy
91 var bx = ax
92 var by = dy + img.height.to_f
93 var cx = dx
94 var cy = by
95
96 if going_left then
97 display.blit_stretched( img, ax, ay, bx, by, cx, cy, dx, dy )
98 else
99 display.blit_stretched( img, dx, dy, cx, cy, bx, by, ax, ay )
100 end
101 end
102 end
103 end