rem
rem AGK Application 2.0.14b
rem MR 31.07.2015
//Jump over the Box + Ladder Example 2.0
SetDisplayAspect( 4.0/3.0 )
setvirtualresolution(10,10) //10x10 Meter
setphysicsscale(1) //belongs resolution! default 1 unit = 1 meter
SetPhysicsGravity(0 , 50)
setphysicsdebugon()
#constant groupground 1
#constant groupobject 2
#constant groupladder 3
//Player
spr = CreateSprite(0)
SetSpriteSize (spr,0.5,2)
SetSpritePosition(spr,2.5,6)
SetSpritePhysicsOn(spr,2)
SetSpriteDepth(spr,0)
SetSpritePhysicsCanRotate (spr,0)
SetSpritePhysicsMass (spr,60)
setspritephysicsisbullet(spr,1)
SetSpritePhysicsFriction(spr,0.1)
SetSpritePhysicsRestitution(spr,0.25)
SetSpritePhysicsDamping(spr,0.25)
MakeBox(0 ,6,10-2,1,-1)
MakeGround(0, 0,10-0.05,10,0.2)
MakeGround(0, 0,5,5,0.2)
MakeGround(0, 6,5,4,0.2)
MakeLadder(0, 5,4.5,1,5.5)
do
print("Use Cursor Left/Right, Space for Jump")
// Move Player to Left and Right
ix#=0.0
iy#=0.0
if GetRawKeyState(39) = 1
ix#=50.0
elseif GetRawKeyState(37) = 1
ix#=-50.0
endif
//Check was is below Feet
belowfeet=FeetAt(spr,0,0.1)
//print("belowfeet:" + str(belowfeet))
feet=FeetAt(spr,groupladder,-0.1)
//print("feet:" + str(feet))
//Jump
if GetRawKeypressed(32) = 1
if AtGround(belowfeet)=1 then iy#=-750.0
endif
if 1=1
ladder=AtLadder(feet)
//print(ladder)
if ladder<>0 and abs(timer()-ladderdelay#)>1.0
if stayatladder=0
if getrawkeystate(38)=1 or getrawkeystate(40)=1 or GetSpritePhysicsVelocityY(spr)>1.0
stayatladder=1
joint=CreateMouseJoint( spr,getspritexbyoffset(spr), getspriteybyoffset(spr), 5000 )
SetJointMouseTarget(joint,getspritexbyoffset(feet), getspriteybyoffset(spr))
endif
endif
x#=getspritexbyoffset(feet)
y#=getspriteybyoffset(spr)
drawline(x#,0,x#,10, 255,255,0)
endif
//ladder mode
if stayatladder=1
if getrawkeystate(38)=1 and feet<>0 //Up
SetJointMouseTarget(joint,getspritexbyoffset(spr), getspriteybyoffset(spr)-0.25)
elseif getrawkeystate(40)=1 and feet<>0 //Down
SetJointMouseTarget(joint,getspritexbyoffset(spr), getspriteybyoffset(spr)+0.25)
elseif getrawkeypressed(39)=1 or getrawkeypressed(37)=1 or feet=0 //Links Rechts
if stayatladder=1
deletejoint(joint)
joint=0
stayatladder=0
ix#=ix# / 2.0
ladderdelay#=timer()
endif
else
endif
endif
endif
if ix#<>0.0 or iy#<>0.0 then setspritephysicsimpulse(spr,getspritexbyoffset(spr),getspriteybyoffset(spr),ix#,iy#)
Sync()
loop
end
function FeetAt(spr,group,plusy#)
//feet position
x# = GetWorldXFromSprite(spr,0,GetSpriteHeight(spr)/2.0+plusy#)
y# = GetWorldYFromSprite(spr,0,GetSpriteHeight(spr)/2.0+plusy#)
DrawEllipse(x#,y#,.1,.1,MakeColor(255,255,0),MakeColor(255,0,0),1) //visible point for debug
if group //sprite filter by group
spr=GetSpriteHitGroup(group,x#,y#) //for not hit the player itself such at ladder
else
spr=GetSpriteHit(x#,y#)
endif
endfunction spr
function AtGround(spr)
ret=0
if spr=0 then exitfunction ret
group=getspritegroup(spr)
if group=groupobject or group=groupground
ret=1
endif
endfunction ret
function AtLadder(spr)
ret=0
if spr=0 then exitfunction ret
group=getspritegroup(spr)
if group=groupladder
ret=1
endif
endfunction ret
function MakeGround(img,x#,y#,w#,h#)
//Ground
spr = CreateSprite(img)
SetSpriteColor (spr,128,128,128,255)
SetSpriteSize (spr,w#,h#)
SetSpritePosition(spr,x#,y#)
SetSpritePhysicsON(spr,1)
SetSpritePhysicsMass(spr,2)
SetSpritePhysicsCanRotate(spr,0)
SetSpritePhysicsFriction(spr,0.75)
SetSpritePhysicsRestitution(spr,0.5)
SetSpritePhysicsDamping(spr,0.5)
SetSpriteGroup( spr, groupground )
endfunction spr
function MakeLadder(img,x#,y#,w#,h#)
//Ladder
spr = CreateSprite(img)
SetSpriteColor (spr,128,128,128,255)
SetSpriteSize (spr,w#,h#)
SetSpritePosition(spr,x#,y#)
SetSpritePhysicsON(spr,1)
setspritephysicsissensor(spr,1)
SetSpritePhysicsMass(spr,2)
SetSpritePhysicsCanRotate(spr,0)
SetSpritePhysicsFriction(spr,0.75)
SetSpritePhysicsRestitution(spr,0.5)
SetSpritePhysicsDamping(spr,0.5)
SetSpriteGroup( spr, groupladder )
endfunction spr
function MakeBox(img,x#,y#,w#,h#)
//Box
box = CreateSprite(img)
SetSpriteColor (box,233,75,80,255)
SetSpriteSize (box,w#,h#)
SetSpritePosition(box,x#,y#)
SetSpritePhysicsON(box,2)
SetSpritePhysicsMass (box,2)
SetSpritePhysicsCanRotate(box,1)
SetSpritePhysicsFriction(box,0.75)
SetSpritePhysicsRestitution(box,0.25)
SetSpritePhysicsDamping(box,0.5)
SetSpriteGroup( box, groupobject )
endfunction box
Help make AGK better by submitting an example for this command! (All examples are subject to approval)
rem rem AGK Application 2.0.14b rem MR 31.07.2015 //Jump over the Box + Ladder Example 2.0 SetDisplayAspect( 4.0/3.0 ) setvirtualresolution(10,10) //10x10 Meter setphysicsscale(1) //belongs resolution! default 1 unit = 1 meter SetPhysicsGravity(0 , 50) setphysicsdebugon() #constant groupground 1 #constant groupobject 2 #constant groupladder 3 //Player spr = CreateSprite(0) SetSpriteSize (spr,0.5,2) SetSpritePosition(spr,2.5,6) SetSpritePhysicsOn(spr,2) SetSpriteDepth(spr,0) SetSpritePhysicsCanRotate (spr,0) SetSpritePhysicsMass (spr,60) setspritephysicsisbullet(spr,1) SetSpritePhysicsFriction(spr,0.1) SetSpritePhysicsRestitution(spr,0.25) SetSpritePhysicsDamping(spr,0.25) MakeBox(0 ,6,10-2,1,-1) MakeGround(0, 0,10-0.05,10,0.2) MakeGround(0, 0,5,5,0.2) MakeGround(0, 6,5,4,0.2) MakeLadder(0, 5,4.5,1,5.5) do print("Use Cursor Left/Right, Space for Jump") // Move Player to Left and Right ix#=0.0 iy#=0.0 if GetRawKeyState(39) = 1 ix#=50.0 elseif GetRawKeyState(37) = 1 ix#=-50.0 endif //Check was is below Feet belowfeet=FeetAt(spr,0,0.1) //print("belowfeet:" + str(belowfeet)) feet=FeetAt(spr,groupladder,-0.1) //print("feet:" + str(feet)) //Jump if GetRawKeypressed(32) = 1 if AtGround(belowfeet)=1 then iy#=-750.0 endif if 1=1 ladder=AtLadder(feet) //print(ladder) if ladder<>0 and abs(timer()-ladderdelay#)>1.0 if stayatladder=0 if getrawkeystate(38)=1 or getrawkeystate(40)=1 or GetSpritePhysicsVelocityY(spr)>1.0 stayatladder=1 joint=CreateMouseJoint( spr,getspritexbyoffset(spr), getspriteybyoffset(spr), 5000 ) SetJointMouseTarget(joint,getspritexbyoffset(feet), getspriteybyoffset(spr)) endif endif x#=getspritexbyoffset(feet) y#=getspriteybyoffset(spr) drawline(x#,0,x#,10, 255,255,0) endif //ladder mode if stayatladder=1 if getrawkeystate(38)=1 and feet<>0 //Up SetJointMouseTarget(joint,getspritexbyoffset(spr), getspriteybyoffset(spr)-0.25) elseif getrawkeystate(40)=1 and feet<>0 //Down SetJointMouseTarget(joint,getspritexbyoffset(spr), getspriteybyoffset(spr)+0.25) elseif getrawkeypressed(39)=1 or getrawkeypressed(37)=1 or feet=0 //Links Rechts if stayatladder=1 deletejoint(joint) joint=0 stayatladder=0 ix#=ix# / 2.0 ladderdelay#=timer() endif else endif endif endif if ix#<>0.0 or iy#<>0.0 then setspritephysicsimpulse(spr,getspritexbyoffset(spr),getspriteybyoffset(spr),ix#,iy#) Sync() loop end function FeetAt(spr,group,plusy#) //feet position x# = GetWorldXFromSprite(spr,0,GetSpriteHeight(spr)/2.0+plusy#) y# = GetWorldYFromSprite(spr,0,GetSpriteHeight(spr)/2.0+plusy#) DrawEllipse(x#,y#,.1,.1,MakeColor(255,255,0),MakeColor(255,0,0),1) //visible point for debug if group //sprite filter by group spr=GetSpriteHitGroup(group,x#,y#) //for not hit the player itself such at ladder else spr=GetSpriteHit(x#,y#) endif endfunction spr function AtGround(spr) ret=0 if spr=0 then exitfunction ret group=getspritegroup(spr) if group=groupobject or group=groupground ret=1 endif endfunction ret function AtLadder(spr) ret=0 if spr=0 then exitfunction ret group=getspritegroup(spr) if group=groupladder ret=1 endif endfunction ret function MakeGround(img,x#,y#,w#,h#) //Ground spr = CreateSprite(img) SetSpriteColor (spr,128,128,128,255) SetSpriteSize (spr,w#,h#) SetSpritePosition(spr,x#,y#) SetSpritePhysicsON(spr,1) SetSpritePhysicsMass(spr,2) SetSpritePhysicsCanRotate(spr,0) SetSpritePhysicsFriction(spr,0.75) SetSpritePhysicsRestitution(spr,0.5) SetSpritePhysicsDamping(spr,0.5) SetSpriteGroup( spr, groupground ) endfunction spr function MakeLadder(img,x#,y#,w#,h#) //Ladder spr = CreateSprite(img) SetSpriteColor (spr,128,128,128,255) SetSpriteSize (spr,w#,h#) SetSpritePosition(spr,x#,y#) SetSpritePhysicsON(spr,1) setspritephysicsissensor(spr,1) SetSpritePhysicsMass(spr,2) SetSpritePhysicsCanRotate(spr,0) SetSpritePhysicsFriction(spr,0.75) SetSpritePhysicsRestitution(spr,0.5) SetSpritePhysicsDamping(spr,0.5) SetSpriteGroup( spr, groupladder ) endfunction spr function MakeBox(img,x#,y#,w#,h#) //Box box = CreateSprite(img) SetSpriteColor (box,233,75,80,255) SetSpriteSize (box,w#,h#) SetSpritePosition(box,x#,y#) SetSpritePhysicsON(box,2) SetSpritePhysicsMass (box,2) SetSpritePhysicsCanRotate(box,1) SetSpritePhysicsFriction(box,0.75) SetSpritePhysicsRestitution(box,0.25) SetSpritePhysicsDamping(box,0.5) SetSpriteGroup( box, groupobject ) endfunction box