and please use only id's returned from create functions
also at loading level
createsprite(brick[n].spritenumber,brick[n].imagenum)
=
brick[n].spritenumber=createsprite(brick[n].imagenum)
SetDisplayAspect( 1024.0 / 600.0 )
#constant PHYS_NONE 0
#constant PHYS_STATIC 1
#constant PHYS_DYNAMIC 2
#constant PHYS_KINEMATIC 3
#constant PSQUARE = 2
#constant PCIRCLE = 1
#constant PPOLYGON = 3
global cursorimage = 100
global cursorsprite = 900
global gridx=3
global gridy=2
global brickcount = 0
global swidth = 3
global sheight = 2
global x1,y1,x2,y2
global gfxplusbutton = 1
global gfxminusbutton = 2
global editbutton = 3
global savebutton = 4
global loadbutton = 5
global clearbutton = 6
global sizeplusbutton = 7
global sizeminusbutton = 8
global physicsmodebutton = 9
global physicsmode = PHYS_STATIC
global gplusdetect
global gminusdetect
global sizeplusdetect
global sizeminusdetect
global physicsbuttondetect
global editdetect
global savedetect
global loaddetect
global cleardetect
global buttonsize = 5
global bricklimit = 800
global mode = 0
global maxscroll=400
global minscroll=0
global scrollx#
global scrolly#
global x1
global y1
global x2
global y2
type bricks
xloc as integer
yloc as integer
spritenumber as integer
physicstype as integer
spriteshape as integer
sizex as integer
sizey as integer
imagenum as integer
endtype
type TMouse
x1#
y1#
x2#
y2#
x3
y3
endtype
global Mouse as TMouse
dim brick[800] as bricks
SetPhysicsDebugOn()
init()
rem A Wizard Did It!
clearlev()
do
//------------------------------------- Scroll
x#=getvirtualjoystickx(1)
y#=getvirtualjoysticky(1)
if x#<>0 or y#<>0
scrollx#=scrollx#+x#
scrolly#=scrolly#+y#
if scrollx#>maxscroll
scrollx#=maxscroll
endif
if scrollx#<minscroll
scrollx#=minscroll
endif
if scrolly#>maxscroll
scrolly#=maxscroll
endif
if scrolly#<minscroll
scrolly#=minscroll
endif
y1=y1-scrolly#
x1=x1-scrollx#
SetViewOffset( scrollx#,scrolly# )
endif
//-------------------------------------
select physicsmode
case 1:
pm$="Static"
endcase
case 2:
pm$="Dynamic"
endcase
case 3:
pm$="Kinematic"
endcase
endselect
print (""+str(brickcount))
print (""+str(mode))
print ("Physics Mode = " + pm$)
control()
Sync()
loop
function control()
FillMouse(gridx,gridy)
`******* THIS MUST BE WRONG *********************************************************************************************************
//pointer???
//setspriteposition(pointer,Mouse.x2#,Mouse.y2#)
setspriteposition(cursorsprite,Mouse.x3,Mouse.y3)
gplus = getvirtualbuttonpressed(gfxplusbutton)
gminus = getvirtualbuttonpressed(gfxminusbutton)
editdetect=getvirtualbuttonpressed(editbutton)
savedetect=getvirtualbuttonpressed(savebutton)
loaddetect=getvirtualbuttonpressed(loadbutton)
cleardetect=getvirtualbuttonpressed(clearbutton)
sizeplusdetect = getvirtualbuttonpressed(sizeplusbutton)
sizeminusdetect = getvirtualbuttonpressed(sizeminusbutton)
physicsbuttondetect=getvirtualbuttonpressed(physicsmodebutton)
state=getpointerstate()
found=0
if physicsbuttondetect > 0
inc physicsmode
if physicsmode> 3
physicsmode = 1
endif
Endif
if gplus > 0
endif
rem *** Has user pressed edit button, if so cycle through edit/placement mode ***
if editdetect > 0
inc mode
if mode>1
mode=0
endif
Endif
rem *** Has user pressed save level button ****
if savedetect > 0
save_level()
Endif
rem *** Has user pressed load level button ***
if loaddetect > 0
clearlev()
load_level()
endif
rem *** Has user pressed clear level button ***
if cleardetect > 0
clearlev()
Endif
if sizeplusdetect > 0
swidth = swidth + 1
setspritesize(cursorsprite,swidth,sheight)
endif
if sizeminusdetect > 0 and swidth>3
swidth = swidth - 1
setspritesize(cursorsprite,swidth,sheight)
endif
`*** If pointer is Pressed
if state = 1
select mode
`*** Placement Mode
case 0:
`******************************** first see if there is a sprite already at pointer location or not ******************
for n = 1 to bricklimit
if brick[n].xloc = Mouse.x3 and brick[n].yloc = Mouse.y3
found=1
f=brick[n].spritenumber
exit
endif
next n
if found=0 and brickcount<bricklimit
inc brickcount
id=createsprite(cursorimage)
id2=id-10000
deletesprite(id)
createsprite(id2,cursorimage)
setspritesize(id2,swidth,sheight)
setspritedepth(id2,0)
brick[id2].xloc = Mouse.x3
brick[id2].yloc = Mouse.y3
brick[id2].spritenumber = id2
brick[id2].imagenum = cursorimage
brick[id2].sizex=swidth
brick[id2].sizey=sheight
brick[id2].physicstype = physicsmode
setspritephysicson(id2,brick[id2].physicstype)
setspriteposition(id2,brick[id2].xloc,brick[id2].yloc)
else
print ("Space occupied "+ "By sprite #"+str(f))
endif
Endcase
`*** Edit Mode
case 1:
r=getspritehit(Mouse.x2#,Mouse.y2#)
if getspriteexists(r)
if r<>900
deletesprite(r)
brick[r].xloc = 0
brick[r].yloc = 0
brick[r].spritenumber = 0
brick[r].imagenum = 0
brick[r].physicstype = 0
dec brickcount
endif
Endif
endcase
Endselect
endif
endfunction
function init()
loadimage(cursorimage,"cursor.png")
loadimage(101,"redbrick.png")
loadimage(102,"bluebrick2.png")
loadimage(103,"greenbrick.png")
setclearcolor(0,200,0)
clearscreen()
createsprite(cursorsprite,cursorimage)
setspritesize(cursorsprite,swidth,sheight)
setspritedepth(cursorsprite,1)
`Create virtual Buttons
addvirtualbutton(gfxplusbutton,70,95,buttonsize)
setvirtualbuttontext(gfxplusbutton,"Gfx ++")
addvirtualbutton(gfxminusbutton,75.5,95,buttonsize)
setvirtualbuttontext(gfxminusbutton,"Gfx --")
addvirtualbutton(sizeplusbutton,50,95,buttonsize)
setvirtualbuttontext(sizeplusbutton,"Size ++")
addvirtualbutton(sizeminusbutton,55,95,buttonsize)
setvirtualbuttontext(sizeminusbutton,"Size --")
addvirtualbutton(physicsmodebutton,35,95,buttonsize+2)
setvirtualbuttontext(physicsmodebutton,"Physics")
addvirtualbutton(editbutton,81,95,buttonsize)
setvirtualbuttontext(editbutton,"Edit")
addvirtualbutton(savebutton,15,95,buttonsize)
setvirtualbuttontext(savebutton,"Save")
addvirtualbutton(loadbutton,10,95,buttonsize)
setvirtualbuttontext(loadbutton,"Load")
addvirtualbutton(clearbutton,20,95,buttonsize)
setvirtualbuttontext(clearbutton,"Clear")
addvirtualjoystick(1,80,80,10)
endfunction
function clearlev()
brickcount = 0
for n=1 to bricklimit
if getspriteexists(n)
deletesprite(n)
endif
brick[n].xloc = 0
brick[n].yloc = 0
brick[n].spritenumber = 0
brick[n].imagenum = 0
brick[n].physicstype = 0
next n
sync()
endfunction
function load_level()
myfile = OpenToread("Test2.dat")
`brickcount = readinteger (myfile)
for n = 1 to bricklimit
xl = readinteger (myfile)
yl = readinteger (myfile)
im = readinteger (myfile)
sn= readinteger (myfile)
sx = readinteger (myfile)
sy = readinteger (myfile)
pt = readinteger (myfile)
brick[n].xloc = xl
brick[n].yloc = yl
brick[n].imagenum = im
brick[n].spritenumber = sn
brick[n].sizex= sx
brick[n].sizey= sy
brick[n].physicstype = pt
if brick[n].spritenumber <> 0
inc brickcount
createsprite(brick[n].spritenumber,brick[n].imagenum)
setspritesize(brick[n].spritenumber,brick[n].sizex,brick[n].sizey)
setspritephysicson(brick[n].spritenumber,brick[n].physicstype)
setspriteposition(brick[n].spritenumber,brick[n].xloc,brick[n].yloc)
setspritedepth(brick[n].spritenumber,0)
endif
next n
closefile (myfile)
print("loaded!!")
sync()
endfunction
function save_level()
myfile = OpenToWrite("Test2.dat", 0)
print ("poop !!")
`writeinteger (myfile,brickcount)
for n = 1 to bricklimit
writeinteger (myfile,brick[n].xloc)
writeinteger (myfile,brick[n].yloc)
writeinteger (myfile,brick[n].imagenum)
writeinteger (myfile,brick[n].spritenumber)
writeinteger (myfile,brick[n].sizex)
writeinteger (myfile,brick[n].sizey)
writeinteger (myfile,brick[n].physicstype)
next n
closefile (myfile)
print ("saved !!")
endfunction
function FillMouse(gridx,gridy)
Mouse.x1#=getpointerx()
Mouse.y1#=getpointery()
Mouse.x2#=screentoworldx(Mouse.x1#)
Mouse.y2#=screentoworldy(Mouse.y1#)
Mouse.x3=floor(Mouse.x2#/gridx)*gridx
Mouse.y3=floor(Mouse.y2#/gridy)*gridy
endfunction