marchagotchi

Once upon a time I halfheartedly participated in the march-agotchi game jam. This is the core game play logic of the little tamogotchi clone I wrote. Note that it is dependent on dice.js, found above.

'use strict';

console.log('Marchagotchi!');

// set starting values
var petStats = {
  hunger: 25,
  hungerDeath: 200,   // dead if > 200
  happiness: 100,
  happinessDeath: 0,  // dead if < 0
  sickness: 0,    
  sicknessDeath: 200, // dead if > 200
  sleepiness: 0, 
  sleepinessDeath: 100 // dead if > 100
};


// check for death
const isPetDead = (petStats) => {
  let petIsAlive = true;
  if (petStats.hunger > petStats.hungerDeath || petStats.happiness < petStats.happinessDeath || petStats.sickness > petStats.sicknessDeath || petStats.sleepiness > petStats.sleepinessDeath) {
    petIsAlive = false;
    console.error('RIP, PET IS DEAD');
  };
  return petIsAlive;
};


// check for contentedness
const isPetContent = (petStats) => {
  if (petStats.hunger <= 15 && petStats.happiness >= 76 && petStats.sickness <= 15 && petStats.sleepiness <=15) {
    console.log('PET IS CONTENT');
    petStats.hungerDeath = petStats.hungerDeath + 10;
    petStats.happiness = petStats.happiness + 10; // FIXME: is this the right thing to do!?
    petStats.sicknessDeath = petStats.sicknessDeath + 10;
    petStats.sleepinessDeath = petStats.sleepinessDeath + 10;
  }
  return petStats;
};


// functions to alter values
const setHunger = (hunger, hungerDelta) => {
  const updatedHunger = hunger + hungerDelta;
  return petStats.hunger = updatedHunger;
};

const setHappiness = (happiness, happinessDelta) => {
  const updatedHappiness = happiness + happinessDelta;
  return petStats.happiness = updatedHappiness;
};

const setSickness = (sickness, sicknessDelta) => {
  const updatedSickness = sickness + sicknessDelta;
  return petStats.sickness = updatedSickness;
};

const setSleepiness = (sleepiness, sleepinessDelta) => {
  const updatedSleepiness = sleepiness + sleepinessDelta;
  return petStats.sleepiness = updatedSleepiness;
};


// player actions
const feedPet = (mealType) => {
  switch (mealType) {
    case 'full':
      setHunger(petStats.hunger, -25);
      setSleepiness(petStats.sleepiness, 10);
      break;
    case 'snack':
      setHunger(petStats.hunger, -5);
      setHappiness(petStats.happiness, 5)
      setSickness(petStats.sickness, 10);
      break;
    default:
      console.error('TERRIBLE ERROR! Incorrect mealType, ' + mealType);
  };
  isPetContent(petStats);
  isPetDead(petStats);
};

const playWithPet = () => {
  setHappiness(petStats.happiness, 10);
  setSleepiness(petStats.sleepiness, 20);
  isPetContent(petStats);
  isPetDead(petStats);
};

const cleanUpAfterPet = () => {
  setSickness(petStats.sickness, -25);
  setHappiness(petStats.happiness, -5);
  isPetContent(petStats);
  isPetDead(petStats);
};

const singLullabyToPet = () => {
  setSleepiness(petStats.sleepiness, -10);
  setHappiness(petStats.happiness, -5);
  isPetContent(petStats);
  isPetDead(petStats);
};

const displayLastAction = (emoji) => {
  document.getElementById('lastAction').textContent = emoji;
};


// game play loop
var timerId;
var bagOfNumbers;
const  gamePlayLoop = () => {

  var currentPetState = petStats;
  timerId = setTimeout(gamePlayLoop, 250); // 1000 = 1 second

  // generate some pseudo-random numbers
  bagOfNumbers = {
    whichStat: dice.roll('1d3'),
    delta: dice.roll('2d6')
  };


  // use these random numbers to add some spice to life
  switch(bagOfNumbers.whichStat) {
    case 1:
      // add hunger
      setHunger(currentPetState.hunger, bagOfNumbers.delta);
      break;
    case 2:
      // add happiness
      setHappiness(currentPetState.happiness, bagOfNumbers.delta);
      break;
    case 3: 
      // add sickness
      setSickness(currentPetState.sickness, bagOfNumbers.delta);
      break;
    default:
      console.error('THIS IS NOT HOW MATH IS SUPPOSED TO WORK!?');
  };

  // DO GAME PLAY
  (()=> {
    console.log(currentPetState);
    isPetContent(currentPetState);
    let petIsAlive = isPetDead(currentPetState);


    document.getElementById('hunger').textContent = 'Hunger: ' + petStats.hunger + '/' + petStats.hungerDeath;
    document.getElementById('happiness').textContent = 'Happiness: ' + petStats.happiness + '/' + petStats.happinessDeath;
    document.getElementById('sleepiness').textContent = 'Sleepiness: ' + petStats.sleepiness + '/' + petStats.sleepinessDeath;


    if (petIsAlive === false) {
      document.getElementById('pet').textContent = 'PET IS DEAD';
      document.getElementById('lastAction').textContent = '☠️';
      console.log('GAME OVER!');
      gameOver();
    };

  })();
};

const gameOver = () => {
  clearTimeout(timerId);
};

gamePlayLoop();