--- tags: mstu5003, doodle --- # Data of My Life --- Group members: Yixiong Chen (yc2216) Dilys Han (dd3142) Yajuan Wu () The code can be found in: https://codepen.io/yc2215/pen/gOKbqOL ## Game description :::success This code simulates a hero who goes through a day's activities to gain experience by doing works and exercises. A simple model is used to gauge the progress of the day. The main indicator of the hero is his experience, which starts at 500 points in this particular day. The hero need to have energy to perform various activity and as activity goes on, the boredam level increase. The experience points gained is related to the hero's boredam level. ::: ## Code breakdown :::info The code is broken down by time of the day, such as morning, afternoon which is further broken down by early noon and late noon. Within each time segment, various functions are used to describe hero's activity, such as - consuming food, - exercise, - work, - meditation. From time to time, this code check the hero's - energy level and - boredam level. When the energy level is too low, hero will consume energy bar to replenish energy. When the boredam level is too high, hero will either take a break to meditate, or go straight to the end of the day to sleep for a long period of time. ::: ## Notes on variables :::info There are four arrays for four categories of activities. - exerciseItems and workItems are similar. Each of the elements in these two arrays are strings. - foodItems have many elements, each of which has a property as .drinkable, the value for this property is true of false. If it is true, when consumed, it will have a drinking animation, otherwise, it will have a chewing animation. - bathroomItems have several items, each of which has a special property as .play. When called, it will have specific animation associated with each item. Variables such as workoutTime, workTime, are hardcoded here. They can be changed later, can be function of other variables, such as energy level, boredam level, time of the day, etc. The function to compute if the hero get tired after morning is hardcoded here too. It is a simple function and it only checks how close the hero's energy level and boredam level, and with some random variable thrown in. It can be changed to more sophisicated function too. Time variable here are all in terms of hours if it is a duration of an activity or it is the 24 hour format if it is the time of the day. ::: ```:javascript= startOfDay(); let energyLvl = 100; // it will start fresh everyday as 100, could get up to 120 during the day, need to replenish when it hit 50 let boredemLvl = 0; // it starts fresh at zero every day, and will increase with activity. some experience gained will depend on the boredemLvl let experience = 500; // as life progress, experience should increase let isTired = false; // start the day fresh, no tired yet morning(); noon(); afternoon(); evening(); endOfDay(); let exerciseItems = ["Yoga", "WeightLifting", "Run"]; // all exercise item consume energy, some may reduce boredom, let workItems = ["Research", "Coding"]; // every work item consume energy, increase boredom, let foodItems = ["Energy Bar", "Milk", "Orange Juice", "Wine", "Oatmeal", "Cantaloupe", "Ham", "Cheese", "Baguette", "Green Tea", "Macarons"]; // every food item replenish energy, slightly reduce boredom, do nothing to experience let bathroomItems = ["Shower", "TeethMaking", "Makeup", "Makeup Remove"]; // every item in the bathroomItems has a .play method, which will animate the action. function startOfDay() { alarm(7); //alarm ring at 7 in the morning let morningBath = [bathroomItems[0], bathroomItems[1], bathroomItems[2]]; bathroomRun(morningBath); } function morning() { let breakfastMenu = [foodItems[1], foodItems[4], foodItems[5]]; consuming(breakfastMenu); //breakfast let workoutTime = 0.5; workout (exerciseItems[0], workoutTime); // morning Yoga let workTime = 1.0; work (workItems[1], time); // some coding energyReplenish(); // check energy level checkBoredem(); //check boredam level isTired = (energyLvl - boredemLvl) < Math.random()*5; // check if it is tired } function afternoon () { earlyNoon(); lateNoon(); } function earlyNoon(isTired) { // isTired is a boolean value, either true or false, if it is true, nap time will be 60 mins, other wise nap time is 15 mins. let lunchMenu = [foodItems[6],foodItems[7],foodItems[8]] consuming(lunchMenu); // lunch time if (isTired) { let napTime = 60; sleep(napTime); } else { let napTime = 15; meditation(napTime); } energyReplenish(); checkBoredem(); } function lateNoon() { let teaTimeMenu = [foodItems[9],foodItems[10]]; consuming(teaTimeMenu) // snack time workout (exerciseItems[2], 1); work (workItems[0], 2); energyReplenish(); checkBoredem(); } function night() { earlyEvening(); lateEvening(); } function earlyEvening() { let supperMenu = [foodItems[2],foodItems[3], foodItems[6],foodItems[7]]; consuming(supperMenu); //dinner time workout (exerciseItems[1], 0.5); energyReplenish(); checkBoredem(); } function lateEvening() { let eveningSnack = [foodItems[1],foodItems[4]]; consuming(eveningSnack); //late night snack workout (exerciseItems[2], 0.5); work (workItems[2], 2); energyReplenish(); } function endOfDay() { let eveningBath = [bathroomItems[0], bathroomItems[3]]; bathroomRun(eveningBath); let timeToSleep = 22; sleep(timeToSleep); } function work(item, time) { // this function spell out what each work item do to the energy, boredam, and experience switch (item) { case "Research": energyLvl -= time * 1; boredemLvl += time * 1.1; experience += time * 1 * (1 - boredemLvl/100); break; case "coding": energyLvl -= time * 1.5; boredemLvl += time * 3; experience += time * 1 * (1 - boredemLvl/100); break; default: undefined; } } function workout(item, time) { // this function spell out what each work item do to the energy, boredam, and experience switch (item) { case "Yoga": energyLvl -= time * 0.5; boredemLvl -= time * 0.5; experience += time * 0.5 * (1 - boredemLvl/100);; break; case "WeightLifting": energyLvl -= time * 0.5; boredemLvl += time * 0.1; experience += time * 0.3 * (1 - boredemLvl/100);; break; case "Run": energyLvl -= time * 0.3; boredemLvl += time * 0.3; experience += time * 0.8 * (1 - boredemLvl/100);; break; default: undefined; } } function alarm(time) { // animation and souund of alarm at given time. } function bathroomRun(ary) { // this function animate each action in the bathroom run for(let i=0; i<ary.length; i++) { ary[i].play; } } function consuming(ary) { // consuming an array of food for(let i=0; i<ary.length; i++) { if(ary[i].drinkable) { // drink and chew will have different animations. ary[i].drinking; } else { ary[i].chewing; } } } function meditation(time) { // a break during the day that could recharge the hero. let energyGain = time * 0.05; energyLvl += Math.max(20, energyGain); let boredemReduced = time * 0.05; boredemLvl -= Math.max(10, boredemReduced); } function sleep(time) { if (time >= 8) { // 8 hours or more of the sleep will fully recharge energy and reduce boredam to zero. energyLvl = 100; boredemLvl = 0; } else { let factor = time/8:00; energyLvl += (100-energyLvl) * factor; boredemLvl -= boredemLvl * factor; } // animation of sleeping } function energyReplenish() { if(energyLvl <= 50) { foodItems[0].chewing; } } function checkBoredam() { if(boredemLvl >= 99 ){ // if so bored, go back home and sleep right away. endOfDay(); } else if (boredemLvl >= 80) { // set up a warning if it is so bored now. return "You are getting bored!" + "Try meditation to regain some strength!" } } ```