game-world
Game worlds
Next:game-attributes


Vocabulary
game.worlds

Class description
A subclass of world that automatically sets up and manages connections to the game.loop, game.input, and audio.engine libraries. It does this by providing methods on begin-world, end-world, and draw*. Subclasses can provide their own world setup, teardown, and update code by adding methods to the begin-game-world and end-game-world generic words. The standard world generics draw-world* and resize-world can also be given methods to draw the window contents and handle resize events. The draw-world* method will be invoked in a tight loop by the game loop.

The game-world tuple has the following publicly accessible slots:
game-loop contains the game-loop instance managed by the game world. If the world is inactive, this slot will contain f.
audio-engine contains the audio-engine instance managed by the game world. If the world is inactive, or the use-audio-engine? slot of the game-attributes object used to initialize the world was false, this slot will contain f.


See also
begin-game-world, end-game-world, tick-game-world

Definition
USING: kernel math ui.gadgets.worlds ;

IN: game.worlds

TUPLE: game-world < world
game-loop audio-engine
{ tick-interval-nanos integer initial: 0 }
{ use-game-input? boolean initial: f }
{ use-audio-engine? boolean initial: f }
audio-engine-device
{ audio-engine-voice-count initial: 16 }
{ tick-slice float initial: 0.0 } ;


Methods
USING: accessors combinators game.worlds generic
ui.gadgets.worlds ;

M: game-world apply-world-attributes
{
[ tick-interval-nanos>> >>tick-interval-nanos ]
[ use-game-input?>> >>use-game-input? ]
[ use-audio-engine?>> >>use-audio-engine? ]
[ audio-engine-device>> >>audio-engine-device ]
[
audio-engine-voice-count>>
>>audio-engine-voice-count
]
[
M\ game-world apply-world-attributes
(call-next-method)
]
} cleave ;


USING: accessors game.input game.loop game.worlds
game.worlds.private kernel ui.gadgets.worlds ;

M: game-world begin-world
dup use-game-input?>> [ open-game-input ] when
dup use-audio-engine?>>
[ dup open-game-audio-engine >>audio-engine ] when dup
[ tick-interval-nanos>> ] [ ] bi <game-loop>
[ >>game-loop begin-game-world ] keep start-loop ;


USING: accessors game.loop game.worlds kernel threads
ui.gadgets ;

M: game-world draw* swap >>tick-slice relayout-1 yield ;


USING: accessors destructors game.input game.loop game.worlds
kernel ui.gadgets.worlds ;

M: game-world end-world
dup game-loop>> [ stop-loop ] when*
[ end-game-world ] [ audio-engine>> [ dispose ] when* ]
[ use-game-input?>> [ close-game-input ] when ] tri ;


USING: accessors audio.engine game.loop game.worlds kernel ;

M: game-world tick*
[ tick-game-world ]
[ audio-engine>> [ update-audio ] when* ] bi ;