You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
radium/radium/object.coffee

51 lines
1.2 KiB
CoffeeScript

10 years ago
# NOTE: All class methods are loosely bound (ie. -> instead of =>). This is to
# make sure that @properties refer to instances of Objects, rather than
# the defaults set in the Object class.
class Object
constructor: (@engine, @name) ->
@sprite = null
@instances = []
@x = 0
@y = 0
10 years ago
callEvent: (name, data = {}) ->
event_map =
mouseover: @onMouseOver
mouseout: @onMouseOut
10 years ago
create: @onCreate
step: @onStep
click: @onClick
click_global: @onClickGlobal
10 years ago
switch name
when "draw"
@drawSelf(data.surface ? "")
@onDraw?(data)
10 years ago
else event_map[name]?.bind(this)(data)
10 years ago
drawSelf: (surface) ->
@drawSprite(surface)
10 years ago
drawSprite: (surface = "") ->
@sprite.draw(@x, @y, {}, surface) if @sprite? and (@draw_sprite ? "true")
10 years ago
getBoundingBox: ->
image_size = @sprite?.getSize()
return {
x1: @x
x2: @x + image_size?.width
y1: @y
y2: @y + image_size?.height
}
getInstances: ->
return @instances
10 years ago
checkPointCollision: (x, y) ->
# TODO: Precision collision matching!
bounding_box = @getBoundingBox()
10 years ago
return x >= bounding_box?.x1 and x <= bounding_box?.x2 and y >= bounding_box?.y1 and y <= bounding_box?.y2