I've been working on a few features for my RTS game (which I am not reveiling
) which I thought might be useful to a few other people too.
I've put them all into a project so you can see what they do.
The code Demonstrates how to fade the edges of an RTS map (simple) and shows how you can implement a camera system which can rotate and zoom, and stay within the borders of a map while doing so.
MEDIA IS ATTACHED WITH PROJECT!
Rem Project: Advanced RTS Features
Rem Created: 13/09/2006 13:14:21
Rem ***** Main Source File *****
Set Display Mode 1024,768,32
Sync On : Sync Rate 60
Backdrop On : Color Backdrop 0
Autocam off
type gData
groundobj as integer `Stores the object number of the groud model
groundtex as integer `Stores the image number of the ground texture
selectedunits as integer `Stores the number of units the player has selected
EndType
Type MouseClicked
one as boolean `Left Mouse button
two as boolean `Right Mouse Button
three as boolean `Left + Right Mouse Buttons
four as boolean `Middle Button/Scroll Wheel Click
Endtype
Dim gData(1) as gData
Dim MouseClicked(1) as MouseClicked
Type Cursor
editortex as integer
attacktex as integer
movetex as integer
patroltex as integer
guardtex as integer
nogotex as integer
obj as integer
EndType
Dim Cursor(1) as Cursor
Remstart
The next two variables store the Map Size X/Z which can then be used to keep the camera
and the 3D cursor from going off the edge of the map.
Changing these affects the size of the map, but will not cause any problems.
Remend
Global MapSizeX as integer = 500 `Stores the X size of the battlefield.
Global MapSizeZ as integer = 500 `Stores the Z size of the battlefield.
`These variables affect the movement and rotation speeds of the camera:
Global CamScrollSpeed# as float = 2.0 `The rate at which the camera scrolls.
Global CamAngle# as float = 45.0 `This is the start angle of the camera.
Global CamRadius# as float = 100.0 `This holds the rotation radius for the camera.
Global CamHeight# as float = 100.0 `This holds the height of the camera from the ground, upwards.
Global Camobj as integer `This holds the ID of the object that the camera will be attached to.
`MouseMove Variables
Global mmx as integer
Global mmz as integer
Setup_Camera()
Create_Cursor()
Make_Level(MapSizeX,MapSizeZ)
Do
mmx = MouseMoveX()
mmz = MouseMoveZ()
Move_Cursor()
Camera_Control()
Sync
Loop
`===========================MEDIA MANAGEMENT FUNCTIONS===================================
Function Find_Free_Object()
for find = 1 to 5000
If Object Exist(find)=0 then ExitFunction find
Next find
Endfunction 0
Function Find_Free_Image()
For find = 1 to 1000
If Image Exist(find)=0 then ExitFunction find
Next find
Endfunction 0
`==============================LEVEL FUNCTIONS============================================
Function Make_Level(SizeX as integer,SizeZ as integer)
gData(1).groundobj = Find_Free_Object()
Make Object Plain gData(1).groundobj,SizeX,SizeZ
Rotate Object gData(1).groundobj,270,0,0
Position Object gData(1).groundobj,0,0,0
gData(1).groundtex = Find_free_image()
Load Image "MediaGround.jpg",gData(1).groundtex
Scale Object Texture gData(1).groundobj,SizeX/58.3,SizeZ/58.3
Texture Object gData(1).groundobj,gData(1).groundtex
`Fade the edges of the battlefield to make the battlefield look better.
borderimg = Find_Free_Image()
Load Image "Mediaborder.png",borderimg
leftedge = Find_Free_Object()
Make Object Plain leftedge,SizeZ,20
Rotate Object leftedge,270,90,0
Position Object leftedge,-(SizeX/2)+8,0.1,0
Texture Object leftedge,borderimg
Set Object Transparency leftedge,1
topedge = Find_Free_Object()
Make Object Plain topedge,SizeX,20
Rotate Object topedge,270+0.5,180,0
Position Object topedge,0,0.2,(SizeZ/2)-8
Texture Object topedge,borderimg
Set Object Transparency topedge,1
rightedge = Find_Free_Object()
Make Object Plain rightedge,SizeZ,20
Rotate Object rightedge,270,270,0
Position Object rightedge,(SizeX/2)-8,0.3,0
Texture Object rightedge,borderimg
Set Object Transparency rightedge,1
bottomedge = Find_Free_Object()
Make Object Plain bottomedge,SizeX,20
Rotate Object bottomedge,270-0.5,0,0
Position Object bottomedge,0,0.4,-(SizeZ/2)+8
Texture Object bottomedge,borderimg
Set Object Transparency bottomedge,1
Endfunction
`======================================CAMERA FUNCTIONS===================================
Function Setup_Camera()
position camera 0,90,0
point camera 0,0,0
Rotate Camera 45,Camangle#,0
Camobj = Find_Free_Object()
Make Object Triangle Camobj,0,0,0,0,0,0,0,0,0
Position Object Camobj,0,-5,0
Rotate Object Camobj,0,Camera Angle Y(),0
Hide Object Camobj
Endfunction
Function Camera_Control()
If MouseClicked(1).four = 0
`Move Camera Left
If MouseX()<10 and Check_Cam_Boundary() = 1 and Mouseclick()<>4
Move Object Left Camobj,CamScrollSpeed#
Else
Repos_Cam_In_Boundary()
Endif
`Move Camera Right
If MouseX()>1014 and Check_Cam_Boundary() = 1 and Mouseclick()<>4
Move Object Right Camobj,CamScrollSpeed#
Else
Repos_Cam_In_Boundary()
Endif
`Move Camera forward
If MouseY()<10 and Check_Cam_Boundary() = 1
Move Object Camobj,CamScrollSpeed#
Else
Repos_Cam_In_Boundary()
Endif
If MouseY()>758 and Check_Cam_Boundary() = 1
Move Object Camobj,-CamScrollSpeed#
Else
Repos_Cam_In_Boundary()
Endif
Endif
`Zoom the camera in/out if the Mouse scroll wheel is used
CamRadius# = CamRadius# - (mmz/20)
CamHeight# = CamHeight# - (mmz/20)
`Check to make sure camera is not zoomed in more than the maximum level
If CamHeight# < 30
CamHeight# = 30
Endif
If CamHeight# > 150
CamHeight# = 150
Endif
If CamRadius# < 90
CamRadius# = 90
Endif
If CamRadius# > 110
CamRadius# = 110
Endif
`Rotate the camera around the point at the center of the screen.
If Mouseclick()=4 and Check_Cam_Boundary() = 1
MouseClicked(1).four = 1
CamAngle# = CamAngle#+(mmx/3)
If CamAngle#>359.9 or CamAngle#<-359.9 then CamAngle#=0
YRotate Object Camobj,CamAngle#
Else
Repos_Cam_In_Boundary()
MouseClicked(1).four = 0
Endif
Position Camera Object Position X(Camobj),CamHeight#,Object Position Z(Camobj)
Set Camera To Object Orientation Camobj
Move Camera -CamRadius#
Point Camera Object Position X(Camobj),0,Object Position Z(Camobj)
Endfunction
Function Repos_Cam_In_Boundary()
If Object Position X(Camobj)<-(MapSizeX/2)
Position Object Camobj,-(MapSizeX/2),Object Position Y(Camobj),Object Position Z(Camobj)
Endif
If Object Position X(Camobj)>(MapSizeX/2)
Position Object Camobj,(MapSizeX/2),Object Position Y(Camobj),Object Position Z(Camobj)
Endif
If Object Position Z(Camobj)>((MapSizeZ/2))
Position Object Camobj,Object Position X(Camobj),Object Position Y(Camobj),((MapSizeZ/2))
Endif
If Object Position Z(Camobj)<-((MapSizeZ/2))
Position Object Camobj,Object Position X(Camobj),Object Position Y(Camobj),-((MapSizeZ/2))
Endif
Endfunction
Function Check_Cam_Boundary()
If Object Position X(Camobj)=>-(MapSizeX/2)
xnegative = 1
Endif
If Object Position X(Camobj)<=(MapSizeX/2)
xpositive = 1
endif
If Object Position Z(Camobj)<=((MapSizeZ/2))
znegative = 1
endif
If Object Position Z(Camobj)=>-((MapSizeZ/2))
zpositive = 1
endif
If xnegative = 1 and xpositive = 1 and znegative = 1 and zpositive = 1
ok = 1
else
ok = 0
endif
Endfunction ok
`=================================CURSOR FUNCTIONS======================================
Function Create_Cursor()
Cursor(1).obj = Find_Free_Object()
Make Object Plain Cursor(1).obj,15,15
XRotate Object Cursor(1).obj,270
Cursor(1).editortex = 300
Load Image "Mediaeditor_cur.png",Cursor(1).editortex
texture object Cursor(1).obj,0,Cursor(1).editortex : Set Object Transparency Cursor(1).obj,1
Endfunction
function Move_Cursor()
`rotate the cursor to make it a bit more appealing
Yrotate Object Cursor(1).obj,Object Angle Y(Cursor(1).obj)+1.0
cx=camera position x()
cy=camera position y()
cz=camera position z()
null=make vector3(1)
null=make vector3(2)
null=make vector3(3)
null=make vector3(4)
set vector3 1,0,-1,0
pick screen mousex(), mousey(), 1
mx# = get pick vector x()
my# = get pick vector y()
mz# = get pick vector z()
set vector3 2,mx#,my#,mz#
angle# = acos(dot product vector3(2, 1))
cosangle# = cos(angle#)
hypolength# = cy/cosangle#
normalize vector3 2,2
scale vector3 2,2,hypolength#
set vector3 1,cx,cy,cz
add vector3 4,1,2
position object Cursor(1).obj, x vector3(4),0.5,z vector3(4)
Repos_Cursor_In_Boundary()
endfunction
Function Repos_Cursor_In_Boundary()
If Object Position X(Cursor(1).obj)<-(MapSizeX/2)
Position Object Cursor(1).obj,-(MapSizeX/2),Object Position Y(Cursor(1).obj),Object Position Z(Cursor(1).obj)
Endif
If Object Position X(Cursor(1).obj)>(MapSizeX/2)
Position Object Cursor(1).obj,(MapSizeX/2),Object Position Y(Cursor(1).obj),Object Position Z(Cursor(1).obj)
Endif
If Object Position Z(Cursor(1).obj)>((MapSizeZ/2))
Position Object Cursor(1).obj,Object Position X(Cursor(1).obj),Object Position Y(Cursor(1).obj),((MapSizeZ/2))
Endif
If Object Position Z(Cursor(1).obj)<-((MapSizeZ/2))
Position Object Cursor(1).obj,Object Position X(Cursor(1).obj),Object Position Y(Cursor(1).obj),-((MapSizeZ/2))
Endif
Endfunction
I will be adding to this code as I progress with my game, so you will eventually be able to get hold of more RTS code. :p
I Would appreciate some feedback (good or bad) on this.
WORLD - A free World editor, capable of creating the most complex of worlds (including physics, skies, water, shaders, terrain editing and more). Best of all, it's FREE!