116 lines
2.7 KiB
Plaintext
116 lines
2.7 KiB
Plaintext
package io.sarl.demos.fireworks.agents
|
|
|
|
import io.sarl.core.Behaviors
|
|
import io.sarl.core.DefaultContextInteractions
|
|
import io.sarl.core.Initialize
|
|
import io.sarl.core.InnerContextAccess
|
|
import io.sarl.core.Lifecycle
|
|
import io.sarl.core.Logging
|
|
import io.sarl.core.MemberJoined
|
|
import io.sarl.demos.fireworks.Positions
|
|
import io.sarl.demos.fireworks.events.CreateArea
|
|
import io.sarl.demos.fireworks.events.Exit
|
|
import io.sarl.demos.fireworks.events.Freeze
|
|
import io.sarl.demos.fireworks.events.Launch
|
|
import io.sarl.demos.fireworks.events.SetupSettings
|
|
import io.sarl.demos.fireworks.gui.FXMLViewerController
|
|
import io.sarl.util.OpenEventSpaceSpecification
|
|
import java.util.Random
|
|
import java.util.UUID
|
|
|
|
/*
|
|
* The main agent able to communicate with the GUI
|
|
*/
|
|
agent LaunchingArea {
|
|
uses DefaultContextInteractions, Lifecycle, Behaviors, Logging, InnerContextAccess
|
|
|
|
var rocketsQuantity : Integer
|
|
var fireQuantity : Integer
|
|
var gravity : Double
|
|
var grid : Positions = new Positions
|
|
var maxWidth = 10.0
|
|
var exited = false
|
|
|
|
/*
|
|
* Configure according to GUI settings
|
|
*/
|
|
on SetupSettings {
|
|
this.rocketsQuantity = occurrence.rocketsQuantity
|
|
this.fireQuantity = occurrence.fireQuatity
|
|
this.gravity = occurrence.gravity
|
|
this.maxWidth = occurrence.maxWidth
|
|
}
|
|
|
|
/*
|
|
* Kill itself on Exit signal after all inner agent are killed
|
|
*/
|
|
on Exit [!hasMemberAgent] {
|
|
killMe
|
|
}
|
|
|
|
/*
|
|
* Transmit Exit signal to all inner agent
|
|
*/
|
|
on Exit [hasMemberAgent && !exited] {
|
|
exited = true
|
|
innerContext.defaultSpace.emit(new Exit)
|
|
}
|
|
|
|
/*
|
|
* Initialization step without GUI
|
|
*/
|
|
on Initialize [occurrence.parameters.empty] {
|
|
rocketsQuantity = 20
|
|
fireQuantity = 30
|
|
gravity = 0.5
|
|
|
|
info("Setup Area")
|
|
|
|
}
|
|
|
|
/*
|
|
* Initialization step with GUI
|
|
*/
|
|
on Initialize [!occurrence.parameters.empty] {
|
|
var ctrl = occurrence.parameters.get(0) as FXMLViewerController
|
|
var ispace = defaultContext.createSpace(OpenEventSpaceSpecification, UUID.randomUUID)
|
|
ctrl.setGUISpace(ispace)
|
|
ispace.register(asEventListener)
|
|
|
|
ctrl.listenAndDraw(grid)
|
|
|
|
info("Finishing initialization of Launching Area")
|
|
|
|
}
|
|
|
|
/*
|
|
* Create and configure every RocketLauncher
|
|
*/
|
|
on CreateArea {
|
|
var x : Double
|
|
var i = 0
|
|
var rnd = new Random()
|
|
while (i < rocketsQuantity) {
|
|
x = rnd.nextDouble() * maxWidth
|
|
spawnInContext(RocketLauncher, getInnerContext, x, 0.0, fireQuantity, gravity, grid)
|
|
i++;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Transmit Freeze signal
|
|
*/
|
|
on Freeze [!isFromMe(occurrence)] {
|
|
innerContext.defaultSpace.emit(occurrence)
|
|
}
|
|
|
|
/*
|
|
* Start all RocketLauncher when everything is ready
|
|
*/
|
|
on MemberJoined [occurrence.inInnerDefaultSpace && memberAgentCount == rocketsQuantity] {
|
|
info("Area is Ready: All rockets are here")
|
|
innerContext.defaultSpace.emit(new Launch)
|
|
}
|
|
|
|
}
|