ArcBasicDocs

samples/JamesH/HexIsland

hex_island.abs

167 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.

Download hex_island.absAll files in this folder

hex_island.abs
; Hex Island - a world built from hexagon tiles;; A random island is made from Kenney's hexagon tile kit (CC0, www.kenney.nl):; water, beaches, grass, forests, hills and mountains, with villages and; ships. It also shows off the ArcBasic lighting extensions.;; Keys:  Left/Right  orbit     Up/Down  tilt     +/-  zoom;        R  new island         S  shadows   A  ambient occlusion   G  glow;        Escape  quitGraphics3D 1024,768,0,2SetBuffer BackBuffer()Const RADIUS=9Const HEX_W#=1.0, ROW_D#=0.866       ; tile width, and distance between rowsType Tile	Field entityEnd TypeType Ship	Field entity, angle#, radius#, speed#End Type; ---- camera, sky and lights ----pivot=CreatePivot()tilt=CreatePivot(pivot)camera=CreateCamera(tilt)CameraClsColor camera,120,180,235CameraRange camera,.1,200TurnEntity tilt,35,0,0zoom#=17PositionEntity camera,0,0,-zoomAmbientLight 110,115,130sun=CreateLight(1)RotateEntity sun,50,-35,0LightColor sun,255,240,215ShadowQuality 2shadows=True : ao=True : glow=FalseLightShadows sun,shadowsCameraAmbientOcclusion camera,ao,3,.6CameraGlow camera,glow; ---- tile templates (loaded once, copied for every tile) ----Global t_water=Template("water.glb"), t_water_rocks=Template("water-rocks.glb")Global t_sand=Template("sand.glb"), t_sand_rocks=Template("sand-rocks.glb")Global t_grass=Template("grass.glb"), t_forest=Template("grass-forest.glb"), t_hill=Template("grass-hill.glb")Global t_stone=Template("stone.glb"), t_stone_hill=Template("stone-hill.glb"), t_mountain=Template("stone-mountain.glb")Global t_rocks=Template("stone-rocks.glb")Dim building(8)building(0)=Template("building-house.glb") : building(1)=Template("building-farm.glb")building(2)=Template("building-mill.glb") : building(3)=Template("building-tower.glb")building(4)=Template("building-castle.glb") : building(5)=Template("building-village.glb")building(6)=Template("building-sheep.glb") : building(7)=Template("building-market.glb")building(8)=Template("building-wizard-tower.glb")Global t_ship=Template("unit-ship.glb"), t_ship_large=Template("unit-ship-large.glb")seed=MilliSecs()MakeIsland(seed)While Not KeyHit(1)	; controls	If KeyDown(203) Then TurnEntity pivot,0,1,0	If KeyDown(205) Then TurnEntity pivot,0,-1,0	If KeyDown(200) And EntityPitch(tilt)<80 Then TurnEntity tilt,1,0,0	If KeyDown(208) And EntityPitch(tilt)>10 Then TurnEntity tilt,-1,0,0	If KeyDown(13) Or KeyDown(78) Then zoom=zoom-.2	If KeyDown(12) Or KeyDown(74) Then zoom=zoom+.2	If zoom<5 Then zoom=5	If zoom>40 Then zoom=40	PositionEntity camera,0,0,-zoom	If KeyHit(19) Then seed=MilliSecs() : MakeIsland(seed)	If KeyHit(31) Then shadows=Not shadows : LightShadows sun,shadows	If KeyHit(30) Then ao=Not ao : CameraAmbientOcclusion camera,ao,3,.6	If KeyHit(34) Then glow=Not glow : CameraGlow camera,glow,1,.9	; ships sail around the island	For s.Ship=Each Ship		s\angle=s\angle+s\speed		PositionEntity s\entity,Sin(s\angle)*s\radius,.05+Sin(MilliSecs()*.2+s\radius*40)*.02,Cos(s\angle)*s\radius		RotateEntity s\entity,0,-s\angle-90*Sgn(s\speed),Sin(MilliSecs()*.15+s\radius*30)*3	Next	RenderWorld	Color 255,255,255	Text 10,10,"HEX ISLAND - tiles by Kenney (www.kenney.nl, CC0)"	Text 10,30,"Left/Right orbit   Up/Down tilt   +/- zoom   R new island"	Text 10,45,"S shadows: "+OnOff$(shadows)+"   A ambient occlusion: "+OnOff$(ao)+"   G glow: "+OnOff$(glow)	FlipWendEnd; Loads a tile model and hides it; the island is made of copies.Function Template(file$)	m=LoadMesh("hexagon\"+file$)	If m=0 Then RuntimeError "Missing tile: "+file$	HideEntity m	Return mEnd FunctionFunction OnOff$(v)	If v Then Return "on"	Return "off"End FunctionFunction PlaceTile(template,x#,z#,turn)	t.Tile=New Tile	t\entity=CopyEntity(template)	PositionEntity t\entity,x,0,z	TurnEntity t\entity,0,turn*60,0	Return t\entityEnd Function; Hexagon distance from the centre, for axial coordinates q,rFunction HexDistance(q,r)	Return (Abs(q)+Abs(r)+Abs(q+r))/2End FunctionFunction MakeIsland(seed)	For t.Tile=Each Tile : FreeEntity t\entity : Delete t : Next	For s.Ship=Each Ship : FreeEntity s\entity : Delete s : Next	SeedRnd seed	; a few random bumps make the island an interesting shape	wob1#=Rnd(360) : wob2#=Rnd(360)	For q=-RADIUS To RADIUS		For r=-RADIUS To RADIUS			If HexDistance(q,r)<=RADIUS				x#=(q+r/2.0)*HEX_W				z#=r*ROW_D				a#=ATan2(z,x)				d#=HexDistance(q,r)/Float(RADIUS)				d=d+Sin(a*3+wob1)*.12+Sin(a*5+wob2)*.07+Rnd(-.06,.06)				turn=Rand(0,5)				If d>.86					If Rnd(1)<.05 Then PlaceTile(t_water_rocks,x,z,turn) Else PlaceTile(t_water,x,z,turn)				ElseIf d>.74					If Rnd(1)<.12 Then PlaceTile(t_sand_rocks,x,z,turn) Else PlaceTile(t_sand,x,z,turn)				ElseIf d>.25					c#=Rnd(1)					If c<.12						PlaceTile(building(Rand(0,8)),x,z,turn)					ElseIf c<.38						PlaceTile(t_forest,x,z,turn)					ElseIf c<.48						PlaceTile(t_hill,x,z,turn)					Else						PlaceTile(t_grass,x,z,turn)					EndIf				ElseIf d>.12					c#=Rnd(1)					If c<.4 Then PlaceTile(t_stone_hill,x,z,turn) Else If c<.6 Then PlaceTile(t_rocks,x,z,turn) Else PlaceTile(t_stone,x,z,turn)				Else					PlaceTile(t_mountain,x,z,turn)				EndIf			EndIf		Next	Next	; ships	For i=1 To 3		s.Ship=New Ship		If i=1 Then s\entity=CopyEntity(t_ship_large) Else s\entity=CopyEntity(t_ship)		s\angle=Rnd(360) : s\radius=RADIUS*.92+Rnd(-.3,.3)		s\speed=Rnd(.08,.15) : If i=2 Then s\speed=-s\speed	NextEnd Function