samples/JamesH/Characters
characters.abs
224 lines of ArcBasic. Words in colour that are commands link to their help page. To run it, download ArcBasic, open the file and press F5.
; Characters - cute animated characters;; Characters from Kenney's Mini Characters and Blocky Characters packs; (CC0, www.kenney.nl) are .glb models with their animations built in.; FindAnimSeq picks an animation by name ("idle", "walk", "sprint"...),; and Animate's transition blends smoothly from one to the next.;; Keys: Up/Down walk Left/Right turn Shift sprint Space jump; 1 attack 2 kick 3 yes 4 no C change character; Escape quitGraphics3D 1024,768,0,2SetBuffer BackBuffer()Const ARENA#=7 ; the arena runs from -ARENA to +ARENAType Wanderer Field entity, speed#, turn#End TypeType Coin Field entityEnd Type; ---- lights, camera ----camera=CreateCamera()CameraClsColor camera,140,200,245CameraRange camera,.05,100AmbientLight 120,125,140sun=CreateLight(1)RotateEntity sun,55,-40,0LightColor sun,255,245,225LightShadows sunShadowQuality 2CameraAmbientOcclusion camera,True,3,.4CameraGlow camera,True,.8,.9; ---- the arena ----For x=-3 To 3 For z=-3 To 3 b=LoadMesh("platformer\block-grass-large.glb") PositionEntity b,x*2.08,-1,z*2.08 NextNextfence=LoadMesh("platformer\fence-straight.glb") : HideEntity fenceFor i=-3 To 3 f=CopyEntity(fence) : PositionEntity f,i*2.08,0,7.6 f=CopyEntity(fence) : PositionEntity f,i*2.08,0,-7.6 f=CopyEntity(fence) : PositionEntity f,7.6,0,i*2.08 : TurnEntity f,0,90,0 f=CopyEntity(fence) : PositionEntity f,-7.6,0,i*2.08 : TurnEntity f,0,90,0NextScatter("platformer\tree.glb",4,1.5)Scatter("platformer\tree-pine.glb",4,1.5)Scatter("platformer\flowers.glb",14,.8)Scatter("platformer\mushrooms.glb",6,.6)Scatter("platformer\rocks.glb",5,.9)Scatter("platformer\crate.glb",3,.9)flag=LoadMesh("platformer\flag.glb") : PositionEntity flag,6.5,0,6.5coinModel=LoadMesh("platformer\coin-gold.glb") : ScaleEntity coinModel,.7,.7,.7 : HideEntity coinModelEntityGlow coinModel,.6Global score=0SpawnCoins(coinModel); ---- characters ----Dim character$(8)character$(0)="mini\character-knight.glb" : character$(1)="mini\character-female-a.glb"character$(2)="mini\character-male-b.glb" : character$(3)="mini\character-robot.glb"character$(4)="mini\character-alien.glb" : character$(5)="mini\character-soldier.glb"character$(6)="blocky\character-a.glb" : character$(7)="blocky\character-e.glb"character$(8)="blocky\character-h.glb"Dim template(8)For i=0 To 8 template(i)=LoadAnimMesh(character$(i)) ; the two packs are made at different sizes: make every character the same height size#=1.0/MeshHeight(template(i)) ScaleEntity template(i),size,size,size HideEntity template(i)Next; the player: a pivot that moves, carrying the character modelplayer=CreatePivot()Global model=0, current=0ChangeCharacter(player,0); some friends wandering aboutFor i=1 To 4 w.Wanderer=New Wanderer w\entity=CopyEntity(template(Rand(1,8))) PositionEntity w\entity,Rnd(-5,5),0,Rnd(-5,5) TurnEntity w\entity,0,Rnd(360),0 w\speed=Rnd(.015,.025) : w\turn=Rnd(-1,1) Animate w\entity,1,.5,FindAnimSeq(w\entity,"walk"),5Next; ---- play ----Global action$="", moving$="idle"yvel#=0 : y#=0camX#=0 : camY#=3 : camZ#=-6While Not KeyHit(1) busy=(action$<>"" And Animating(model)) If Not busy Then action$="" ; move and turn If KeyDown(203) Then TurnEntity player,0,3,0 If KeyDown(205) Then TurnEntity player,0,-3,0 sprint=KeyDown(42) Or KeyDown(54) speed#=0 If KeyDown(200) Then speed=.045 : If sprint Then speed=.09 If KeyDown(208) Then speed=-.03 If busy And action$<>"jump" Then speed=0 MoveEntity player,0,0,speed px#=EntityX(player) : pz#=EntityZ(player) If px<-ARENA Then px=-ARENA If px>ARENA Then px=ARENA If pz<-ARENA Then pz=-ARENA If pz>ARENA Then pz=ARENA ; jumping If KeyHit(57) And y=0 Then yvel=.12 : Act("jump") yvel=yvel-.008 : y=y+yvel If y<0 Then y=0 : yvel=0 PositionEntity player,px,y,pz ; actions If KeyHit(2) Then Act("attack-melee-right") If KeyHit(3) Then Act("attack-kick-right") If KeyHit(4) Then Act("emote-yes") If KeyHit(5) Then Act("emote-no") If KeyHit(46) Then ChangeCharacter(player,(current+1) Mod 9) ; idle / walk / sprint when not doing anything else If action$="" want$="idle" If speed<>0 Then want$="walk" If speed>.05 Then want$="sprint" If want$<>moving$ Or Not Animating(model) moving$=want$ Play(moving$,1,.5,6) EndIf EndIf ; friends wander and turn back at the fence For w.Wanderer=Each Wanderer TurnEntity w\entity,0,w\turn,0 MoveEntity w\entity,0,0,w\speed If Abs(EntityX(w\entity))>ARENA-.5 Or Abs(EntityZ(w\entity))>ARENA-.5 TurnEntity w\entity,0,180,0 : MoveEntity w\entity,0,0,.1 EndIf If Rand(200)=1 Then w\turn=Rnd(-1.2,1.2) Next ; spinning coins For c.Coin=Each Coin TurnEntity c\entity,0,3,0 If EntityDistance(c\entity,player)<.6 FreeEntity c\entity : Delete c score=score+1 EndIf Next If First Coin=Null Then SpawnCoins(coinModel) ; the camera follows behind the player TFormPoint 0,1.8,-3.4,player,0 camX=camX+(TFormedX()-camX)*.06 camY=camY+(TFormedY()-camY)*.06 camZ=camZ+(TFormedZ()-camZ)*.06 PositionEntity camera,camX,camY,camZ PointEntity camera,player TurnEntity camera,-12,0,0 UpdateWorld RenderWorld Color 255,255,255 Text 10,10,"CHARACTERS - by Kenney (www.kenney.nl, CC0)" Text 10,30,"Up/Down walk Left/Right turn Shift sprint Space jump" Text 10,45,"1 attack 2 kick 3 yes 4 no C change character" Text 10,65,"Coins: "+score+" Animation: "+AnimSeqName$(model,AnimSeq(model))+" ("+CountAnimSeqs(model)+" in this model)" FlipWendEnd; Plays a named animation on the player's character, if it has one.Function Play(name$,mode,speed#,transition#) seq=FindAnimSeq(model,name$) If seq>=0 Then Animate model,mode,speed,seq,transition Return seq>=0End Function; A one-off action: play it once, then go back to moving.Function Act(name$) If Play(name$,3,.6,4) Then action$=name$ : moving$=""End FunctionFunction ChangeCharacter(player,index) If model Then FreeEntity model current=index model=CopyEntity(template(index),player) action$="" : moving$=""End Function; Copies of a model at random places around the arena.Function Scatter(file$,count,clear#) m=LoadMesh(file$) For i=1 To count Repeat x#=Rnd(-ARENA,ARENA) : z#=Rnd(-ARENA,ARENA) Until Abs(x)>clear+1 Or Abs(z)>clear+1 c=CopyEntity(m) PositionEntity c,x,0,z TurnEntity c,0,Rnd(360),0 Next FreeEntity mEnd FunctionFunction SpawnCoins(coinModel) For i=1 To 10 c.Coin=New Coin c\entity=CopyEntity(coinModel) PositionEntity c\entity,Rnd(-ARENA+1,ARENA-1),.45,Rnd(-ARENA+1,ARENA-1) NextEnd Function
