TGC Codebase Backup



base for level Editor by MEE

13th May 2008 21:29
Summary

A foundation for a basic level editor or posably an RTS.



Description

Basicly a level editor I started a while ago and don't have time to finish. It mostly just needs a way to texture the matrix square by square.
It uses an RTS style camera movement system so the code could also be used for an RTS game

This is my first real program so please feel free to comment:)

Good luck and have fun.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Hold (right click) and use mouse to move  
`(left click) to place objects
`(SPACE) to change to next object
`Green cone Taraforms "use (TAB) key to switch up and down" 
`(scroll wheel) rotates object
`(num pad) to look left/right and move up/down
`Hold (SHIFT) and use mouse/scroll to tilt cam/zoom
`Hold (S) key and scroll to scale
`(F2) save, (F3) load, (F4) delete 



`Things I need to do:)
` get save working properly 
` make scale work better
`clean up code

sync on
sync rate 40
Xrez=800
Yrez=600
set display mode Xrez,Yrez,32
`set window on
hide mouse


num = 1
flag = 0
fl=0
Q=1
strech# = 0
U#=180




`load image "grass1.jpg",1
`load image "grass.jpg",2



make object cone 1,1
rotate object 1,0,0,U#
color object 1, rgb(0,255,0)



make object cube 2,2



make object cylinder 3,2
color object 3, rgb(200,200,255)
move object 2,5





make matrix 1,60,80,60,80
position matrix 1,0,-1,0
dim hat#(60,80)


position camera 15,15,22.5
rotate camera 45,0,0






`The start of our loop

do

`only show active object
hide object 1
hide object 2
hide object 3
`hide object 4
`hide object 5
`hide object 6
show object num


` get values
camY# = (camera position Y())
camAngX# = camera angle X()
camAngY# = camera angle Y()
camAngZ# = camera angle Z()
obX#=object position X(num)
obY#=object position Y(num)
obZ#=object position Z(num)
speedx#=((mousex()-(Xrez/2))*.01)
speedy#=((mousey()-(yrez/2))*.01)
posY#=((Yrez-mousey())/5)
posX#=(((Xrez/2)-mousex())/-10)
sizeX#=object size X(num)
sizeY#=object size Y(num)
sizeZ#=object size Z(num)





`Click to place object
   if mouseclick()=1 and flag = 0 then gosub placeobject

   if mouseclick()=1
      flag = 1
   else
      flag = 0
   endif


`Use space to change to next object
   if spacekey()=1 and flag2=0 then num=num+1
   if num>3 then num=1

   if spacekey()=1
      flag2 = 1
   else
      flag2 = 0
   endif



`Hold right click to move around level
	if mouseclick()=2
		camX# = (camera position X())
		camZ# = (camera position Z())
	else
		camX# = int(camera position X())
		camZ# = int(camera position Z())
	endif




`Fallow terrain height
	height#=get ground height(1,obX#,obZ#)
	Zval#=height#


`Change terain height
	if keystate(15)=1 and flag7=0 then U# = U#+180
   if keystate(15)=1
      flag7 = 1
   else
      flag7 = 0
   endif
	if U#>180 then U#=0

	if num=1 and obZ#<80 and obZ#>0 and obX#<59 and obX#>0 and mouseclick()=1
		set matrix height 1,obX#,obZ#,height#+.2
		set matrix height 1,obX#,obZ#-1,height#+.1
		set matrix height 1,obX#,obZ#+1,height#+.1
		set matrix height 1,obX#-1,obZ#,height#+.1
		set matrix height 1,obX#+1,obZ#,height#+.1
	endif
	if num=1 and obZ#<80 and obZ#>0 and obX#<59 and obX#>0 and U#=180 and mouseclick()=1
		set matrix height 1,obX#,obZ#,height#-.2
		set matrix height 1,obX#,obZ#-1,height#-.1
		set matrix height 1,obX#,obZ#+1,height#-.1
	   set matrix height 1,obX#-1,obZ#,height#-.1
		set matrix height 1,obX#+1,obZ#,height#-.1
	endif

 rotate object 1,0,0,U#



`Use scroll wheel to rotate object
if r=1 and shiftkey()=0
   if mousemoveZ()>MMZ and flag3=0 and shiftkey()=0 then y=wrapvalue(y+45)
   if mousemoveZ()<MMZ and flag3=0 and shiftkey()=0 then y=wrapvalue(y-45)


   if mousemoveZ()>MMZ or mousemoveZ()<MMZ
      flag3 = 1
   else
      flag3 = 0
   endif
   MMZ=mousemoveZ()
endif



`scale object

if keystate(31)=1 
	MMZ=0
	r=0
	scale object num,sizeX#+mousemoveZ()*.1,sizeY#+mousemoveZ()*.1,sizeZ#+mousemoveZ()*.1
