examples: remove mnit_dino
[nit.git] / examples / mnit_ballz / src / objects.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 # Module containing all objects of the game
16 module objects
17
18 import geometry
19 import geometry::polygon
20 import geometry::boxes
21 import collision
22
23 # The ball is the main character of the game!
24 class Ball
25 # Center of the ball
26 var center: Point[Float] is writable
27 end
28
29 # Walls make the ball bounce on them
30 class Wall
31 # Coordinates of the center of the wall
32 var center: Point[Float]
33
34 # Angle in radian
35 var angle: Float
36
37 # Scale for drawing `self`
38 var scale: Float
39
40 # Width of `self`
41 var width: Float is noautoinit
42
43 # Height of `self`
44 var height: Float is noautoinit
45
46 # Lines composing `self`
47 var lines: Array[OrientedLine] is noautoinit
48
49 # Initialize `self` with all its lines from `center` and `angle`
50 init do
51 self.height = 128.0
52 self.width = 32.0
53 var i = new Point[Float](0.0, 0.0)
54 var j = new Point[Float](0.0, 0.0)
55
56 var a = new Point[Float]((center.x - width/2.0), center.y)
57 var b = new Point[Float]((center.x), (center.y - height/2.0))
58 var c = new Point[Float]((center.x + width/2.0), center.y)
59 var d = new Point[Float]((center.x), (center.y + height/2.0))
60
61 var l1 = new OrientedLine(i, j, angle - pi/2.0, height * scale.to_f, rotate_point(a, center, angle))
62 var l2 = new OrientedLine(i, j, angle, width * scale.to_f, rotate_point(b, center, angle))
63 var l3 = new OrientedLine(i, j, angle - pi/2.0, height * scale.to_f, rotate_point(c, center, angle))
64 var l4 = new OrientedLine(i, j, angle, width * scale.to_f, rotate_point(d, center, angle))
65 lines = new Array[OrientedLine]
66 lines.add_all([l1, l2, l3, l4])
67 end
68 end
69
70 # A line represented with a center and an angle
71 class OrientedLine
72 super Line[Float]
73 redef type P: Point[Float]
74
75 # Angle in radian
76 var angle: Float is writable
77
78 # Length
79 var length: Float
80
81 # Center
82 var center: Point[Float]
83
84 redef fun point_left
85 do
86 var luv = unit_vector(new Point[Float](angle.cos, angle.sin))
87 var offset_from_center = new Point[Float](luv.x * (length / 2.0), luv.y * (length / 2.0))
88 return new Point[Float](center.x + offset_from_center.x, center.y + offset_from_center.y)
89 end
90
91 redef fun point_right
92 do
93 var luv = unit_vector(new Point[Float](angle.cos, angle.sin))
94 var offset_from_center = new Point[Float](luv.x * length / 2.0, luv.y * length / 2.0)
95 return new Point[Float](center.x - offset_from_center.x, center.y - offset_from_center.y)
96 end
97
98 redef fun left do return point_left.x.min(point_right.x)
99 redef fun right do return point_left.x.max(point_right.x)
100 end