Okay, well I changed it to use memblocks, and the map looks way cooler, and pretty much a lot has changed.
Now there is also pathfinding and destructible environments (sorta). The destructible part is really slow, not sure how I would speed it up.
But anyways, the controls are
-left mouse : moves player to pointer (will get stuck)
-right mouse : destroys walls
code :
Rem Project: Testing
Rem Created: Saturday, January 23, 2010
Rem ***** Main Source File *****
`input \"width:\",width
`input \"height:\",height
`input \"depth:\",depth
width = 1440
height = 900
depth = 32
`set up our screen
set display mode width,height,depth
set window position 0,0
sync on : sync rate 60
Global low as integer
`create a new bitmap.
`by deafult, the screen is bitmap number 0. It is the only bitmap that is drawn to the screen, but other bitmaps sit in the buffer
`and can be operated on. This bitmap will be the world collision map.
`create bitmap 1,screen width(),screen height()
`set up a set of variables to work for waypoints
type waypoints
x as float
y as float
score as float
distance as float
flag as float
options as float
color as dword
totalDist as integer
endtype
`set up a set of variables to work for our actors
type actor
image as integer
sprNum as integer
x as float
y as float
speed as float
xdestination as float
ydestination as float
xgoto as float
ygoto as float
wp as integer
angle as float
vel as float
endtype
`set up a set of variables to work for our world
type world
colImage as integer
collisionMemBlock as integer
artImage as integer
ambImage as integer
paiNumber as integer
wayPointCount as integer
offsetX as float
offsetY as float
scrollSpeed as float
endtype
type light
x as float
y as float
sprNum as integer
image as integer
endtype
`construct the level as a world
Global level as world
level.collisionMemBlock = 1
level.colImage = 2
level.artImage = 3
level.ambImage = 4
level.paiNumber = 1
level.scrollSpeed = 4
`construct the player as an actor
Global plr as actor
plr.image = 1
plr.sprNum = 1
Global lamp as light
lamp.image = 5
lamp.sprNum=2
load image \"media\\testing3_plr.bmp\",plr.image
`set image colorkey 255,255,255
`load up all that crazy awesome media!
load image \"media\\finding5_col_layer.png\",level.colImage
load image \"media\\finding5_art_layer.png\",level.artImage
load image \"media\\finding5_amb_layer.png\",level.ambImage
open to read level.paiNumber,\"media\\finding5_pai_layer.txt\"
read string level.paiNumber,temp$ : level.wayPointCount = VAL(temp$) -1
dim wayPoint(level.wayPointCount) as waypoints
for c = 1 to level.wayPointCount
read string level.paiNumber, temp$ : waypoint(c).x = VAL(temp$)
read string level.paiNumber, temp$ : waypoint(c).y = VAL(temp$)
next c
load image \"media\\light.png\",lamp.image
spaceImage = 6
load image \"media\\circle.png\",spaceImage
`
level.offsetX = 100
level.offsetY = 100
`create the player sprite, so that later in the code, it doesnt crash and burn.
plr.x = 50 + level.offsetX
plr.y = 50 + level.offsetY
sprite plr.sprNum,plr.x,plr.y,plr.image
plr.speed = 5
plr.vel = 5
for c = 1 to level.wayPointCount
inc waypoint(c).x,level.offsetX
inc wayPoint(c).y,level.offsetY
next c
make memblock from image level.collisionMemBlock,level.colImage
do
cls
`be able to move the map around
if mousex() > screen width()-(screen width()/8)
dec level.offsetX,level.scrollSpeed
dec plr.x,level.scrollSpeed
dec plr.xdestination,level.scrollspeed
for c = 1 to level.waypointCount : dec waypoint(c).x,level.scrollspeed : next c
endif
if mouseX() < screen width()/8
inc level.offsetX,level.scrollSpeed
inc plr.x,level.scrollSpeed
inc plr.xdestination,level.scrollspeed
for c = 1 to level.waypointCount : inc waypoint(c).x,level.scrollspeed : next c
endif
if mouseY() > screen height()-(screen height()/8)
dec level.offsetY,level.scrollSpeed
dec plr.y,level.scrollSpeed
dec plr.ydestination,level.scrollspeed
for c = 1 to level.waypointCount : dec waypoint(c).y,level.scrollspeed : next c
endif
if mouseY() < screen height()/8
inc level.offsetY,level.scrollSpeed
inc plr.y,level.scrollSpeed
inc plr.ydestination,level.scrollspeed
for c = 1 to level.waypointCount : inc waypoint(c).y,level.scrollspeed : next c
endif
`by setting the bitmap 1, all operations are refering to bitmap 1, and not bitmap 0.
`set current bitmap 1
`we draw our collision map to bitmap 1.
`paste image level.colImage,level.offsetX,level.offsetY,1
`color = getColorPoint(level.colImage,mouseX()-level.offsetX,mouseY()-level.offsetY)
`move player
plr.angle = 180+atanfull(plr.xdestination - plr.x,plr.ydestination - plr.y)*-1
`when the mouse is clicked, a new destination is set.
if mouseclick() = 1
`these 3 variable sets mean something. Ill be damned if I know what though.
plr.xgoto = mouseX()
plr.ygoto = mouseY()
plr.xdestination = mouseX()
plr.ydestination = mouseY()
x = mouseX()
y = mouseY()
`reset all the settings on the way points to prepare for a new path.
for c = 1 to level.wayPointCount
waypoint(c).score = 0
waypoint(c).flag = 0
waypoint(c).options = 0
waypoint(c).totalDist = 0
next c
`if the player cannot just go directly, find out what waypoint is best to go to
if canGo(plr.X,plr.Y,plr.xgoto,plr.ygoto) = 0
`this constructs all the way points with new settings to suit the position of the player and the destination
pass = 1
`this sets the flag of every way point connected to the destination to 1
for c = 1 to level.wayPointCount
if waypoint(c).flag = 0
if canGo(waypoint(c).x,waypoint(c).y,x,y)
waypoint(c).flag = 1
inc waypoint(c).options
distX = waypoint(c).x - x
distY = waypoint(c).y - y
dist = sqrt( (distX*distX)+(distY*distY) )
inc waypoint(c).totalDist,dist
endif
endif
next c
`now keep on moving out and increasing the flag.
repeat
allwaypointslocated = 1
for c = 1 to level.wayPointCount
if waypoint(c).flag = pass
for c2 = 1 to level.wayPointCount
if waypoint(c2).flag <= 0
if canGo(waypoint(c).x,waypoint(c).y,waypoint(c2).x,waypoint(c2).y)
inc waypoint(c2).options
waypoint(c2).flag = -1
distX = waypoint(c).x - waypoint(c2).x
distY = waypoint(c).y - waypoint(c2).y
dist = sqrt( (distX*distX)+(distY*distY) )
inc waypoint(c2).totalDist,dist
inc waypoint(c2).totalDist,waypoint(c).totalDist
endif
endif
next c2
endif
next c
for c2 = 1 to level.wayPointCount
if waypoint(c2).flag< 0
waypoint(c2).flag = pass+1
endif
next c2
for c3 = 1 to level.wayPointCount
if waypoint(c3).flag = 0 then allwaypointslocated = 0
next c3
inc pass
until allwaypointslocated
findNextWayPoint()
endif
endif
`now we want to move our player if the player is not already where it needs to be.
if atDestination(plr.x,plr.y,plr.xdestination,plr.ydestination)=0
`if there is a waypoint in the destination, then we must handle it!
if plr.wp > 0
plr.xdestination = waypoint(plr.wp).x
plr.ydestination = waypoint(plr.wp).y
distX = waypoint(plr.wp).x - plr.x
distY = waypoint(plr.wp).y - plr.y
dist = sqrt( (distX*distX)+(distY*distY) )
`if we are at the waypoint, then find the next one.
if dist < sprite width(plr.sprNum)*.6
if waypoint(plr.wp).flag = 1 then plr.wp = 0
if plr.wp<> 0
findNextWayPoint()
endif
endif
endif
`if there are no more waypoints, then go to the destination
if plr.wp = 0
plr.xdestination = plr.xgoto
plr.ydestination = plr.ygoto
endif
`this is where the movement actually takes place. The crummiest sliding collision ever.
inc plr.x,cos(plr.angle-90)*plr.vel
sprite plr.sprNum,plr.x,plr.y,plr.image
if checkcollisionSprite(plr.sprnum)
dec plr.x,cos(plr.angle-90)*plr.vel
endif
inc plr.y,sin(plr.angle-90)*plr.vel
sprite plr.sprNum,plr.x,plr.y,plr.image
if checkcollisionSprite(plr.sprNum)
dec plr.y,sin(plr.angle-90)*plr.vel
endif
endif
`now becuase we want to see whats going on, set the bitmap back to the screen...
`set current bitmap 0
`now we draw the player to the screen
sprite plr.sprNum,plr.x,plr.y,plr.image
rotate sprite plr.sprnum,plr.angle+180
offset sprite plr.sprnum,sprite width(plr.sprNum)/2,sprite height(plr.sprNum)/2
`and now we have to remember to draw the art layer of the level.
`paste image level.bakImage,level.offsetX,level.offsetY
paste image level.colImage,level.offsetX,level.offsetY,1
paste image level.artImage,level.offsetX,level.offsetY,1
`paste image level.colImage,level.offsetX,level.offsetY,1
`paste our waypoints
if waypointOn
for c = 1 to level.wayPointCount
ink rgb (255,255,255),0
ink waypoint(c).color,0
if plr.wp = c then ink rgb(255,0,0),0
center text waypoint(c).x,waypoint(c).y,str$(waypoint(c).totalDist)+\":\"+str$(waypoint(c).flag)
next c
endif
`text 0,0,\"Right Mouse to toggle waypoints:\"+str$(waypointOn,1)
text 0,0,str$(screen fps())
if mouseclick() = 2 and timer() > mouseTime
mouseTIme = timer() + 1000
pasteToImage(level.colImage,spaceImage,1,2,mouseX()-level.offsetX,mouseY()-level.offsetY)
`pasteToImage(level.artImage,spaceImage,2,3,mouseX()-level.offsetX,mouseY()-level.offsetY)
endif
`refresh the screen!
sync
loop
Function findNextWayPoint()
`find which waypoints are connected to the player, and choose the ones with the lowest flag
low = 99
old = plr.wp
for c = 1 to level.wayPointCount
waypoint(c).score = 0
waypoint(c).color = rgb(255,255,255)
if canGo(plr.x,plr.y,waypoint(c).x,waypoint(c).y)
if waypoint(c).flag =< low and plr.wp <> c
low = waypoint(c).flag
waypoint(c).score = 1
waypoint(c).color = rgb(0,255,0)
endif
endif
next c
`now out of the lowest flags, which is the lowest distance, because that is the key.
lowest = 9999
for c = 1 to level.wayPointCount
if waypoint(c).score = 1
waypoint(c).color = rgb(0,0,255)
if waypoint(c).totalDist < lowest AND plr.wp <> c
lowest = waypoint(c).totalDist
plr.wp = c
endif
endif
next c
Endfunction
FUnction atDestination(x1 as float, y1 as float ,x2 as float ,y2 as float )
`are we within 3 units of the destination?
if x2 > x1 - 3 AND x2 < x1+3
if y2 > y1 - 3 AND y2 < y1+3
exitfunction 1
endif
endif
Endfunction 0
function canGo(x1,y1,x2,y2)
`this draws a theoretical line to the target, and if anything gets in the way, the way is impassible. To increase performace, put a step on.
distX# = x2 - x1
distY# = y2 - y1
dist# = sqrt( (distX#*distX#) + (distY#*distY#) )
angle = 180+atanfull(distX#,distY#)*-1
for t = 0 to dist# step 10
x = x1 + (cos(angle-90)*t)
y = y1 + (sin(angle-90)*t)
if checkCollisionPoint(x,y)
exitFunction 0
endif
next t
endfunction 1
function checkCollisionSprite(sprNum)
`Yes, this is a <edited out swear> collision function, but it works for circles.... Were going to need a better one though.
`it checks points all around the circle for collision. So really, a small sliver of collision map could go unnoticed.
for a = 0 to 360 step 5
x = sprite x(sprNum) + (cos(a)*sprite width(sprNum)/2)
y = sprite y(sprNum) + (sin(a)*sprite height(sprNum)/2)
collision = checkCollisionPoint(x,y)
if collision = 1 then exitFunction collision
next a
Endfunction collision
Function checkCollisionPoint(x,y)
`the point function retrieves the color value at the given point. So becuase our collision map is pure black/white, if the retreived
`value is black, then that is a wall or something.
`colTest =Point(x,y)
colTest = getCollisionPoint(x-level.offsetX,y-level.offsetY)
if colTest = 0
collision = 1
endif
endfunction collision
Function pasteToImage(image,addImage,mem,bitmap,x,y)
if bitmap exist(bitmap) then delete bitmap bitmap
create bitmap bitmap,image width(image),image height(image)
set current bitmap bitmap
paste image image,0,0,1
paste image addImage,x-(image widtH(addImage)/2),y-(image height(addImage)/2),1
make memblock from bitmap mem,bitmap
make image from memblock image,mem
set current bitmap 0
Endfunction
Function getCollisionPoint(x as dword ,y as dword )
pixel = (y * memblock dword(level.collisionMemBlock, 0)) + x
color = memblock dword(level.collisionMemBlock, 12 + (pixel * 4))
Endfunction color
function getMemblockPixel(mem as dword, x as dword, y as dword)
if memblock dword(mem, 8) <> 32
`print memblock dword(mem, 8) + \"-bit images not supported!\"
exitfunction 0
endif
if x >= memblock dword(mem, 0) or y >= memblock dword(mem, 4)
`print \"The specified pixel is out of the memblock image\'s bounds!\"
exitfunction 0
endif
Media and an exe are attached
pixel = (y * memblock dword(mem, 0)) + x
colour = memblock dword(mem, 12 + (pixel * 4))
endfunction colour