mirror of
https://github.com/klmp200/sarl-fireworks.git
synced 2024-11-22 14:53:19 +00:00
116 lines
2.7 KiB
Plaintext
116 lines
2.7 KiB
Plaintext
|
package io.sarl.demos.fireworks
|
||
|
|
||
|
import io.sarl.core.AgentTask
|
||
|
import io.sarl.core.Behaviors
|
||
|
import io.sarl.core.DefaultContextInteractions
|
||
|
import io.sarl.core.ExternalContextAccess
|
||
|
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.core.MemberLeft
|
||
|
import io.sarl.core.Schedules
|
||
|
import java.util.Random
|
||
|
import java.util.UUID
|
||
|
import java.util.Vector
|
||
|
|
||
|
agent Rocket {
|
||
|
|
||
|
uses Lifecycle, Logging, Schedules, Behaviors, DefaultContextInteractions, InnerContextAccess, ExternalContextAccess
|
||
|
var x : Double
|
||
|
var y : Double
|
||
|
var gravity : Double
|
||
|
var speedx : Double
|
||
|
var speedy : Double
|
||
|
var fireQuantity : Integer
|
||
|
var lifetime : Integer
|
||
|
var frozen = false
|
||
|
var exploded = false
|
||
|
var exited = false
|
||
|
var grid: Positions
|
||
|
var id: UUID
|
||
|
var move: AgentTask
|
||
|
|
||
|
on Exit [!hasMemberAgent]{
|
||
|
emit(new Exit)
|
||
|
killMe
|
||
|
}
|
||
|
|
||
|
on Exit [hasMemberAgent && !exited]{
|
||
|
exploded = true
|
||
|
frozen = true
|
||
|
exited = true
|
||
|
cancel(move, true)
|
||
|
|
||
|
innerContext.defaultSpace.emit(new Exit)
|
||
|
}
|
||
|
|
||
|
on Freeze {
|
||
|
this.frozen = occurrence.value
|
||
|
if (frozen)
|
||
|
cancel(move, true)
|
||
|
if (!frozen)
|
||
|
wake(new UpdateRocketPosition)
|
||
|
}
|
||
|
|
||
|
on Initialize {
|
||
|
info("New rocket launched")
|
||
|
var rnd = new Random()
|
||
|
if (occurrence.parameters.size.equals(7)){
|
||
|
x = occurrence.parameters.get(0) as Double
|
||
|
y = occurrence.parameters.get(1) as Double
|
||
|
speedx = occurrence.parameters.get(2) as Double
|
||
|
speedy = occurrence.parameters.get(3) as Double
|
||
|
gravity = occurrence.parameters.get(4) as Double
|
||
|
fireQuantity = occurrence.parameters.get(5) as Integer
|
||
|
grid = occurrence.parameters.get(6) as Positions
|
||
|
} else {
|
||
|
info("Error in Fireworks Initialize : bad parameters number")
|
||
|
}
|
||
|
lifetime = rnd.nextInt(5) * 300 + 300
|
||
|
id = UUID.randomUUID
|
||
|
|
||
|
move = atFixedDelay(30)[try{
|
||
|
wake(new UpdateRocketPosition);}
|
||
|
catch(e: Exception) {e.printStackTrace}]
|
||
|
}
|
||
|
|
||
|
on UpdateRocketPosition [isFromMe(occurrence) && !frozen && !exploded] {
|
||
|
var vect = new Vector(2)
|
||
|
x = x + speedx
|
||
|
y = y + speedy
|
||
|
vect.clear()
|
||
|
vect.add(x)
|
||
|
vect.add(y)
|
||
|
lifetime = lifetime - 10
|
||
|
if (grid!==null)
|
||
|
grid.setRocketPosition(id, vect)
|
||
|
if (lifetime <= 0){
|
||
|
exploded = true
|
||
|
cancel(move, true)
|
||
|
grid.hideHocketPosition(id)
|
||
|
wake(new Explode)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
on Explode {
|
||
|
for (var i=0; i < fireQuantity; i++){
|
||
|
spawnInContext(Fire, innerContext, x, y, grid, id)
|
||
|
}
|
||
|
// emit(new Launch)
|
||
|
}
|
||
|
|
||
|
on MemberLeft [!isFromMe(occurrence) && !frozen && !hasMemberAgent] {
|
||
|
exited = true
|
||
|
grid.removeRocketPosition(id)
|
||
|
// emit(new Launch)
|
||
|
killMe
|
||
|
}
|
||
|
|
||
|
on MemberJoined [!isFromMe(occurrence) && hasMemberAgent && memberAgentCount == fireQuantity]{
|
||
|
innerContext.defaultSpace.emit(new FireReady)
|
||
|
}
|
||
|
|
||
|
}
|