else
	r=1
endif



 rotate object 2,0,y,0
 rotate object 3,0,y,0
` rotate object 4,90,0,y
` rotate object 5,90,0,y
` rotate object 6,0,y,0




`Control input for camera rotation/height 



`raise/lower camera
	if keystate(72)=1 then camY#=camY#+1
	if keystate(80)=1 then camY#=camY#-1

`rotate camera view
	if keystate(75) and flag4=0 then Q=Q-1
	   if keystate(75)=1
   	   flag4 = 1
   	else
   	   flag4 = 0
   	endif


	if keystate(77) and flag5=0 then Q=Q+1
	   if keystate(77)=1
	      flag5 = 1
	   else
	      flag5 = 0
	   endif


`Q selects look direction
	if Q=5 or Q=-3 then Q=1
	if Q=1 then gosub forward
	if Q=2 or Q=-2 then gosub right
	if Q=3 or Q=-1 then gosub backward
	if Q=4 or Q=0 then gosub left
 position camera camX#,camY#,camZ#
 yrotate camera wrapvalue(camAngY#)



`Look up/down and zoom using shift key
   if shiftkey()=1
   position mouse Xrez/2,Yrez/2
      if mousemovey() > 0 then xrotate camera wrapvalue(camAngX#+1)
      if mousemovey() < 0 then xrotate camera wrapvalue(camAngX#-1)
      if mousemoveZ() > MMZ then Move camera 2
      if mousemoveZ() < MMZ then Move camera -2
		MMZ=mousemoveZ()
   endif


` wire frame
	if keystate(17)=1 and flag6 = 0 then wframe = wframe + 1

	if wframe>1 then wframe = 0 

   if keystate(17)=1
      flag6 = 1
   else
      flag6 = 0
   endif

	if wframe =1
		set matrix wireframe off 1
	else 
		set matrix wireframe on 1
	endif






`Save / Load / Delete Static objects

`F2 is save, F3 load and F4 is delete 

   if keystate(60)=1 then gosub _save_routine
	if keystate(61)=1 then gosub load
	if keystate(62)=1 then delete static objects




`Debug info
set cursor 10,10
print "mouse x " , mousex()
print "mouse y " , mousey()
print mousemovez()
print "object number ",num
print "object angle ",y
print obX#," ",obZ#
print "X ",camX#," Z ",camZ#
print sizeX#," ",sizeY#," ",sizeZ#
print posY#," ",posX#
print speedY#," ",speedX#
print scancode()



update matrix 1
sync


loop











`gosubs

` SPECIAL THANKS TO LATCH FOR THIS ONE!
_save_routine:
   repeat
      text 300,400,"are you sure you want to save? (Y)/(N)"
      choice$=lower$(inkey$())
   until choice$="y" or choice$="n"
   if choice$="y"
      set cursor 300,440	
		Set Camera View 0,0,1,1
      input "Please Enter File Name : ";static$
	`save objects
		save static objects "static$"
		
	`put matrix values in array
		for x = 1 to 60
			for y = 1 to 80
				hat#(x,y)=get ground height(1,x,y)
			next y
		next x
	
	`save matix values
		save array "hat.dat",hat#(0)
	
      text 300,460,"File "+static$+" was saved successfully"
   else
      text 300,460,"LEVEL NOT SAVED !!!"
   endif
	wait 1000
	Set Camera View 0,0,800,600
return





load:
load static objects "static$"
load array "hat.dat",hat#(0)
`set matrix height with values from array
	for x = 1 to 60
		for y = 1 to 80
			set matrix height 1,x,y,hat#(x,y)
		next y
	next x
	
return






placeobject:
if num>1
 make static object num
endif
return





`camera movement

forward:
   if mouseclick()=2
      camX#=camX#+speedx#
      camZ#=(camZ#-speedy#)
   endif
camAngY#=0
position object num,posX#+camX#,Zval#,(posY#+camZ#)-32.5
return


right:
camAngY#=90
   if mouseclick()=2
      camX#=camX#-speedy#
      camZ#=camZ#-speedx#
   endif
position object num,(posY#-32)+camX#,Zval#,((posX#*-1)+camZ#)+.5
return


backward:
   if mouseclick()=2
      camX#=camX#-speedx#
      camZ#=(camZ#+speedy#)
   endif
camAngY#=180
position object num,(posX#*-1)+camX#,Zval#,((posY#*-1)+camZ#)+32.5
return


left:
   if mouseclick()=2
      camX#=camX#+speedy#
      camZ#=camZ#+speedx#
   endif
camAngY#=270
position object num,((posY#*-1)+camX#)+32,Zval#,((posX#)+camZ#)+.5

return