In the previous phases we created a new model, populated it with facilities, bomber aircrafts, defined behavior of aircrafts and their mission, specified animation shapes, added 3D view, camera, and protected area.
In this phase we will add bombs to our model and learn how to destroy facilities with them. To do that we must create interaction between the bomber and the target building. We will use yet another agent type, Bomb. On approaching the attack distance, the bomber will drop a bomb onto the targeted building, which will change its state to Destroyed once the bomb reaches the building.
Although aircrafts carry bombs, the Bomb agents will be located not inside the Bomber agent in the model, but directly inside the Main agent, i.e. at the same level as the bombers and the buildings populations. It will be easier to place bombs in the same space with this model architecture.
Let us start with creating the Bomb agent type and program its interaction with the building:
Create Bomb agent
Now we need to adjust the scale of the animation shape to perfectly fit it into our environment.
Adjust scale of the animation shape
Now we will make a bomb and the targeted building interact with each other by means of a statechart. The bomb will send the "You are destroyed" message to the building on hitting it, destroying itself immediately afterwards.
Start creating the statechart in the Bomb diagram that you are currently in.
Create the interaction between the bomb and the building
setXYZ( bomber.getX(), bomber.getY(), bomber.getZ() );
Now click and drag the
element from the
section of the
palette up to the previously drawn
Statechart Entry Point
element on the
diagram. Release the mouse button when the green dot appears. It indicates that the two elements will be connected.
Name it Falling.
moveTo( target.getX(), target.getY(), target.getZ() );
element to the agent diagram and connect it to the
element. Type the following code into the
field of its
to destroy the current bomb once the message has been delivered:
main.remove_bombs( this );
We have completed the statechart and the only missing piece that is left is the bomber's decision to drop a bomb. We will do it exactly upon arrival to the point directly above the target building (in the action of the transition going from ToTarget and Away states).
Define bomb dropping
main.add_bombs(target,this);Note: On creating a new bomb, the bomber passes itself (this) and the target building to the bomb constructor parameters.
As you can see, the bombs are falling down unnaturally slowly, that is because the initial speed of the bombs is set to the default 10 meters per second.
Define the speed of the bombs
The next step is to show the result of a bomb hitting the building, i.e. to implement the building's reaction to the bomb explosion. The state of being hit will be transferred to the building by a message inside the bomb.
Transfer the message from the bomb to a building
Type deliver( "You are destroyed", target );
We are using the
deliver()method, which delivers the message instantly (within the same event) instead of the
method, which does it in a separate event (still at the model time though). This is because the message sent by the
method will be discarded once the sender agent ceases to exist.
Now we will add a Boolean flag destroyed and some animation to the building hit by the bomb.Modify the Building agent
destroyed = true;We are not analyzing the content of the message because a bomb is the only message source for our building for now.
In the end the properties should be configured as shown on the image below.
Move on to the Position section and set the Z parameter to 70.
Run the model to observe the building catching on fire when hit.
You must have noticed that, although all buildings are destroyed by the very first bomber attack, the new bombers are, nevertheless, sent to the target area. This happens because the mission assignment is done regardless of the state of the target assets. We will fix it now.
Modify mission assignment
Run the model. As you can see, the bombers are sent to bomb the still intact facilities.
We have completed the third phase of the Air defense system tutorial.