2 --all objects should descend from this
14 Object
.angle
= 0 --angle in degrees
15 Object
.speed
= 0 --speed in pixels
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
)
42 self
.rect
= ObjectList
:new()
46 --updates the object's state
47 function Object
:update(delta
)
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
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)
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
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)
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)
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))
99 --sets the drawing offset of the sprite
100 function Object
:set_origin(x
,y
)