Hi, I tried the tiled importers in our forums, they work great for maps with 64x64 but I'm my case I need support for huge maps.
So I was writing a little code and it works fine streaming a map of 400x400 tiles
Now I'm having some issues to scroll the view with SetViewOffset; I would like to move the player sprite around the world. Any clue how could I do that?
// Project: Simple Map Streamer
// Created: 2017-12-29
// Author: nonom
SetErrorMode(2)
SetWindowTitle( "Simple Map Streamer" )
SetWindowSize( 768, 1024, 0 )
SetDisplayAspect( 768 / 1024.0)
SetWindowAllowResize( 1 )
SetVirtualResolution( 768 / 32, 1024 / 32)
SetSyncRate(60, 0 )
SetScissor( 0,0,0,0 )
//Water color
SetClearColor(0,0,255)
#option_explicit
#constant MAP_WIDTH = 472
#constant MAP_HEIGHT = 400
#constant TILE_SCALE = 1
global tile as integer
global offsetx as integer
global offsety as integer
global clickPointer as integer
global playerSpriteId as integer
global tweenId as integer
global map as integer[MAP_WIDTH, MAP_HEIGHT]
global colors as integer[2, 2]
global wx as integer
global wy as integer
global i, j, i1, i2, j1, j2 as integer
global k, l, m, n as integer
global offx#, offy# as float
global startx#, starty# as float
global xstart#, ystart# as float
global fin as integer = 0
global draw as integer = 0
global gameState as integer = 1
// Create tile base
tile = CreateSprite(0)
SetSpriteSize(tile, TILE_SCALE, TILE_SCALE)
SetSpriteTransparency(tile, 0)
//Load a CSV Map
Map = LoadMap(Map,"TestMap.csv")
// Stream zone
offsetx = (GetVirtualWidth() * TILE_SCALE) / 2
offsety = (GetVirtualHeight() * TILE_SCALE) / 2
//Grass
colors[0,0] = 0
colors[0,1] = 250
colors[0,2] = 0
//Foam
colors[1,0] = 0
colors[1,1] = 250
colors[1,2] = 250
//Sand
colors[2,0] = 240
colors[2,1] = 250
colors[2,2] = 184
do
select gameState:
case 1:
Init()
gameState = 2
endcase
case 2:
StreamMap()
ClickToMove()
UpdateView()
endcase
endselect
UpdateAllTweens(GetFrameTime())
PrintC ( Str(ScreenToWorldX(GetPointerX()),0) + " : " + Str(ScreenToWorldX(GetPointerY()),0) + " / ")
Print ( ScreenFPS() )
Sync()
loop
function Init()
playerSpriteId = CreateSprite ( 0 )
SetSpriteSize ( playerSpriteId, TILE_SCALE, -1 )
SetSpritePosition ( playerSpriteId, GetVirtualWidth() / 2, GetVirtualHeight() / 2 )
SetSpriteColor(playerSpriteId, 255, 0, 0, 255)
tweenId = CreateTweenSprite(2.2)
clickPointer = CreateSprite(0)
SetSpriteSize(clickPointer, TILE_SCALE, TILE_SCALE)
SetSpriteColorAlpha(clickPointer, 50)
//SetSpriteVisible(clickPointer, 0)
endfunction
function StreamMap()
// Offsets
i1 = GetSpriteX(playerSpriteId) - offsetx
i2 = GetSpriteX(playerSpriteId) + offsetx
j1 = GetSpriteY(playerSpriteId) - offsety
j2 = GetSpriteY(playerSpriteId) + offsety
if i1 < 0 then i1 = 0
if j1 < 0 then j1 = 0
for i = i1 to i2 step TILE_SCALE
for j = j1 to j2 step TILE_SCALE
// Coloring
k = i + TILE_SCALE // right of i
l = j + TILE_SCALE // bottom of j
m = i - TILE_SCALE // left of i
n = j - TILE_SCALE // top of j
// Prevent any array out of bounds
if m < 0 then m = 0
if n < 0 then n = 0
if k < MAP_WIDTH and l < MAP_HEIGHT
draw = 0
// Terrain
select map[i,j]
case 0
SetSpriteColor(tile,colors[0,0],colors[0,1],colors[0,2],255)
if (map[i,l] = 1 or map[i,n] = 1 or map[k,j] = 1 or map[m,j] = 1)
SetSpriteColor(tile,colors[2,0],colors[2,1],colors[2,2],255)
endif
draw = 1
endcase
case 1
if (map[i,l] = 0 or map[i,n] = 0 or map[k,j] = 0 or map[m,j] = 0)
SetSpriteColor(tile,colors[1,0],colors[1,1],colors[1,2],255)
draw = 1
endif
endcase
endselect
if draw
SetSpritePosition(tile, i - TILE_SCALE, j - TILE_SCALE)
SetSpriteSize(tile, TILE_SCALE, TILE_SCALE)
DrawSprite(tile)
endif
endif
next j
next i
endfunction
function ClickToMove()
if GetPointerPressed() and not fin
startx# = GetSpriteX(playerSpriteId)
starty# = GetSpriteY(playerSpriteId)
wx = ScreenToWorldX(GetPointerX())
wy = ScreenToWorldY(GetPointerY())
SetSpritePosition(clickPointer, wx, wy)
SetTweenSpriteX(tweenId,GetSpriteX(playerSpriteId),wx,TweenSmooth1())
SetTweenSpriteY(tweenId,GetSpriteY(playerSpriteId),wy,TweenSmooth1())
PlayTweenSprite(tweenId,playerSpriteId, 0)
fin = 1
endif
endfunction
function UpdateView()
if fin
offx# = xstart# - (startx# - GetSpriteX(playerSpriteId))
offy# = ystart# - (starty# - GetSpriteY(playerSpriteId))
SetViewOffset(offx#, offy#)
if not GetTweenSpritePlaying(tweenId, playerSpriteId)
xstart# = offx#
ystart# = offy#
fin = 0
endif
endif
endfunction
function LoadMap (arr as integer[][], filename as string)
ix as integer = 0
delimiters$ as string = ","
fi as integer
line$ as string
fi = OpenToRead(filename)
line$ = ReadLine(fi)
while FileEOF(fi) <> 1
tokens as integer
tokens = CountStringTokens(line$,delimiters$)
c as integer
for c = 0 to tokens - 1
num as integer
num = Val(GetStringToken(line$,delimiters$,c))
arr[c,ix] = 1
if num = 0
arr[c,ix] = 0
endif
next c
line$ = ReadLine(fi)
inc ix
endwhile
CloseFile(fi)
endfunction arr
TestMap.csv attached
Happy new 2018 mates!