Quote: "I think you should put the editor into its own agc file. It may be easier to figure out. "
It whas my idea from the beginning but because i constantly change parts ,so did i not put to much effort on the editor
But i improved the editor slightly today so its easier for you to use and understand.
And its now in its own agc file.
Quote: "I have been thinking about how to call the wall texture so it just doesn't produce a random texture but one you want. I have a solution and will code it up but I don't know if I will have time to get it done before the middle of next week."
I have actually done that for you so look at the code and use the parts you want?
I will upload a newer batch of files soon when i have done more changes.
I havent worked so much on it since the last release
this is how my main file looks now.
remstart
////////////////////////////////////////////////////////////////////////////////
// Raycast engine for AGK //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/////// Open source ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
By
Cliff Mellangård
Sweden
Forum id
Cliff Mellangard 3DEGS
////////////////////////////////////////////////////////////////////////////////
Link to usefull sites. /////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
http://www.permadi.com/tutorial/raycast/index.html
http://dev.opera.com/articles/view/creating-pseudo-3d-games-with-html-5-can-1/
http://web.archive.org/web/20070101034811/http://student.kuleuven.be/~m0216922/CG/raycasting.html
////////////////////////////////////////////////////////////////////////////////
remend
global AngDummy
AngDummy = CreateSprite ( 0 )
SetSpritePosition ( AngDummy, -1000, -1000 )
global temp#
SetSyncRate ( 0, 0 )
SetVirtualResolution ( 320, 240 )
setorientationallowed(0,0,1,0)
gosub Optimizer
global IMG
IMG = LoadImage ( "AlienWalls.png" )
global IMG_Door
IMG_Door = LoadImage ( "Door.png" )
global IMG_Back
IMG_Back = LoadImage ( "BackDark.png" )
global IMG_3D_Sprites
IMG_3D_Sprites = LoadImage ( "AlienSprites.png" )
// number of map blocks in x-direction
#constant mapWidth 32
// number of map blocks in y-direction
#constant mapHeight 24
// how many pixels to draw a map block
#constant miniMapScale 2
#constant WorldScale 64
// The same as minimap scale and is only used to get the right texture column for the screen column
#constant TextureScale 128
// NEW ////////////////////////////////////////////////////////////////
#Constant FALSE 0
#Constant TRUE 1
type sMap
_Type as integer
IMG_Top as integer
IMG_Bottom as integer
IMG_Left as integer
IMG_Right as integer
_3DSprite as integer
Height as integer
endtype
global dim map [mapWidth,mapHeight] as sMap
#constant Rc_Wall 1
#constant Rc_Door 2
#constant Rc_Barrel 4
#constant Rc_Light 3
#constant Rc_Pillar 5
#constant Rc_HitLeft 1
#constant Rc_HitRight 2
#constant Rc_HitTop 3
#constant Rc_HitBottom 4
// we dont nead to raycast from the players actual position but from the view planes position
#constant ViewPlane 0.32
global Rc_HitFace
global Rc_HitX
global Rc_HitY
//Random_Map ()
// Number of rays we use
#Constant numRays 160
// this is 60/numrays
#Constant rayspeed 0.375
#Constant PI 3.1415926535
// not used yet
// distance in grid blocks
global View_Distance = 15
// screen width divided by numrays
#constant Column_Width 2
// Create our rays //////////////////
type sRay
x as float
y as float
Angle as float
Dot as integer
Stop as integer
endtype
global dim Ray[numRays] as sRay
remstart
for t=0 to numRays
i = CreateSprite ( 0 )
SetSpriteSize ( i , Column_Width , 2 )
SetSpritePosition ( i, -100, -100 )
SetSpriteColor ( i, 0 , 0 , 0 , 255 )
setspritedepth(i,0)
Ray [t].Dot=i
next t
remend
i = CreateSprite ( IMG_Back )
SetSpritePosition ( i , 5 , 0 )
SetSpriteSize ( i , Column_Width*numRays , 240 )
setspritedepth(i,10000)
global dim MiniMap [mapWidth*mapHeight]
remstart
for t=1 to (mapWidth*mapHeight)
i = CreateSprite ( 0 )
SetSpriteSize ( i , miniMapScale , miniMapScale )
SetSpriteColor ( i, 255 , 255 , 255 , 255 )
SetSpriteTransparency( i , 0 )
setspritedepth(i,0)
Block[t]=i
next t
remend
//gosub Setup_Linecast
gosub Setup_Wall
gosub Setup_3D_Sprites
gosub Setup_Chunky_Floor
#include "Wall_Renderer.agc"
#include "3Dsprite_Renderer.agc"
//#include "Floor_Ceiling_Renderer.agc"
#include "Raycast_Floor_Ceiling.agc"
#include "Misc_Raycast.agc"
#include "Optimize.agc"
// NEW /////////////////////////////////////////////////////////////
#include "Map_Editor.agc"
/////////////////////////////////////////
// Create our player ////////////////////
////////////////////////////////////////
type sPlayer
ID as integer
x as float
y as float
xWorld as float
yWorld as float
dir as integer
rot as float
speed as integer
moveSpeed as float
rotSpeed as float
endtype
global Player as sPlayer
// current x, y position of the player
Player.x = 1.0 : Player.y = 1.0
// the direction that the player is turning, either -1 for left or 1 for right.
Player.dir = 0
// the current angle of rotation
Player.rot = 90.0
// is the playing moving forward (speed = 1) or backwards (speed = -1).
Player.speed = 0
// how far (in map units) does the player move each step/update( its this number * frame time )
Player.moveSpeed = 5.0
// old ////// ( Number math.PI ) how much does the player rotate each step/update (in radians)
Player.rotSpeed = 120.0
// New ////////////////////////////////////////////////////////////////
/////////////////////////////////////
// Editor types ////////////////////
////////////////////////////////////
Type sEditor
Active as integer
Save_Text as float
Endtype
global RC_Editor as sEditor
// z key
#constant RC_Editor_ON 90
// x key
#constant RC_Editor_OFF 88
/////////////////////////////////////////////////////////////////////////
i = CreateSprite ( 0 )
SetSpriteSize ( i , miniMapScale , miniMapScale )
SetSpriteColor ( i, 0 , 0 , 0 , 255 )
SetSpritePosition ( i, Player.x*miniMapScale, Player.y*miniMapScale )
setspritedepth(i,0)
Player.ID = i
global Draw_Priority
global debugg
global spritehere
global spritestor#
AddVirtualJoystick( 1, 0+32, 240-32, 64 )
AddVirtualJoystick( 2, 320-32, 240-32, 64 )
if GetKeyboardExists() =1
SetVirtualJoystickActive( 1, 0 )
SetVirtualJoystickActive( 2, 0 )
SetVirtualJoystickVisible( 1, 0 )
SetVirtualJoystickVisible( 2, 0 )
endif
Global World_Center=120
Map_File( "load" , "temp.map" )
Draw_MiniMap (miniMapScale ,miniMapScale ,( 160 - ( ( mapWidth / 2 ) * miniMapScale ) ),( 240 - ( mapHeight * miniMapScale ) ))
global GFT#
Move_Player ()
do
GFT# = GetFrameTime()
Check_keys ()
if Player.speed <> 0 or Player.dir <> 0 then Move_Player ()
//print(str(player.x))
//print(str(player.y))
select ( Draw_Priority )
case 0:
Render_Walls(1,numRays,WrapAngle(player.rot - 30.0),30,0.06,7.0)
Chunky_Floor_Ceiling( 1 , 1 , 1 , 0.06 , 7.0 )
Render_3DSprites()
endcase
case 1:
endcase
endselect
inc Draw_Priority
if Draw_Priority>1 then Draw_Priority=0
Draw_Priority=0
// NEW /////////////////////////////////////////////////////////
if GetRawKeyPressed( RC_Editor_ON )
Draw_MiniMap (miniMapScale ,miniMapScale ,( 160 - ( ( mapWidth / 2 ) * miniMapScale ) ),( 240 - ( mapHeight * miniMapScale ) ))
SetSpriteSize ( Player.ID , miniMapScale , miniMapScale )
RC_Editor.Active = FALSE
endif
if RC_Editor.Active = FALSE
SetSpritePosition ( Player.ID, (160-((mapWidth/2)*miniMapScale))+(Player.x*miniMapScale), (240-(mapHeight*miniMapScale))+(Player.y*miniMapScale ))
endif
if GetRawKeyPressed( RC_Editor_OFF )
Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0)
SetSpriteSize ( Player.ID , miniMapScale*2 , miniMapScale*2 )
RC_Editor.Active = TRUE
endif
if RC_Editor.Active = TRUE then Real_Time_Editor()
rem if GetRawMouseLeftState() then inc World_Center
rem if GetRawMouseRightState() then dec World_Center
//print (str(spritestor#))
if RC_Editor.Active = FALSE
print("Fps = "+str( Screenfps() ) )
print("Chunky spr = "+str(Floor_count-FirstFloor_SPR))
print("Spr draw count = "+str(GetManagedSpriteDrawnCount()))
endif
//////////////////////////////////////////////////////////////////
// escape on computer and back button on device
if GetRawKeyState(27)=1 then end
Sync()
loop
function Check_keys ()
Player.speed = 0
Player.dir = 0
if GetKeyboardExists() <>1
if GetVirtualJoystickY( 1 )<0 then Player.speed = 1
if GetVirtualJoystickY( 1 )>0 then Player.speed = -1
if GetVirtualJoystickX( 2 )<0 then Player.dir = -1
if GetVirtualJoystickX( 2 )>0 then Player.dir = 1
endif
if GetKeyboardExists() =1
if GetRawKeyState(38)=1 then Player.speed = 1
if GetRawKeyState(40)=1 then Player.speed = -1
if GetRawKeyState(39)=1 then Player.dir = 1
if GetRawKeyState(37)=1 then Player.dir = -1
endif
endfunction
function Move_Player ()
// player will move this far along the current direction vector
moveStep# = player.speed * (player.moveSpeed*GFT#)
// add rotation if player is rotating (player.dir != 0)
player.rot = WrapAngle(player.rot+(player.dir * (player.rotSpeed*GFT#)))
// calculate new player position with simple trigonometry
newX# = player.x + (cos(player.rot) * moveStep#)
newY# = player.y + (Sin(player.rot) * moveStep#)
newX = player.x + (cos(player.rot) * (player.speed*0.5))
newY = player.y + (Sin(player.rot) * (player.speed*0.5))
// set new position
y=player.y
if map [newX,y]._Type <>1 then player.x = newX#
x=player.x
if map [x,newY]._Type <>1 then player.y = newY#
player.xWorld = player.x*WorldScale
player.yWorld = player.y*WorldScale
endfunction
function InView(x#,y#,x2#,y2#)
sprite_new_angle# = atanfull(x2#-x#,y2#-y#)
//SetSpriteAngle(id,sprite_new_angle#)
endfunction sprite_new_angle#
/////////////////////////////////////////////////////////////////////
function Draw_MiniMap (scalex,scaley,Screen_X,Screen_Y)
// Clean it up if the sprite exists
for t=0 to ( mapWidth * mapHeight )
if GetSpriteExists( MiniMap[ t ] ) = 0 then exit
deletesprite( MiniMap[ t ] )
next t
Cube = 0
// Generate mini map background
spr = CreateSprite ( 0 )
SetSpriteSize ( spr , scalex * mapWidth , scaley * mapHeight )
SetSpriteTransparency( spr , 0 )
setspritedepth( spr ,0)
SetSpritePosition ( spr , Screen_X , Screen_Y )
SetSpriteColor ( spr , 255 , 255 , 255 , 255 )
MiniMap[ Cube ] = spr
inc Cube
// Generate the rest of the map cubes
for y = 0 to mapHeight-1
for x = 0 to mapWidth-1
if map [ x , y ]._Type >= 1 and map [ x , y ]._Type <= 5
spr = CreateSprite ( 0 )
SetSpriteSize ( spr , scalex , scaley )
SetSpriteTransparency( spr , 0 )
setspritedepth( spr ,0)
if map [ x , y ]._Type = 1 then SetSpriteColor ( spr , 155 , 155 , 155 , 255 )
if map [ x , y ]._Type = 2 then SetSpriteColor ( spr , 55 , 155 , 155 , 255 )
if map [ x , y ]._Type = 3 then SetSpriteColor ( spr , 155 , 55 , 155 , 255 )
if map [ x , y ]._Type = 4 then SetSpriteColor ( spr , 155 , 155 , 55 , 255 )
if map [ x , y ]._Type = 5 then SetSpriteColor ( spr , 55 , 155 , 55 , 255 )
SetSpritePosition ( spr , Screen_X + (x*scalex), Screen_Y + (y*scaley) )
MiniMap[ Cube ] = spr
inc Cube
//else
//SetSpriteColor ( Block[rect], 255 , 255 , 255 , 255 )
//SetSpritePosition ( Block[rect], scrX+x*miniMapScale, scrY+y*miniMapScale )
//inc rect
endif
next x
next y
endfunction
This is the editor file named = Map_Editor.agc
Function Real_Time_Editor()
// Tab key saves map file
if GetRawKeyPressed( 9 ) then Map_File( "save" , "temp.map" )
SetSpritePosition ( Player.ID, (Player.x*(miniMapScale*2)), (Player.y*(miniMapScale*2)))
msx=(GetRawMouseX()-miniMapScale)/(miniMapScale*2)
msy=(GetRawMousey()-miniMapScale)/(miniMapScale*2)
oldKey=GetRawLastKey( )
// 1
if GetRawKeyPressed( 49 ) : map [ msx , msy ]._Type = Rc_Barrel : Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0) : endif
// 2
if GetRawKeyPressed( 50 ) : map [ msx , msy ]._Type = Rc_Light : Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0) : endif
// 3
if GetRawKeyPressed( 51 ) : map [ msx , msy ]._Type = Rc_Pillar : Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0) : endif
// D
if GetRawKeyPressed( 68 ) : map [ msx , msy ]._Type = Rc_Door : Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0) : endif
// Create wall
if GetRawMouseLeftPressed() and msx=<mapWidth and msy=<mapHeight
map [ msx , msy ]._Type = Rc_Wall
Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0)
endif
remstart
// This handles wath texture will be displayed on the wall sides
// its the textures first frame that is used.
// If you want texture 2 so simply write TextureScale
// If you want texture 3 so simply write 2*TextureScale
map [ msx , msy ].IMG_Top = 0
map [ msx , msy ].IMG_Bottom = 0
map [ msx , msy ].IMG_Left = 0
map [ msx , msy ].IMG_Right = 0
remend
// q
if GetRawKeyPressed( 81 ) : map [ msx , msy ].IMG_Top = map [ msx , msy ].IMG_Top+TextureScale : endif
// w
if GetRawKeyPressed( 87 ) : map [ msx , msy ].IMG_Bottom = map [ msx , msy ].IMG_Bottom+TextureScale : endif
// e
if GetRawKeyPressed( 69 ) : map [ msx , msy ].IMG_Left = map [ msx , msy ].IMG_Left+TextureScale : endif
// r
if GetRawKeyPressed( 82 ) : map [ msx , msy ].IMG_Right = map [ msx , msy ].IMG_Right+TextureScale : endif
// delete wall
if GetRawMouseRightPressed() and msx=<mapWidth and msy=<mapHeight
map [ msx , msy ]._Type = 0
map [ msx , msy ].IMG_Top = 0
map [ msx , msy ].IMG_Bottom = 0
map [ msx , msy ].IMG_Left = 0
map [ msx , msy ].IMG_Right = 0
Draw_MiniMap (miniMapScale*2 ,miniMapScale*2 ,0,0)
endif
// Spacekey gives your map random wall textures on each face
if GetRawKeyPressed( 32 )
For x = 0 to MapWidth
For y = 0 to MapHeight
if map [ x , y ]._Type = Rc_Wall
map [ x , y ].IMG_Top = random(0,2)*TextureScale
map [ x , y ].IMG_Bottom = random(0,2)*TextureScale
map [ x , y ].IMG_Left = random(0,2)*TextureScale
map [ x , y ].IMG_Right = random(0,2)*TextureScale
endif
Next y
Next x
endif
print(" Fps = "+str( Screenfps() ) )
print(" Tab = saves file")
print(" Z = Exit editor")
print(" 1 = Barrel")
print(" 2 = Light")
print(" 3 = Pillar")
print(" D = Door")
print(" SPCK = Random wall textures")
// File saved info text
if RC_Editor.Save_Text>0.0
RC_Editor.Save_Text = RC_Editor.Save_Text-GFT#
print(" >>>...FILE SAVED !...<<< )")
endif
Endfunction
Function Map_File( state as string , file_io as string )
If GetFileExists( file_io )=0 and state = "load"
// Auto generate a border of walls around the map
for x = 0 to mapWidth-1
for y = 0 to mapHeight-1
if x=0 or x=mapWidth-1 then map [ x , y ]._Type = Rc_Wall
if y=0 or y=mapHeight-1 then map [ x , y ]._Type = Rc_Wall
next y
next x
ExitFunction
endif
If state = "load" then OpenToRead ( 1 , file_io )
If state = "save" then OpenToWrite ( 1, file_io, 0 )
For x = 0 to MapWidth
For y = 0 to MapHeight
If state = "load"
Map [ x , y ]._Type = ReadInteger ( 1 )
map [ x , y ].IMG_Top = ReadInteger ( 1 )
map [ x , y ].IMG_Bottom = ReadInteger ( 1 )
map [ x , y ].IMG_Left = ReadInteger ( 1 )
map [ x , y ].IMG_Right = ReadInteger ( 1 )
Endif
If state = "save"
WriteInteger ( 1 , Map [ x , y ]._Type )
WriteInteger ( 1 , map [ x , y ].IMG_Top)
WriteInteger ( 1 , map [ x , y ].IMG_Bottom)
WriteInteger ( 1 , map [ x , y ].IMG_Left)
WriteInteger ( 1 , map [ x , y ].IMG_Right)
Endif
Next y
Next x
CloseFile ( 1 )
// set the save text info timer
If state = "save" then RC_Editor.Save_Text = GFT#*100.0
EndFunction
If you insert it right so should it look like this in editor mode and give you an small pop up text when it saves the map file.
I marked the places with new in the main file so you now wath parts i changed