scene2d :: Sprite :: defaultinit
# The basic atomic living and moving object.
#
# A sprite has a position and a velocity
class Sprite
super LiveObject
# x coordinate of the center point
var x: Int = 0 is writable
# y coordinate of the center point
var y: Int = 0 is writable
# width of the sprite
var width: Int = 100 is writable
# height of the sprite
var height: Int = 100 is writable
# X coordinate of left side.
fun left: Int do return x - width/2
# X coordinate of right side.
fun right: Int do return x + width/2
# Y coordinate of top.
fun top: Int do return y - height/2
# Y coordinate of bottom.
fun bottom: Int do return y + height/2
# x velocity (applied by `update')
var vx: Int = 0 is writable
# y velocity (applied by `update')
var vy: Int = 0 is writable
redef fun update
do
self.x += self.vx
self.y += self.vy
end
redef fun draw(view) do view.draw_sprite(self)
# Is self overlaps (or contains) an other sprite
# `x', `y', `width', and `height' of both sprites are considered
fun overlaps(other: Sprite): Bool
do
return self.right > other.left and self.left < other.right and self.bottom > other.top and self.top < other.bottom
end
# Return the current angle of velocity
# Often used to rotate the displayed image with the correct angle
fun velocity_angle: Float
do
return atan2(self.vx.to_f, -self.vy.to_f)
end
# Return the angle to target an other sprite
fun angle_to(target: Sprite): Float
do
return atan2((target.x-self.x).to_f, (self.y-target.y).to_f)
end
# Update of vx and vy toward a given angle and magnitude
fun set_velocity(angle: Float, maginude: Int)
do
var magf = maginude.to_f
self.vx = (angle.sin * magf).to_i
self.vy = (angle.cos * -magf).to_i
end
end
lib/scene2d/scene2d.nit:33,1--105,3