New collision code! Minor doc changes.
[luagame.git] / base / Object.lua
blob449844324bc3cecb8c40ee478f07a957d5a55d1a
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.angle = 0 --angle in degrees
15 Object.speed = 0 --speed in pixels
17 --rotation
18 Object.rotation = 0
19 Object.angular_velocity = 0
21 --global physics stuff
22 Object.falling_speed = 0 -- the current "falling" speed of an Object
23 Object.gravity = false --whether an Object is affected by gravity
24 Object.wind = false --whether an Object is affected by wind
25 Object.weight = nil --weight of the object - used for wind calculation
27 Object.image = nil --this should hold the image of the Object
28 Object.rects = ObjectList:new() --this field should hold the collision Rect objects
30 Object.type = "Object" --for each class, this should be set to a unique identifier. it is used in collision detection
32 Object.max_updates = -1 --if set to a non-negative, non-zero number, object will automatically collect itself after this many updates
33 Object.num_updates = 0 --current count of updates
35 Object.death_anim = false --setting this to true will prevent it from colliding
36 Object.collect = false --set to true to cause resource manager to collect this object
38 function Object:new(o)
39 o = o or {}
40 setmetatable(o, self)
41 self.__index = self
42 self.rect = ObjectList:new()
43 return o
44 end
46 --updates the object's state
47 function Object:update(delta)
48 if delta ~= nil then
49 delta = delta/1000
50 end
52 self.x = self.x + math.cos(math.rad(self.angle)) * (self.speed * (delta or 1))
53 self.y = self.y + math.sin(math.rad(-1 * self.angle)) * (self.speed * (delta or 1))
54 self.rotation = (self.rotation + (self.angular_velocity * (delta or 1))) % 360
56 --gravity calculations
57 if(self.gravity == true and gravity_strength ~= 0 and gravity_strength ~= nil and gravity_angle ~= nil) then
58 self.x = self.x + math.cos(math.rad(gravity_angle)) * self.falling_speed * (delta or 1)
59 self.y = self.y + math.sin(math.rad(-1 * gravity_angle)) * self.falling_speed * (delta or 1)
61 --updating after calculations allows for collision to work properly
62 self.falling_speed = self.falling_speed + gravity_strength
63 end
66 if(self.wind == true and wind_strength ~= 0 and wind_strength ~= nil and wind_angle ~= nil and self.weight ~= nil) then
67 self.x = self.x + ((math.cos(math.rad(wind_angle)) * wind_strength) / self.weight) * (delta or 1)
68 self.y = self.y + ((math.sin(math.rad(-1 * wind_angle)) * wind_strength) / self.weight) * (delta or 1)
69 end
71 --Object self-collect check (mostly for particle systems)
72 if self.max_updates > 0 then
73 self.num_updates = self.num_updates + 1
74 if self.num_updates >= self.max_updates then
75 self.collect = true
76 end
77 end
79 end
81 --default draw routine
82 function Object:draw()
83 --last option is to re-center after rotating
84 blit(self.image, self.x-self.x_offset, self.y-self.y_offset, self.rotation, true)
85 end
87 --there is no predefined collision behavior
88 -- "ids" is the list of Rect ids that have been collided with
89 -- "type" is the type of the Object that is colliding
90 function Object:collide(ids, type)
92 end
94 --returns the coordinate of the center
95 function Object:get_center()
96 return math.floor(self.x + (self.w/2)), math.floor(self.y + (self.h/2))
97 end
99 --sets the drawing offset of the sprite
100 function Object:set_origin(x,y)
101 self.x_offset = x
102 self.y_offset = y