|
count - how many people the crowd has, 0 or more x#, y#, z# - the middle of the crowd's circle, in the world radius# - the circle's radius, in metres model (optional) - the entity its people are copies of, such as a character with an animation set; 0 to add models later with CrowdModel (default) |
|
Creates a crowd: an entity owning people who walk about a circle on their own. A market square full of shoppers, spectators round a stage, a village going about its day: a crowd is the quick way to fill a place with people. CreateCrowd makes a pivot at x#,y#,z# and count copies of the model, placed at random in the circle, on the ground. Each person is a NavWalker (see WalkEntity) that wanders about the circle, keeping to it, with the model's idle, walk and run animations if it has an animation set. UpdateWorld moves them, as it moves every walker. The circle reaches 10 m up and down from y#, or its radius if that is more, so the people stand on hills and in hollows inside it. They are placed on a terrain's ground where there is one, and otherwise on the world's walkable map. To keep them to the streets of a village instead, give the crowd a painted region with WalkRegion; CrowdActivity sends them along paths, between places, to stand facing something, or to gather round it. Copies of one model don't all look the same: each is up to 5% taller or shorter, starts its animations at its own place, and has one of four tints, so a crowd never marches in step. Only four tints are used because copies of different colours can't share an instanced draw. Every person has random numbers of its own, so the same crowd moves the same way every time the program runs. The people are owned by the crowd (EntityOwner, CountOwnedEntities and GetOwnedEntity find them), not its children, so they walk anywhere, and FreeEntity on the crowd frees them all. CrowdSize changes how many there are. Hide the model as you would for CopyEntity: the crowd copies it, and shows the copies. Far from the camera, people cost very little: see WalkDetail. For thousands, CrowdEntityLimit gives entities only to the nearest, and the rest walk on without. A count below 0 or a radius of 0 or less is a runtime error. If the circle has no ground in it, the crowd places nobody, and tries again each second. See also: CrowdModel, CrowdActivity, CrowdSize, CrowdEntityLimit, WalkRegion, WalkDetail. |
; CreateCrowd Example
; -------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
camera=CreateCamera()
PositionEntity camera,0,14,-24
RotateEntity camera,30,0,0
light=CreateLight()
RotateEntity light,45,45,0
; A 60 x 60 m square, one unit to a metre: the floor's top is at y=0
floor=CreateCube()
ScaleEntity floor,30,0.25,30
PositionEntity floor,0,-0.25,0
EntityColor floor,150,130,100
; The robot from the animation-sets sample, at half size so that it is
; about as big as a person, hidden: the crowd shows its copies
document=LoadResourceDocument("../../../samples/animation-sets/robot.animset")
If document=0 Then RuntimeError ResourceDocumentError()
library=LoadResourceDocumentAsync(document)
FreeResourceDocument document
While Not WorldReady(library)
If WorldLoadState(library)=WORLD_LOAD_FAILED Then RuntimeError WorldLoadError(library)
UpdateAsyncLoads 2
Wend
robot=CreateAnimationSet(FindWorldAsset(library,"resource"))
FreeWorld library
ScaleEntity robot,0.5,0.5,0.5
HideEntity robot
; Thirty robots who wander about a circle 12 m across the middle
radius#=12
crowd=CreateCrowd(30,0,0,0,radius,robot)
While Not KeyDown(1)
; Space makes the crowd afresh: new people in new places
If KeyHit(57) Then
FreeEntity crowd
crowd=CreateCrowd(30,0,0,0,radius,robot)
EndIf
; Arrow keys move the camera
If KeyDown(200) Then MoveEntity camera,0,0,0.1
If KeyDown(208) Then MoveEntity camera,0,0,-0.1
If KeyDown(203) Then TurnEntity camera,0,1,0,True
If KeyDown(205) Then TurnEntity camera,0,-1,0,True
; A ring on the floor shows the crowd's circle
For i=0 To 47
DebugLine radius*Sin(i*7.5),0.05,radius*Cos(i*7.5),radius*Sin((i+1)*7.5),0.05,radius*Cos((i+1)*7.5),255,220,0
Next
; UpdateWorld walks the crowd's people
UpdateWorld
RenderWorld
Text 0,0,"Arrow keys: move camera Space: a new crowd Esc: exit"
Text 0,20,"crowd=CreateCrowd(30,0,0,0,"+Int(radius)+",robot)"
Text 0,40,CountOwnedEntities(crowd)+" people"
Flip
Wend
End