First commit of LuaGame with an OpenGL backend.
[luagame.git] / base / Object.lua
blobf2c091f070712bfbd23f00e607c9f1896002a01c
1 --the Object class
2 --all objects should descend from this
4 Object = {}
5 Object.x = 0
6 Object.y = 0
7 Object.w = 0
8 Object.h = 0
10 Object.x_offset = 0
11 Object.y_offset = 0
13 --movement
14 Object.heading = 0 --heading in degrees
15 Object.speed = 0 --speed in pixels
17 --rotation
18 Object.rotation = 0
19 Object.angular_velocity = 0
21 Object.image = nil --this should hold the image of the Object
22 Object.rects = ObjectList:new() --this field should hold the collision Rect objects
24 Object.type = "Object" --for each class, this should be set to a unique identifier. it is used in collision detection
26 Object.max_updates = -1 --if set to a non-negative, non-zero number, object will automatically collect itself after this many updates
27 Object.num_updates = 0 --current count of updates
29 Object.no_collide = false --setting this to true will prevent it from colliding
30 Object.collect = false --set to true to cause resource manager to collect this object
32 function Object:new(o)
33 o = o or {}
34 setmetatable(o, self)
35 self.__index = self
36 return o
37 end
39 --updates the object's state
40 function Object:update(delta)
41 if delta ~= nil then
42 delta = delta/1000
43 end
45 self.x = self.x + math.cos(math.rad(self.heading)) * (self.speed * (delta or 1))
46 self.y = self.y + math.sin(math.rad(-1 * self.heading)) * (self.speed * (delta or 1))
47 self.rotation = (self.rotation + (self.angular_velocity * (delta or 1))) % 360
49 --Object self-collect check (mostly for particle systems)
50 if self.max_updates > 0 then
51 self.num_updates = self.num_updates + 1
52 if self.num_updates >= self.max_updates then
53 self.collect = true
54 end
55 end
57 end
59 --default draw routine
60 function Object:draw()
61 display(self.image, self.x-self.x_offset, self.y-self.y_offset, self.rotation, 1, 1)
62 end
64 --there is no predefined collision behavior
65 -- "ids" is the list of Rect ids that have been collided with
66 -- "object" is the Object that is colliding with this one
67 function Object:collide(ids, object)
69 end
71 --returns the coordinate of the center
72 function Object:get_center()
73 return math.floor(self.x + (self.w/2)), math.floor(self.y + (self.h/2))
74 end
76 --sets the drawing offset of the sprite
77 function Object:set_origin(x,y)
78 self.x_offset = x
79 self.y_offset = y
80 end
82 --unloads resources and nils out the class' table
83 -- There is no defined behavior for this function in the Object class.
84 function Object:unload()
85 --normally, resources would be unloaded here
86 --and one would put a line such as
87 --ClassName = nil at the end
88 end