Fixed oversights in collision code. More sound control.
[luagame.git] / demos / tutorial / scripts / MyObj.lua
blob536515e70e8065f8af2845b95c947774ef8622b2
1 MyObj = Object:new()
3 MyObj.type = "MyObj"
4 MyObj.rects = ObjectList:new()
5 MyObj.image, MyObj.w, MyObj.h = get_image("data/images/wikilogo.png")
7 function MyObj:new(o)
8 o = o or {}
9 setmetatable(o, self)
10 self.__index = self
11 return o
12 end
14 function MyObj:reflect_x()
15 self.angle = (-1 * self.angle) + 180
16 self.angle = self.angle % 360
17 end
19 function MyObj:reflect_y()
20 self.angle = -1 * self.angle
21 self.angle = self.angle % 360
22 end
24 function MyObj:update(delta)
25 Object.update(self, delta)
27 --this code is what makes it bounce
28 local cx, cy = Object.get_center(self)
30 if cx <= 0 then
31 self.x = 0 - math.floor(self.w/2)
32 self:reflect_x()
33 end
35 if cx >= s_width then
36 self.x = s_width - math.floor(self.w/2)
37 self:reflect_x()
38 end
40 if cy <= 0 then
41 self.y = 0 - math.floor(self.h/2)
42 self:reflect_y()
43 end
45 if cy >= s_height then
46 self.y = s_height - math.floor(self.h/2)
47 self:reflect_y()
48 end
49 end
51 --silly
52 function MyObj:collide(ids, object)
53 print("Ouch! You hit me, "..object.type.." at "..os.time().."!")
54 end