Prerequisite: clone this repository.
To begin, let's have a quick look at the code. In this new challenge, we will integrate an HTML interface to improve our game. So you will find an index.html file and a linked style.css file. At the bottom of the index.html, you will find the imports of the different js files. Please note that the order is important. In the index.html file, there is almost nothing. The html templates are prepared and injected directly, as the objects are implemented. You can browse them but you do not need to touch them in the process of the workshop. However, you will be ask to consume some of its features.
The HTML
Hero
zone is created directly from the index.js (line 18 to 20).
A new challenge awaits our champion, slaying the monstrous birds of Lake Stymphalus. Heracles (and his equipment) as well as three birds are already instantiated in index.js (Line 3 to 16).
Currently both birds and heroes are instances of the
Fighter
class. However, if they have common properties specific to fighters (name, life, dexterity, strength...) some characteristics differ. For example, onlyHero
will be able to wear equipment (Shield, Weapon...).
It should therefore not be possible to attribute a sword to a bird, but it is currently possible. To solve this design problem, you have to go through inheritance.
-
Created two classes
Hero
andMonster
, each inheriting fromFighter
. The properties common to both will remain in Fighter, those specific toHero
(Weapon
andShield
) will move toHero
. TheMonster
class currently only extendsFighter
without further modification. -
Moreover, the
getDamage()
andgetDefense()
methods have a different behavior between a monster and a hero. In the first case, only strength and dexterity are taken into account, in the case of the hero, the values returned also take into account the characteristics of weapons and armor. ThegetDamage()
andgetDefense()
methods must therefore exist inFighter
to reflect the simplest case, and be rewritten inHero
to take equipment into account (which normally corresponds to the current code ofFighter
at the end of the previous workshop). -
In index.js, modify the instantiations to take into account its new classes,
Hero
for Heracles andMonster
for the 3 birds. (Line 3, 14, 15, 16)
Another design change appears with this new event. Here, Heracles no longer fights against a single monster, but against a multitude. Currently, a Fighter doesn't have a way to know all the other Fighters in the fight. It would be possible to create an
adversaries
property inFighter
, but each Fighter would have to contain all the others, which would be quite redundant and not allow an easy overview.
A better solution is to create a new class
Arena
which would contain all theFighter
as well as methods to manipulate them (make them fight each other, move them around the arena, etc.). In addition, this arena can be used as a support for a map on which to place theFighter
and thus add positioning to the gameplay of our game.
-
Created an
Arena
class containing the propertiesmonsters
(an array ofMonster
objects) andhero
(aHero
object). For simplicity, we will assume that we necessarily have a single hero in an arena and one or more monsters. Also create aconstructor()
which will take a hero and an array of monsters as parameters. Also add asize
property (integer with default value 10) which will indicate the size of the arena. Then don't forget to add it at the bottom of the index.html file with the other POO files -
We want to position fighters in the arena: add the
x
andy
properties inFighter
and modifyMonster
andHero
. This will allow you to give a position to the fighters (Remember to add the values between 1 and 10 when instantiating them withnew
) -
In index.js, create an
Arena
type object by passing itheracles
and the threebirds
that you will have put in an array. -
Now add this code under your
Arena
instantiation
const ArenaHTML = new ArenaTemplate('arena'); ArenaHTML.createArena(<< My Arena instance >>);
- Update: you must see them on a map representing your arena and your fighters on it! Modify the coordinates of each one, they must move accordingly!
The next step will be to attack the birds: this will be the second part of this workshop;)