#Stencyl:
Zerg terrain infection
Part
2: Checking if it's ok to place building and dealing with effects of
placing new building
Topics:
- Algorithm for placing buildings
- Adding new matrix
- Checking if it's ok to place building
- Effects of placing building
Behaviors
used in part II
Behavior:
- main scene behavior (scene behavior)
- when created
- trigger event <building_check>
- when mouse pressed
- building behavior (actor behavior attached to buildings actors)
- trigger event <placed_on_map>
Main
variables:
- main scene behavior (scene behavior)
- building_check - Boolean; true if it's ok to place building
- building_list - Matrix; contains as records x,y, x+width, y+ height of buildings actors already placed on map
- building behavior
- matrix_index - Number; index of <building_list> matrix
- number_of_rows - Number; number of rows in <building_list> matrix
Algorithm
for placing buildings
- In <building_list> matrix we store as records coordinates of buildings already placed on map
- We have already picked the building we want to place and we are trying to place it
- We go through each record of <building_list> matrix comparing position of picked building with data from record to see if picked building is trying to place itself on place occupied by another building already on map.
- If there is free place we place building on map and add its coordinates to the matrix as new record. Otherwise we play beep sound and mark that tasting for placing building was failed.
Adding
new matrix
->
when created
In
<when created> we add <building_list> matrix.
Checking
if it's ok to place building
->
trigger event <building_check>
We
assume at first that's it's going to be ok and set <building_check>
to <true>. Next thing we want to do is to see if building we
are about to place won't interfere with buildings already on map. To
do this we will go through each record of <building_list>
matrix.
Firstly
we need to know how many records are there in the matrix. We learn it
by triggering <get_data>. This gives us information about
number of rows and index of <building_list> matrix.
Then
we go record by record searching for something that would tell us
it's not ok to place building. If we find something like this we play
beep sound and set <building_check> to <false>.
Helper_list_1:
- #0 - x of building actor already on map
- #1 - y of building actor already on map
- #2 - x + width of building actor already on map
- #3 - y + height of building actor already on map
We
doing this with 2 checks: <First check> and <Second check>.
4
picture represent that all it takes for placing building to be wrong
is if one of the building points is inside of the area of building
already on map. This is checked by <First check>.
(Picture has been zoomed) The smaller building (red) is already on
map. The bigger one (green) is about to be placed. Placing bigger
building like this illegal because it would squash the smaller one.
This Picture represents why <First check> is not enough.
<First check> checks if one of the
points of building being placed is between points of already placed
buildings. <Second check> checks if one of the points of
already placed building is between points of building being placed.
Effects
of placing building
->
when
mouse pressed
->
trigger event <placed_on_map>
In
Part I of the tutorial we placed the building. Now we need to add
record with coordinates of the placed building so that in future we
may check if building we are about to place doesn't overlap building
already on map.
After
creating building in <when mouse pressed> we trigger event
<placed_on_map> for placed building in behaior <building
behavior >.
To
choose other part follow the link: