1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Module containing all objects of the game
19 import geometry
::polygon
20 import geometry
::boxes
23 # The ball is the main character of the game!
26 var center
: Point[Float] is writable
29 # Walls make the ball bounce on them
31 # Coordinates of the center of the wall
32 var center
: Point[Float]
37 # Scale for drawing `self`
41 var width
: Float is noautoinit
44 var height
: Float is noautoinit
46 # Lines composing `self`
47 var lines
: Array[OrientedLine] is noautoinit
49 # Initialize `self` with all its lines from `center` and `angle`
53 var i
= new Point[Float](0.0, 0.0)
54 var j
= new Point[Float](0.0, 0.0)
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))
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
])
70 # A line represented with a center and an angle
73 redef type P
: Point[Float]
76 var angle
: Float is writable
82 var center
: Point[Float]
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
)
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
)
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
)