garden game

by STHayden

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://npmcdn.com/react@latest/dist/react-with-addons.js"></script>
<script src="https://npmcdn.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

JavaScript 1.7

function rand(min, max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
}

class GardenSpot {
	constructor(x, y, data) {
  	this.x = x;
    this.y = y;
    this.type = 'GardenSpot';
  }
  
  upkeep(resources) {
  	console.log('upkeep!')
    return resources;
  }
  
  toObject() {
  	var data = {};
    data.type = this.type;
    return data;
  }
}

class Plant extends GardenSpot {
	constructor(x, y, data) {
  	super(x, y, data);
    this.type = 'Plant';
  	var defaults = {
    	level: 1,
      exp: 0,
    };
  	Object.assign(this, defaults, data);
    
    this.levels = {
    	1: {
      	water: 1,
        sunshine: 1,
        tending: 1,
        encouragement: 1,
      },
      2: {
      	water: 2,
        sunshine: 2,
        tending: 2,
        encouragement: 2,
      }
    };
  }
  
  upkeep(resources) {
  	var success = true;
  	if (this.level) {
    	var currentUpkeep = this.levels[this.level];
      resources = resources.map(r => {
      	var cost = currentUpkeep[r.name];
      	if (cost && r.count >= cost) {
        	r.count -= cost;
        } else if (cost) {
        	success = false;
        }
        return r
      });
    }
    
    // only add exp is there is a next level
    if (success && this.levels[this.level + 1]) {
    	this.exp += 1;
    } else if (!success) {
    	this.exp -= 1;
    }
    
    this.expCheck();
    
  	return resources;
  }
  
  expCheck() {
  	if (this.exp >= 2 && this.levels[this.level + 1]) {
    	console.log('i can level up')
      this.level += 1;
      this.exp = 0;
    }
  }
  
  toObject() {
  	var data = super.toObject();
    data.level = this.level;
    data.exp = this.exp;
    return data;
  }
}

class Aloe extends Plant {
	constructor(x, y, data) {
  	super(x, y, data)
    this.type = 'Aloe';
  }
}

Aloe.cost = {
  water: 1,
  sunshine: 1,
  tending: 3,
  encouragement: 3,
}


class Garden {
	constructor(width, height, data) {
  	this.width = width;
    this.height = height;
    this.spots = [];
   ...