Virtual Life App with World Item

 

 

Copy your previous project for the starting point

 

Feature Specification

 

 

Code Design

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

§  act(world) - Will complete the action for a single world item during its turn -- i.e. die, reproduce, move.  Takes the world object by reference to use for services the world provides, like copy, move, remove, etc.

 

 

 

 

 
§    new worldItem()

 

 

 

o    To get you started, here is an example of how this factory will work:

 

 

function worldItemFactory() { }

 

worldItemFactory.build = function(symbol,latitude,longitude) {

   

    var type;

    var ableToMoveOn;

 

    switch (symbol) {

           

        case "#":

            type = "wall";

            break;

 

        case "p":

            type = "plant";

            ableToMoveOn = ["empty"];

            break;

 

        case "a":

            type = "animal";

            ableToMoveOn = ["empty", "plant"];

            break;

 

        case " ":

            type = "empty";

            break;

    }

 

    return new WorldItem(symbol,latitude,longitude,type,ableToMoveOn);

}

 

·         Create links to your JavaScript file(s):

 

<body>
<script src="http://brickhousecodecamp.org/educationMaterials/workbenchProjects/phase-i/virtual-life-01-app/animate-world.js"></script>
 
<script src="world-item.js"></script>
<script src="world-item-factory.js"></script>
<script src="world.js"></script>
 
<script>
          var worldPlan =
              "######" + "\n" +
              "# p# #" + "\n" +
              "# # a#" + "\n" +
              "# pa #" + "\n" +
              "# #p #" + "\n" +
              "######";
 
          var world = new World(worldPlan);
 
          animateWorld(world)
</script>
 
</body>
 
 

Notes

 

·         If you are working in JSBin, you can put all the JavaScript (everything referenced in <script> tags above) and paste the content into your JavaScript panel.  Ask a tutor if you need help with this.  Our hope is that you will do most of your development in Visual Studio Code, then use the Live Server extension to launch this and access with Chrome.  Use the Chrome DevTools (F12) to debug your code.  Then when you have something totally tested and working, port your work into a bin on JSBin to submit the assignment.

 

·         How to define a prototyped object:

 

o    Make a constructor (use upper camel case)

               
function WorldItem(symbol,latitude,longitude,type,ableToMoveOn) { 
 
   this.type = type;
   this.symbol = symbol;
   this.latitude = latitude;
   this.longitude = longitude;
   this.ableToMoveOn = ableToMoveOn;    
 
}
 

o    Add functions:

               
WorldItem.prototype.act = function(world) {
 
    let alive = true;
    let isPlant = this.type == "plant";
    let isAnimal = this.type == "animal";
 
    if (isPlant || isAnimal) {
 
        alive = Math.random() < 0.73 ? true : false;
 
        if (alive) {
 
               …
        }
}
 

o    Create an instance of the object:

 

      var worldItem = new WorldItem(data);
               worldItem.act();
 

References