Merge branch 'master' of https://github.com/privat/nit into nitdoc_merge
[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 if turning then
60 if going_left then
61 turning_angle += turning_step
62 else
63 turning_angle -= turning_step
64 end
65
66 var adx = turning_angle.cos * img_radius
67 var ady = turning_angle.sin * img_radius # * 0.3
68
69 var dx = spos.x - adx
70 var dy = spos.y - ady - (img.height/2).to_f
71 var ax = spos.x + adx
72 var ay = spos.y + ady - (img.height/2).to_f
73 var bx = ax
74 var by = ay + img.height.to_f
75 var cx = dx
76 var cy = dy + img.height.to_f
77
78 display.blit_stretched( img, ax, ay, bx, by, cx, cy, dx, dy )
79
80 if (going_left and turning_angle >= pi) or
81 (going_right and turning_angle <= 0.0)then
82 turning = false
83 end
84 else
85 var dx = spos.x + img_radius
86 var dy = spos.y - (img.height/2).to_f
87 var ax = spos.x - img_radius
88 var ay = dy
89 var bx = ax
90 var by = dy + img.height.to_f
91 var cx = dx
92 var cy = by
93
94 if going_left then
95 display.blit_stretched( img, ax, ay, bx, by, cx, cy, dx, dy )
96 else
97 display.blit_stretched( img, dx, dy, cx, cy, bx, by, ax, ay )
98 end
99 end
100 end
101 end