Part 5: Did you hear that? i think somethings moving out there...
The pieces are falling in to place and were shaping up to have a nice little game, our world is coming
together, and what fun is a world where we're the only ones in it? and why have a have a perfectly sppoky
spooky cave without perfectly spooky monsters? allright, allright, enough of the perfectly rhetorical questions.
Lets start placing some monesters! We'll be using our entity class to create our monsters, and while were at it
lets add a property to the entity class for a name:
Since we've added a property to the Entity initializer we need to open main.swift and add the new value
to where initialized our player:
Youll notice xcode reminding us that we need to change the init for npc, but were actually going to get rid
of that NPC, and the array holding entities we declared below at as well, since we'll be creating ACTUAL NPC's
in this installment instead of the previous place holder. the entity array will be moved to the GameMap class
since that is where we will be doing the monster spawning its easier to organize that way.
So once those two enties are deleted in main.swift, move over to the GameMap class and lets start spawning monsters
In our GameMap definition, add the following line to creat a new Array of type Entity for our monsters
We're also going to another property to our Tile class. We're going to add a boolean named "populated" so we
don't end up puttin two monsters on the same Tile.
We're also goin to add a propert to our Rect struct that we have been using to construct rooms
we'll call it numEnts, and as the name implies, use it to track the number of ents spawn in any goven room
thus giving us a modicum of control regarding their distribution, albeit randomly
so once the integer numEnts is added to the Rect struct, we are going to add the following function,
placeMonsters() in our GameMap class:
And to ensure that our rooms get become populated, we are going to add the following to the bottom of our
The way this code works is that every time a new room is created, theres a fifty percent chance that either
an orc, or a 50 percent chance that a goblin is going to be placed in the room, it then picks a random tile
in the room, and so long as that tile isnt already populated, and isnt in a wall, then the entity is placed there
and we appened the new entity to our monsters array.
Since we changed where the array holding all of our entities is, including our player, we have to make the following
changes to main.swift, placing player into the gameMap.monsters and altering our drawEnts and clearEnts functions
so they have the array passed to them:
Now if we compile and run our game, as we walk around we should encounter Ghosts and Orc's as symbolized by O's and G's
Unlike tiles however, they do NOT stay visible once they are out of our FOV.
So now that we've salted our dungeon with some bad dudes, lets lay the groundwork to be able to kick some monster butt
What if instead of just walking past a monster, they noticed our presence? It sounds like fancy AI, but its actually relativley
uncomplicated to implement, so to wrap up this section we'll do just that. open up the entity class, and in the move function
add the following:
Now, if we go to move in to a spot that is occupied by a monster we are thrown in to battle, which will be handled
by the battle class, which we will complete a little later. Notice that we added another bool to the entity class, inBattle
this is the variable we will use to switch game states from moving around, to battling. but for now, add another file to your project; battle.swift
and add the following:
I think ill stop here for now, in the mean time, think about how YOU would proceed furthur, and see how it compares
when i update the next section. Cheers!