Fixed symlinks. Added quit event to demos.
[luagame.git] / base / Object.lua
blob88bd51097c4dcaf2dc43f4b777fc4b2196c7679e
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 --movement
11 Object.angle = 0 --angle in degrees
12 Object.speed = 0 --speed in pixels
14 --rotation
15 Object.rotation = 0
16 Object.angular_velocity = 0
18 --global physics stuff
19 Object.falling_speed = 0 -- the current "falling" speed of an Object
20 Object.gravity = false --whether an Object is affected by gravity
21 Object.wind = false --whether an Object is affected by wind
22 Object.weight = nil --weight of the object - used for wind calculation
24 Object.image = nil --this should hold the image of the enemy
25 Object.rects = nil --this field should hold the collision Rect objects
27 Object.type = "Object" --for each class, this should be set to a unique identifier. it is used in collision detection
29 Object.max_updates = -1 --if set to a non-negative, non-zero number, object will automatically collect itself after this many updates
30 Object.num_updates = 0 --current count of updates
32 Object.death_anim = false --setting this to true will prevent it from colliding
33 Object.collect = false --set to true to cause resource manager to collect this object
35 function Object:new(o)
36 o = o or {}
37 setmetatable(o, self)
38 self.__index = self
39 self.rect = ObjectList:new()
40 return o
41 end
43 --updates the object's state
44 function Object:update(delta)
45 if delta ~= nil then
46 delta = delta/1000
47 end
49 self.x = self.x + math.cos(math.rad(self.angle)) * (self.speed * (delta or 1))
50 self.y = self.y + math.sin(math.rad(-1 * self.angle)) * (self.speed * (delta or 1))
51 self.rotation = (self.rotation + (self.angular_velocity * (delta or 1))) % 360
53 --gravity calculations
54 if(self.gravity == true and gravity_strength ~= 0 and gravity_strength ~= nil and gravity_angle ~= nil) then
55 self.x = self.x + math.cos(math.rad(gravity_angle)) * self.falling_speed * (delta or 1)
56 self.y = self.y + math.sin(math.rad(-1 * gravity_angle)) * self.falling_speed * (delta or 1)
58 --updating after calculations allows for collision to work properly
59 self.falling_speed = self.falling_speed + gravity_strength
60 end
63 if(self.wind == true and wind_strength ~= 0 and wind_strength ~= nil and wind_angle ~= nil and self.weight ~= nil) then
64 self.x = self.x + ((math.cos(math.rad(wind_angle)) * wind_strength) / self.weight) * (delta or 1)
65 self.y = self.y + ((math.sin(math.rad(-1 * wind_angle)) * wind_strength) / self.weight) * (delta or 1)
66 end
68 --Object self-collect check (mostly for particle systems)
69 if self.max_updates > 0 then
70 self.num_updates = self.num_updates + 1
71 if self.num_updates >= self.max_updates then
72 self.collect = true
73 end
74 end
76 end
78 --default draw routine
79 function Object:draw()
80 --last option is to re-center after rotating
81 blit(self.image, self.x, self.y, self.rotation, true)
82 end
84 --there is no predefined collision behavior
85 -- "ids" is the list of Rect ids that have been collided with
86 -- "type" is the type of the Object that is colliding
87 function Object:collide(type, ids)
89 end
91 --returns the coordinate of the center
92 function Object:get_center()
93 return (self.x + self.w)/2, (self.y + self.h)/2
94 end