Using Cameras to Follow PLayers

Started by kevin, September 14, 2004, 10:06:35 AM

Previous topic - Next topic

kevin

Here's an example of how to get the PlayBASIC to display and follow a player.   This example uses the camera system dynamically.  You could also use the world system to store larger worlds of depth sorted elements.  These can be translated to a camera and viewed at will.   So clipping/draw order are no longer the coders issue.

It should also be noted that you mixed any gfx elements together you wish in the scene buffer/camera, so you can draw text/ vector gfx / maps in front of,  behind sprites (or both)..  as everything is drawn according it their z depth in the scene buffer..

Since this examples creates it's own media, Most of code is just setting up the demo..




PlayBASIC Code: [Select]
;---------------------------------------------------------------------; This example shows a method of attaching a Play Basics camera system; To follow a player and control what the viewer sees.  Cameras handle; render order and clipping for you.  So it'll work out, what needs to; displayed and the order to draw things.  ;---------------------------------------------------------------------
; create a Map #1, assign this map 10 possible LEvels
ThisMap=1
CreateMap ThisMap,10

; ==================================; CReate an image of blocks; ==================================

Cls 0

NumberOfBlocks = 10
BlockSize=32

For lp=1 To NUmberOfBlocks
x=lp*BlockSize
BoxC x,0,X+BlockSize-1,BlockSIze,1,RndRGB()
Next


TempIMage=55

GetImage TempImage,0,0,x,BlockSize
; Convert the Image created above To Tiles for THISMAP
MakeMapGFX ThisMap,TempImage,BlockSize,BlockSize,NumberOfBlocks,RGB(0,0,0)

; create a Level #1 in THisMAp. of size 300 blocks by 200 blocks
CreateLevel ThisMap,1,300,200
; fill map full of random tiles
For ylp=0 To GetLevelHeight(ThisMap,1)
For xlp=0 To GetLevelWidth(ThisMap,1)
PokeLevelTile ThisMap,1,xlp,ylp,RndRange(0,NUmberOfBlocks)
Next
Next



; Create a camera.. It's size will default to the Current surface. in this case; the current surface is the screen.
; by fault cameras have auto CLS enabled.. So you main loop doesn't need to CLS
CreateCamera 1

; Limit This camera movement within the bound of the levels

LimitCamera 1,True
LimitCameraBounds 1,0,0,GetLevelABSWidth(thisMap,1),GetLevelABSHeight(thisMap,1)

; set players Initial position
Player_Xpos=GetScreenWidth()/2
Player_Ypos=GetScreenHeight()/2


Do

; Redirect ALL GFX output to the SCENE BUFFER
CaptureToScene

; Clear the Scene buffer of any preview gfx elements
ClsScene

; Tell PB to Capture ALL the follow gfx commands and give them scene depth
; of 100
CaptureDepth 100

; DRaw the Level Map to the Scene buffer, not the screen.
DrawMap ThisMap,1,0,0


; handle some basic Up/down/left/right styled player controls
If UpKey() Then DEC player_ypos
If DownKey() Then INC player_ypos
If LeftKey() Then DEC player_Xpos
If RightKey() Then INC player_Xpos


; Set the scene capture depth to 50.. So this object will be drawn in front of
; Anything else
CaptureDepth 50
CircleC PLayer_xpos,PLayer_ypos,40,1,$ff00ff


; Set the camera to the Players position
Speed#=50
Cam_X#=CurveValue(PLayer_Xpos,Cam_X#,speed#)
Cam_Y#=CurveValue(PLayer_Ypos,Cam_Y#,speed#)
PositionCamera 1,cam_X#-(GetCameraWidth(1)/2),cam_y#-(GetCameraHeight(1)/2)

; The objects in the SCENE BUFFER through CAMERA #1
DrawCamera 1


Sync
Loop