// Project: CNP Basic Movement snippet
// Alex Schmidt "Jack" 2015
// set window properties
SetWindowTitle( "CNP Basic Movement snippet" )
SetWindowSize( 1280, 720, 0 )
// set display properties
SetVirtualResolution( 1280, 720 )
SetOrientationAllowed( 1, 1, 1, 1 )
// define some types
type engine_def // main engine type
pntx# as float // pointer x as float
pnty# as float // pointer y as float
pntspr as integer // dummy sprite for the pointer
endtype
type player_def // main player type
px# as float // player position x
py# as float // player position y
tgx# as float // player target x
tgy# as float // player target y
ang# as float // player angle to target
spr as integer // dummy sprite for the player
endtype
// generate arrays
dim engine[0] as engine_def
dim player[0] as player_def
// this will start the engine, basically it will create the pointer sprite
init_engine()
// this will create the player and position it to the given coordinates
create_player(150.0,150.0)
// main game loop
do
// game functions (I hope they will explain themself)
handle_input()
handle_player()
// some output
print("Pointer x: "+str(engine[0].pntx#))
print("Pointer y: "+str(engine[0].pnty#))
Print("FPS: "+str(ScreenFPS()) )
Sync()
loop
function init_engine()
// create the debug picker sprite
spr=CreateSprite(0)
SetSpriteSize(spr,8,8)
SetSpriteOffset(spr,4,4)
SetSpriteColor(spr,255,0,0,255)
SetSpriteDepth(spr,0)
engine[0].pntspr=spr
endfunction
function handle_player()
if player[0].px#<>player[0].tgx# or player[0].py#<>player[0].tgy# // this will check, if the target has changed, if so, then the following code will be activated
spr=player[0].spr
// if the angle <0 or (-1) then the following calculation will take place:
if player[0].ang#<0.0
// calculate the angle between the player and the target:
dx#=player[0].tgx#-player[0].px#
dy#=player[0].tgy#-player[0].py#
ang#=atanfull(dx#,dy#)
else
ang#=player[0].ang# // else the given angle inside the player array will be used
endif
SetSpritePosition(spr,player[0].px#+sin(ang#)*2.0,player[0].py#-cos(ang#)*2.0)
print("moving")
player[0].px#=GetSpriteX(spr)
player[0].py#=GetSpriteY(spr)
SetSpritePositionByOffset(spr,player[0].px#,player[0].py#) // reposition the sprite by the given offset
// once the player is near the target, the movement is completed
if GetDistance#(player[0].px#,player[0].py#,player[0].tgx#,player[0].tgy#)<1.0
player[0].tgx#=player[0].px#
player[0].tgy#=player[0].py#
player[0].ang#=-1
endif
endif
endfunction
function create_player(x#,y#)
// create a default player sprite box
spr=createsprite(0)
SetSpriteSize(spr,32,32)
SetSpriteOffset(spr,16,16)
SetSpritePositionbyOFfset(spr,x#,y#)
// define the player variables in the code
player[0].spr=spr
player[0].px#=x#
player[0].py#=y#
player[0].tgx#=x#
player[0].tgy#=y#
player[0].ang#=-1.0 // angle should be -1, so the code will know, that its not calculated yet
endfunction
function handle_input()
// once the main pointer is pressed...
if GetPointerReleased()=1
engine[0].pntx#=GetPointerX()
engine[0].pnty#=GetPointerY()
// reposition the pointer sprite
SetSpritePositionByOffset(engine[0].pntspr,engine[0].pntx#,engine[0].pnty#)
// change the target of the player
player[0].tgx#=engine[0].pntx#
player[0].tgy#=engine[0].pnty#
endif
endfunction
// 2d distance function
Function GetDistance#( X1#, Y1#, X2#, Y2#)
Dist_X# = X1# - X2#
Dist_Y# = Y1# - Y2#
Distance# = Sqrt(Dist_X# * Dist_X# + Dist_Y# * Dist_Y#)
EndFunction Distance#
// create a sprite that will point towards the pointer sprite = CreateSprite(0) SetSpriteSize(sprite, 1, 10) SetSpriteOffset(sprite, 0.5, 10) SetSpritePositionByOffset(sprite, 50, 50) do // get the current pointer position x# = GetPointerX() y# = GetPointerY() // calculate the angle between the origin of the sprite and the pointer using atanfull angle# = ATanFull(x# - GetSpriteXByOffset(sprite), y# - GetSpriteYByOffset(sprite)) // set the angle of the sprite to point towards the pointer SetSpriteAngle(sprite, angle#) // update the screen Sync() loop