#Stencyl:
Zerg terrain infection
Part 1: Creating building, making building follow mouse, placing building
Topics:
- Choosing building on keyboard key
- Cancel chosen building
- Make building follow the mouse
- Placing building
Behaviors used in part I
Behavior:
- main scene behavior (scene behavior)
- when created
- when e pressed
- when r pressed
- on mouse-click
- when d pressed
Main
variables:
- building_active - Boolean; if there is a building following the mouse
- tile_size - Number; size of tiles in pixels
- active_building - Actor; Stores picked building
For tiles and building actors we change
Physics->General->What kind of Actor Type? to <cannot move>
and Physics->Advanced -> Actor Mode to <Simple>.
In
when create we declare tile size(<tile_size>) in pixels. We
declare <building_active> to false. <building_active>
says if we picked building. We create <building_list> matrix
that will be used in checking if there is a place for placing picked
building.
Choosing
building on keyboard key
->
<when e pressed>
We check if there is no building picked by checking (if
(not(<building_active>)
) ). If this is true we create building. We set <building_active>
to <true> since we picked building. We set <active_building>
to <last created actor>. We move <last created actor> so
that it has center on the mouse position.
I
assume that all created actors this way have width and height equal
to multiplied tiles. Implementation for second building is pretty
much the same expect it is <when
r pressed>
event.
Cancel
chosen building
->
<when d pressed>
If
player has made a mistake and picked the wrong type of building we
allow them to cancel their choice. We
check if there exists picked actor and if it's true we kill it and
mark this fact by setting <building_active>
to <false>.
Make
building follow the mouse
->
<on update event>
First
If <building_active>
checks if building exists. If yes we set building so it's center is
at the mouse position.
Then
we check 4 ifs in order to prevent building from leaving the screen.
If x < 0 and if y < 0 are obvious the other pair might seem
strange(explanation on picture below).
The
second pair of <set x> and <set y> snaps the position of
building to the grid.
Picture
shows the building in position to most right and it is last position
acceptable in that direction(yellow). If the building was anymore to
the right it should be limited to that position since otherwise some part of actor would leave screen.
Snapping
to grid means that we should change the current position of
actor(purple) to node of the grid(black).
Code
analysis for x direction. In this project we set <tile_size> to 16.
For x = 8; x/16 = 0.5.
Since we apply floor -> floor (0.5) = 0. This means that purple
point will be moved to the tile number 0 counting from the left. In
order to update position of actor we need to return to pixel
values so we multiply it number of tile by <tile_size> and we
get x-position = 0 (value in pixels).
The
floor function is nice because you know that coordinates of nodes to the right and bottom from checked node are
limits of how far a purple point can get from the checked node point while still being snapped to it. It's
much easier than using round function.
Placing
building
-><when
mouse was pressed>
First
If <building_active> checks if there is a building picked. If
yes we ask if there are no other building on space where we are about
to place building by triggering <building_check> (more details
in Part II). If that's ok we place building. Since the building is
placed and we don't want it to follow mouse anymore we set
<building_active>
to <false>. Then we trigger <placed_on_map> to force
building to do all the work associated with its arrival (Part II).
To choose other part
follow the link:
Brak komentarzy:
Prześlij komentarz