OBJECTS
At the beginning there's an empty world. Game characters, enemies, items and other objects can be loaded into that world. They are independent entities that can interact each other. For example, they can collide. Objects can be moved, and turned, to name only few actions.
PlayObject
Animates an object. The object must have been loaded with the LoadAnimObject-function. The first animation frame is zero. Using negative speed causes the animation run backwards. Optional Continue-parameter can be used to instruct animation to continue where it was when StopObject was called (this is the 'pause' trick).
The animation is played only once after which it stops and returns to the initial frame.
Please note that tilemaps hold an exception. They always animate from start to end, use zero to them both. But the main difference is the speed. The value of 1 means the animation is to be updated once in a second. 2 means every 2 seconds. 0.5 means twice a second, and so on.
Usage:
PlayObject obj [start] [, end] [, speed] [, continue]
Example:
'make screen a bit zoomed
SCREEN 250,188,0,cbsizable
FrameLimit 40 'limit game speed
'load animation for both directions, but hide them
zeroright=LoadAnimObject("Media\zerorun1.bmp",47,51,0,12)
MaskObject zeroright,cbmagenta
ShowObject zeroright,OFF
zeroleft=LoadAnimObject("Media\zerorun2.bmp",47,51,0,12)
MaskObject zeroleft,cbmagenta
ShowObject zeroleft,OFF
'default direction
zero=CloneObject(zeroright)
Color cbblack
AddText "Try arrows"
ClsColor cbwhite
'MAIN LOOP
Repeat
'run right!
If RightKey() Then
PaintObject zero,zeroright
PlayObject zero,2,11,0.25 'animate at speed 0.25
'run left!
ElseIf LeftKey() Then
PaintObject zero,zeroleft
PlayObject zero,2,11,0.25 'animate at speed 0.25
Else
'nothing is pressed so freeze
PlayObject zero,0
EndIf
Text 0,140,"Playing? "+ObjectPlaying(zero)
Text 0,160,"Frame: "+ObjectFrame(zero)
DrawScreen
Until EscapeKey()
LoopObject
The same as PlayObject, but animates the object in loop i.e. starts always over automatically. See PlayObject for more details of animation.
StopObject
Stops an object from animating. If you want to make an object stop immediately so it goes to the starting frame, prefer to use PlayObject obj, OFF.
Usage:
StopObject obj
ResetObjectCollision
Ignores all object collision events at that time. If you clone or make an object, it starts from coordinates 0,0. PositionObject moves it to another location. It might be good idea to reset object collision at that time, so the automatic collision response doesn't think it collided a wall, or something...
Usage:
ResetObjectCollision obj
SetupCollision
The automatic collision response for objects works so that you just define which objects can collide each other. Player-to-Players, for example. Or Players to maps. CoolBasic collision handling is very easy.
Colliding object |
-> |
To collide with |
method |
presentation |
|
-> |
|
sliding |
1,1,2 |
|
-> |
|
sliding |
1,4,2 |
|
-> |
|
stop (1) |
2,2,1 |
|
-> |
|
stop (1) |
2,4,1 |
For example, you want a circle->circle collision for players. You cant it to be sliding. Then you take a look at the able, and reason the presentation is 2,2,2. Collision for tilemaps should be set-up last.
Usage:
SetupCollision src_obj, dest_obj, src_type, dest_type, method
Example:
FrameLimit 40
'load map
map=LoadMap("Media\testmap.til","Media\negative.bmp")
'load player
guy=LoadObject("Media\guy.bmp",72)
PositionObject guy,0, -40
'load a neutral unit
fellow=LoadObject("Media\guy.bmp",72)
PositionObject fellow, -100, -40
'setup a circle-circle sliding collision to the fellow
SetupCollision guy,fellow,2,2,2
'setup a collision between the player and map
SetupCollision guy,map,1,4,2
Repeat
'Controls
If LeftKey() Then TurnObject guy,5
If RightKey() Then TurnObject guy, -5
If UpKey() Then MoveObject guy,3
If DownKey() Then MoveObject guy, -3
'camera follow
UpdateGame
CloneCameraPosition guy
DrawScreen
Until EscapeKey()
ClearCollisions
Empty the object collision list. No collisions anymore on any object.
InitObjectList
Prepare to iterate all objects. See NectObject for example.
NextObject
This command allows you to go through of ALL objects in the game world. See the example:
Example:
'Create 9 cows and position them into a row
For i=0 To 7
obj=LoadObject("Media\cow.bmp")
PositionObject obj,( -4+i)*40,0
Next i
'enable shapes to be rendered into world coordinates
DrawToWorld ON
ClsColor cbdarkred
InitObjectList 'reset the object iteration
Repeat
If TIMER()>interv+1000 Then
current=NextObject()
If current
drawx=ObjectX(current)
drawy=ObjectY(current)
Else
'all objects have been iterated, start all over
InitObjectList
EndIf
interv=TIMER()
EndIf
Color cbpink
Circle drawx-20,drawy+20,40,OFF
Text 0,0,current
DrawScreen
Until EscapeKey()
LoadObject
This function loads an image from hard disk and makes an object out of it. Supported file formats are bmp, jpg, png and tga.
Objects are characters, items, enemies or power-ups that can be loaded into game world. They can be positioned, moved, rotated, pointed, animated, textured and destroyed. They can also collide each other. Also tilemaps and particle emitters are objects, and you can use the same object handling commands for them.
Each object possess a number of attributes ex. angle, position, transparency and so on. Their angle, for example, determines which direction they point and where they move.
If you load an object that you wish to be able to rotate, give it an optional parameter that determines how smoothly it can be rendered in different angles. Basically this defined how many pointing directions an object can have. Thus, the value of 8 means an object can point to 8 different directions depending on its angle. The value should be at range of 1-360. The greater the value is the smoother it turns. However, be careful with great rotation details since large objects take ages to load – and eat a lot of memory. If your object turns, say, 5 degrees when turned, it's no good to use the detail of 360 for rotations. 72 directions is fairly sufficient because 360/5 = 72.
If you are to load a lot of same object (bullets etc) you should use CloneObject instead because it's much faster and memory-friendly. In that case, you should never delete the master-object. Just hide it, and never use it.
The function returns the handle of the loaded object. Zero if the object could not be loaded. Also, never load anything within a loop unless you really know hat you're doing. You could accidentally eat a massive amount of memory. You can notice this if the game runs really jerky. All objects should be loaded at program start. Also notice that all objects must be re-loaded if the SCREEN is used. For example of LoadObject see the MoveObject example.
Usage:
obj = LoadObject(filename$ [, rotation_detail])
LoadAnimObject
This is this is almost LoadAnimImage and almost LoadObject. But a hybrid. If you haven't yet taken a look at LoadObject or LoadAnimImage, do it now. Tou can animate the objet with PlayObject and StopObject.
Usage:
obj = LoadAnimObject(filename$m width, height, start, length [rotation_detail])
Example:
Check PlayObject example
CloneObject
Creates a clone from its source. The new object is positioned to (0,0), as normal. If the master object was hidden, the clone is still visible by default. Angle and position is cleared, all other attributes will be inherited from the master.
Cloning is lighting fast. You should always clone objects that are needed most commonly like bullets, particles and so on. But remember hat clones are very dependent from their master. If you accidentally deleted the master from memory then the clones will crash the whole game because internally they refer to the master. That's why the master object should be hidden as soon it's loaded and never be used otherwise. CoolBasic will remove it automatically when the program ends.
One object to rule them all.
Just don't kill the Master.
Usage:
newObject = CloneObject(source_object)
Example:
Type BULLETS
Field obj
End Type
'- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
'MAIN PROGRAM
'- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
FrameLimit 40
'load the soldier, turning detail 72
guy=LoadObject("Media\soldier.bmp",72)
'load the bullet (master-object) and hide it
bullet=LoadObject("Media\bullet.bmp")
ShowObject bullet,OFF
ClsColor cbdark
AddText "Arrows to turn, SPACE to shoot"
'MAIN LOOP
Repeat
'Update turning
If LeftKey() Then TurnObject guy,5
If RightKey() Then TurnObject guy, -5
'Shoot?
If KeyDown(cbkeyspace) And reload=0
newBullet.BULLETS= New(BULLETS)
newBullet\obj=CloneObject(bullet)
CloneObjectPosition newBullet\obj,guy
CloneObjectOrientation newBullet\obj,guy
'put the bullet at the end of the barrel
MoveObject newBullet\obj,24
reload=4
EndIf
'Update all BULLETS (move them forward)
i=0
For iBullet.BULLETS= Each BULLETS
MoveObject iBullet\obj,6 'speed=6
'delete bullet from collection if it
'gets too far away
If Distance2(iBullet\obj,guy)>200 Then
DeleteObject iBullet\obj
Delete iBullet
EndIf
i=i+1
Next iBullet
'Update reloading
If reload>0 Then reload=reload-1
Text 30,30,"Bulets to update: "+i
'Don't forget this :)
DrawScreen
Until EscapeKey()
MakeObject
This function creates an object that has no visible data. It may still have other attributes like collision range and so on. You can later paint the object with an image or another object with the PaintObject-command.
Usage:
obj = MakeObject()
MakeObjectFloor
This function creates an infinite plane that is automatically replicated to fill whole screen area. Its ideal use is to create an infinite floor pattern for games, like grass for example. Also notice that floor objects render to background; i.e. all other objects are drawn on top of it. Floor objects cannot have picking mode and/or collision defined.
Use PaintObject to set its texture before use. Otherwise you won't see a thing. The texture must be provided as image. The image should never be deleted (DeleteImage) before all floor objects that use it are deleted, too. Otherwise this results in program crash.
To achieve Z-depth, use PositionObject to apply fake depth. You can also animate floor objects.
Usage:
obj = MakeObjectFloor ()
Example:
floor=MakeObjectFloor()
grid=LoadImage("Media\whitegrid.bmp")
PaintObject floor,grid
grass=MakeObjectFloor()
lawn=LoadImage("Media\grass.bmp")
PaintObject grass,lawn
PositionObject grass,0,0, -2 'apply z-depth
'Load the map
map=LoadMap("Media\testmap.til","Media\tileset.bmp")
AddText "Use Arrows to move..."
SetMap map,OFF,ON 'make back layer transparent
PositionCamera 0, -90
RotateCamera 0,90 'make the camera face upwards
Repeat
'Controls
If LeftKey() Then MoveCamera -1,0
If RightKey() Then MoveCamera 1,0
If UpKey() Then MoveCamera 0,1
If DownKey() Then MoveCamera 0, -1
DrawScreen
Until EscapeKey()
PickedObject
Tells which object was picked during the last picking call (ObjecPick, CameraPick or PixelPick). Returns zero if none was picked. See ObjectPickable for details and example-
Usage:
obj = PickedObject ()
PickedX, PickedY
These functions return the exact location of where the last picking occurred. Imagine that CoolBasic projects a ray from the object that is performing the pick. That ray eventually hits a wall or other object. These will tell you the X- and Y –coordinates of the hit point.
Usage:
X = PickedX ()
Y = PickedY ()
PickedAngle
This function tells at which angle the picking ray intersected the picked object. The returned angle is the normal of the surface that was hit. For example, if you shoot at a wall from right, this will return zero-angle because zero degrees point straight off from the wall.
Usage:
Angle# = PickedAngle ()
GetAngle2, Distance2
Use these to get the angle between two objects or the distance between two objects. See the MATH-commands for more information.
Usage:
See MATH-section. They're documented there.
ObjectX, ObjectY
These functions return the coordinates of the given object. In world coordinates.
Usage:
X# = ObjectX (obj)
Y# = ObjectY (obj)
ObjectAngle
This function tells what's the current angle of the given object. The angle is measured in degrees and the return value is a real number within 0-360.
Usage:
Angle#
= ObjectAngle (obj)
ObjectSizeX, ObjectSizeY
These functions tell the pixel width and height of the given object. Please note that the size may not be the original object image dimensions. CoolBasic takes the angle of the object into account, so you'll get the précis size at that moment. If you are to use these on tilemaps, bu want to figure out the map dimension in tiles, take a look at MapWidth and MapHeight.
Usage:
Width# = ObjectSizeX (obj)
Height# = ObjectSizeY (obj)
ObjectPlaying
Returns True if object is currently animating. False (zero) if not.
Usage:
playing = ObjectPlaying (obj)
ObjectFrame
When the object is animating, you can find out which frame is currently shown. Notice that this is a real number because speed is used to interpolate. The first frame is zero.
Usage:
frame# = ObjectFrame (obj)
ObjectsOverlap
Tells whether two objects overlap by their rectangular areas or collide pixel-perfectly. Returns True if they do. There's three methods available. Rectangular method simply checks if the invisible boxes around the objects (based on their size or collision range) overlap. This is a very fast way, but may proof inaccurate on certain object types (spheres, for instance). The sphere-based collision is fairly accurate for billiard games. The pixel-perfect method is the most CPU-intensive task, but provides most accurate collision check.
Usage:
result = ObjectOverlap (obj1, obj2 [, method])
ObjectSight
This function tells whether two objects can see each others i.e. is there a wall or other object blocking the view (line of sight). You can use this, for example, to see if an enemy can see you. The function returns True of False depending if they can see each other.
Usage:
result = ObjectSight (obj1, obj2)
CountCollisions
Returns how many collision occurrences appeared during last UpdateGame (or DrawScreen if UpdateGame is not called). Use this to iterate through al collision events on a specified object.
Usage:
Nb = CountCollisions (obj)
Example:
FrameLimit 40
DrawToWorld ON
'Load the map
map=LoadMap("Media\cdm2.til","Media\tileset.bmp")
'Load player
soldier=LoadObject("Media\soldier.bmp",72)
ObjectRange soldier,32
'Load dumb immobile bot
bot=LoadObject("Media\soldier.bmp")
ObjectRange bot,32
PositionObject bot,50,200
'setup some collisions
SetupCollision soldier,bot,2,2,2
SetupCollision soldier,map,1,4,2
Repeat
'Update controls
If LeftKey() Then TurnObject soldier,5
If RightKey() Then TurnObject soldier, -5
If UpKey() Then MoveObject soldier,3.5
If DownKey() Then MoveObject soldier, -2
'Camera follow
CloneCameraPosition soldier
'Enable additional graphics
DrawGame
collisions=CountCollisions(soldier)
'iterate through all collisions
For i=1 To collisions
Color cbwhite
Text 0,(i-1)*12,"angle: "+CollisionAngle(soldier,i)+" object: "+GetCollision(soldier,i)
'draw the collision-ball
Color cbred
Circle CollisionX(soldier,i) -5,CollisionY(soldier,i)+5,10
Next i
'render SCREEN
DrawScreen
Until EscapeKey()
GetCollision
Check the other object that collided you at specified event. See CountCollisions example.
Usage:
obj = GetCollision (obj, collision_index)
CollisionX, CollisionY
See at which coordinates the specified collision event occurred. See CountCollisions example.
Usage:
X# = CollisionX (obj, collision_index)
X# = CollisionY (obj, collision_index)
CollisionAngle
Tells at which angle the specified collision event occurred. See the example. This is a very cool function :)
Usage:
Angle# = CollisionAngle (obj, collision_index)
Example:
FrameLimit 40
DrawToWorld ON
'Load the map
map=LoadMap("Media\cdm2.til","Media\tileset.bmp")
'Load player
soldier=LoadObject("Media\soldier.bmp",72)
ObjectRange soldier,32
'Load "bullet"
bullet=LoadObject("Media\fireball.bmp",9)
ShowObject bullet,OFF
'setup some collisions
SetupCollision soldier,map,1,4,2
SetupCollision bullet,map,2,4,1
AddText "Arrows to move, SPACE to shoot"
Repeat
'Update controls
If LeftKey() Then TurnObject soldier,5
If RightKey() Then TurnObject soldier, -5
If UpKey() Then MoveObject soldier,3.5
If DownKey() Then MoveObject soldier, -2
'shooting
If KeyDown(cbkeyspace) And alive=0 Then
ShowObject bullet,ON 'create bullet again
ResetObjectCollision bullet
CloneObjectPosition bullet,soldier
CloneObjectOrientation bullet,soldier
bulletplusx#=Cos(ObjectAngle(bullet))*6
bulletplusy#=Sin(ObjectAngle(bullet))*6
alive=120
EndIf
'update bullet if alive
If alive Then
TranslateObject bullet,bulletplusx#,bulletplusy#
TurnObject bullet,40
If CountCollisions(bullet) Then
angle=CollisionAngle(bullet,1)
'mirror movement components
If angle=0 Or angle=180 Then
bulletplusx#= -bulletplusx#
EndIf
If angle=90 Or angle=270 Then
bulletplusy#= -bulletplusy#
EndIf
TranslateObject bullet,bulletplusx#,bulletplusy#
EndIf
'update bullet life state
alive=alive-1
If alive=0 Then 'death
ShowObject bullet,OFF
EndIf
EndIf
'Camera follow
CloneCameraPosition soldier
DrawScreen
Until EscapeKey()