xxxxxxxxxx
757
let gameTimer = 0;
let sceneNum = 0;
//position of the word Level
let levelStartX = 50;
let levelStartY = 370;
let levelButtonStart = 60;
let levelButtonSpacing = 30;
let levelButtonSize = 22;
let l;
//static for now to make it easier, but the goal would be for the level to be changed to add more plants and weeds to manage
let level = 0;
let veggies = [];
let veggieType = ['Tomato', 'Carrot', 'Garlic', 'Lettuce'];
let seedSizeX = 10;
let seedSizeY = 15;
//images
let tomato;
let tomatoPlant;
let toolImg = [];
let weed;
// sounds
let mySound;
//tool stuff
let tools = [];
let toolType = ['Shovel', 'Compost', 'Water Can', 'Clippers'];
let toolActive = false;
let toolGrabbed = false;
let toolExit = false;
let toolBar = 555;
//watering can
let plantWatered = false;
let dp = 100;
//compost position
let cY = 500;
gardenReady = false;
//weed stuff
let weeds = [];
let weedGrow = 1;
let weedSet = false;
let weedLevel;
let weedsCut;
let wX;
let wY;
//top of the ground
let xMiddle = 300;
let groundValue = 300;
let groundMiddle = 350;
let planted = 10;
let plantGrow = 1;
let plantHarvest;
function preload(){
tomato = loadImage('assets/tomato.png');
tomatoPlant = loadImage('assets/tomato_stem.png');
weed = loadImage('assets/weed.png');
for (let i = 0; i<4;i++){
toolImg[i] = loadImage('assets/tool' + i + '.png')
}
soundFormats('m4a', 'ogg');
mySound = loadSound('assets/GardenGrowMusic.m4a');
}
function setup() {
createCanvas(600, 400);
rectMode(CENTER);
background(169, 235, 232);
noStroke();
//mySound.play()
//sound button
//Courtesy Dan Schiffman - https://www.youtube.com/watch?v=YcezEwOXun4
button = createButton("play sound");
button.mousePressed(togglePlaying);
function togglePlaying(){
if (!mySound.isPlaying()){
//mySound.play();
mySound.loop();
mySound.setVolume(0.3);
button.html("stop sound");
} else {
mySound.pause();
button.html("play sound");
}
}
//title Scene
textAlign(CENTER)
textSize(50);
text("Garden Grow", width / 2, 150);
//Description
textSize(30);
text("Take care of your garden", width / 2, 250);
textSize(30);
text("Watch the plants grow", width / 2, 280);
//start game
textSize(20);
text("Click level to Start Game", width / 2, 350);
//choose level
textSize(30);
text("Level", levelStartX, levelStartY + 10);
//levels
for (let l = 0; l < 3; l++) {
//level buttons
fill(255);
rect(levelStartX + (l * levelButtonSpacing) + levelButtonStart, levelStartY, levelButtonSize, levelButtonSize);
//level #
textSize(25);
fill(0);
text(l + 1, levelStartX + l * levelButtonSpacing + levelButtonStart, levelStartY + 10);
}
//veggies/plants/seeds
for (let i = 0; i < 5; i++) {
veggies[i] = new Veggie(i * (width - 50) / 5 + 40, 50, veggieType[i]);
}
//tools needed to grow garden
for (let i = 0; i < 4; i++) {
tools[i] = new Tool(toolBar, ((height - 170) / 4) * i + 70, toolType[i]);
}
//weeds that grow in the garden
for (let i = 0; i < 10; i++) {
let weedX = random(0, width - 100);
let weedY = random(50, 100);
//console.log(weedX);
weeds[i] = new Weed(weedX, groundValue - weedY);
//weed[i].body();
}
}
function draw() {
//console.log(sceneNum);
switch (sceneNum) {
//Title page
case 0:
//console.log('scene 0');
break;
// clear weeds
case 1:
//console.log('scene 1');
break;
// clear weeds
case 2:
//console.log('scene 2');
break;
// plant / grow
case 3:
//console.log('scene 3');
break;
case 4:
//console.log('scene 3');
break;
case 5:
//console.log('scene 3');
break;
case 6:
//console.log('scene 3');
break;
case 7:
//console.log('scene 3');
break;
}
if (sceneNum == 0) {
//title Scene
rectMode(CENTER);
background(169, 235, 232);
noStroke();
imageMode(CENTER);
image(tomato,width/2,height/2+20);
// for(let i = 0; i<4; i++){
// image(toolImg[i],i*50,30,toolImg[i].width/10,toolImg[i].height/10);
// }
textAlign(CENTER)
textSize(50);
fill(0);
text("Garden Grow", width / 2, 90);
//Description
textSize(30);
text("Take care of your garden", width / 2, 250);
textSize(30);
text("Watch the plants grow", width / 2, 280);
//start game
textSize(20);
text("Click level to Start Game", width / 2, 350);
//choose level
textSize(30);
text("Level", levelStartX, levelStartY + 10);
//levels
for (let l = 0; l < 3; l++) {
//level buttons
fill(255);
rect(levelStartX + (l * levelButtonSpacing) + levelButtonStart, levelStartY, levelButtonSize, levelButtonSize);
//level #
textSize(25);
fill(0);
text(l + 1, levelStartX + l * levelButtonSpacing + levelButtonStart, levelStartY + 10);
//console.log(level);
}
}
//SELECT LEVEL
//distance for level 1 button
let levelDist1 = dist(mouseX, mouseY, levelStartX + levelButtonSpacing + levelButtonStart / 2, levelStartY);
//distance for level 2 button
let levelDist2 = dist(mouseX, mouseY, levelStartX + 2 * levelButtonSpacing + levelButtonStart / 2, levelStartY);
//distance for level 3 button
let levelDist3 = dist(mouseX, mouseY, levelStartX + 3 * levelButtonSpacing + levelButtonStart / 2, levelStartY);
//to select level and move to next scene
if (mouseIsPressed == true && sceneNum == 0) {
//change number of plants and weeds in the next level
if (levelDist1 < 15) {
level = 1;
weedLevel = 2;
} else if (levelDist2 < 15) {
level = 2;
weedLevel = 4;
} else if (levelDist3 < 15) {
level = 4;
weedLevel = 10;
} else {
return false;
}
//progress to scene 1 after clicking level
sceneNum = 1;
}
//scene 1 - get instructions for how to care for garden
if (sceneNum >= 1) {
//weedGrow = 1;
//redraw backround
background(169, 235, 232);
// tool section
fill(210);
rect(570, height / 2, 100, height);
for (let i = 0; i < 4; i++) {
tools[i].body(i);
}
//ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
//instructions
if (sceneNum == 1) {
textSize(20);
textAlign(CENTER);
let textStart = 50;
let textShift = 20;
text('Step 1: Prep the garden.', width / 2 - 50, textStart);
text('Remove weeds with the shovel & add compost to soil', width / 2 - 50, textStart + textShift);
text('Step 2: Take care of the plants', width / 2 - 50, textStart + 4 * textShift);
text('Plant the seeds, water the plants', width / 2 - 50, textStart + 5 * textShift);
text('Step 3: Harvest the plants when they are ready', width / 2 - 50, textStart + 7 * textShift + textShift);
text('Use the clippers to harvest', width / 2 - 50, textStart + 8 * textShift + textShift);
//Note ---
fill(255);
textSize(25);
text("Remember: don't let the weeds get too high!", width / 2 - 50, height - 60);
textSize(20);
text("Click here to begin", width / 2 - 50, height - 30);
}
}
//progress to scene 2 - weeds show up
if (mouseIsPressed && sceneNum == 1 && mouseX > 150 && mouseX < 500 && mouseY > 300) {
sceneNum = 2;
//weedSet = true;
}
//Weeds show up after clicking start from instructions
if (sceneNum == 2) {
for (let i = 0; i < weedLevel; i++) {
weedGrow = 50;
weeds[i].body();
//wX = tools[0].x - groundValue;
wY = sqrt((tools[0].y - groundValue) * (tools[0].y - groundValue));
//console.log(wY);
if (wY < 20) {
sceneNum = 3;
weedsCut = true;
}
if (weedsCut){
weedGrow = 0;
weedsCut = false;
}
}
textSize(20);
fill(172, 232, 144);
text('Pull all the weeds', width / 2 - 50, height - 50)
}
//Adding compost
if (sceneNum == 3) {
//ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
textSize(20);
fill(172, 232, 144);
text('Great job! Now add compost for healthy plants', width / 2 - 50, height - 50);
cY = sqrt((tools[1].y - groundValue) * (tools[1].y - groundValue));
//console.log(cY);
if (cY < 20) {
gardenReady = true;
}
if (gardenReady) {
//ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
textSize(20);
fill(172, 232, 144);
text('Sweet! The garden is ready for planting', width / 2 - 50, height - 50);
if (toolExit == false && gardenReady == true) {
gardenReady = false;
sceneNum = 4;
}
}
}
//Planting seeds and taking care of the plants
if (sceneNum >= 4) {
if(sceneNum==4){
textSize(20);
fill(172, 232, 144);
text('Plant the seeds from left to right', width / 2 - 50, height - 50);
}
for (let i = 0; i < level; i++) {
veggies[i].seed();
veggies[i].body();
veggies[i].plant();
if (veggies[level - 1].seedY > groundValue + planted && plantGrow < 91) {
//if (veggies[level - 1].seedY > groundValue + planted) {
//if (sceneNum == 4){
//console.log(veggies[i].seedY);
//ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
textSize(20);
fill(172, 232, 144);
text('Water the plants and watch them grow', width / 2 - 50, height - 50);
//}
}
}
// watering can use
for (let i = 0; i < level; i++) {
dp = tools[2].x - veggies[i].x;
//console.log(dp);
if (dp < 20) {
//tools[0].water();
plantWatered = true;
}
//plant grow
if (plantWatered) {
//veggies[i].SeedY = ;
seedSizeX = 0;
seedSizeY = 0;
plantGrow = 91;
// ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
if (plantGrow == 91) {
fill(172, 232, 144);
textSize(20);
textAlign(CENTER);
text('Congratulations! Your garden is ready to harvest', 250, 350)
}
if (toolExit == false && plantWatered == true) {
plantWatered = false;
sceneNum = 5;
}
}
}
}
//Harvest
if (sceneNum == 5) {
// ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
if (plantGrow == 91) {
fill(172, 232, 144);
textSize(20);
textAlign(CENTER);
text('Congratulations! Your garden is ready to harvest', 250, 350)
}
// clipper use
for (let i = 0; i < level; i++) {
dp = tools[3].x - veggies[i].x;
if (dp < 20) {
plantHarvest = true;
}
//plant grow
if (plantHarvest){
plantGrow = 1;
// ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
if (plantGrow == 1) {
fill(172, 232, 144);
textSize(20);
textAlign(CENTER);
if (level <4){
text('Great job! Now try the next level', 250, 350)
} else {
text('You win! You have a green thumb', 250, 350)
}
}
if (toolExit == false && plantHarvest == true) {
plantHarvest = false;
plantGrow = 1;
sceneNum = 0;
seedSizeX = 10;
seedSizeY = 15;
gameTimer = 0;
}
}
}
}
// work in process - weeds growing against the timer
// console.log(`gameTimer is ${gameTimer}`);
// if (sceneNum >=2){
// gameTimer = gameTimer+frameCount%60/100;
// }
// if (gameTimer > 80 && sceneNum >=2) {
// if (gameTimer > 150) {
// for (let i = 0; i < weedLevel; i++) {
// //redraw ground
// fill(115, 71, 10);
// rect(xMiddle, groundMiddle, width, height - groundValue);
// weeds[i].body();
// weedGrow = weedGrow*1.1;
// }
// for (let i = 0; i < weedLevel; i++) {
// weeds[i].body();
// //redraw ground
// fill(115, 71, 10);
// rect(xMiddle, groundMiddle, width, height - groundValue);
// weedGrow = weedGrow*1.2;
// }
// //weedGrow = weedGrow+5;
// if (weedGrow >=100){
// //redraw ground
// fill(115, 71, 10);
// rect(xMiddle, groundMiddle, width, height - groundValue);
// textSize(20);
// fill(172, 232, 144);
// text('Hurry, before they get too big!', width / 2 - 50, height - 20)
// }
// }
// }
// //console.log(`weeds are ${weedGrow}`);
// // Work in process - how to make calculate weed height?
// if (weedGrow >= 150) {
// //console.log('weeds grew too much!');
// textSize(20);
// fill(172, 232, 144);
// text('Weeds got too big!', width / 2 - 50, height - 30)
// weedGrow = 0;
// gameTimer = 0;
// sceneNum = 0; // reset game
// }
// weedGrow = 0;
} //draw closing bracket
class Tool {
constructor(x, y, t) { //only sets up once - STATIC
this.x = x;
this.y = y;
this.x1 = x;
this.y1 = y;
this.t = t;
this.c = color(104, 115, 114);
this.d; //continuous sensing of distance, not just once
this.toolGrabbed = false;
this.waterC = color(28, 120, 202);
this.waterX = x;
this.waterY = y;
this.waterPlant = false;
}
body(index) {
//fill(this.c);
// rect(this.x, this.y, 30, 20);
this.d = dist(mouseX, mouseY, this.x, this.y);
for(let i = 0; i<4; i++){
imageMode(CENTER);
image(toolImg[index],this.x,this.y,toolImg[index].width/10,toolImg[index].height/10);
}
//set x,y back to original and disengage the tool back to the tool section
if (toolActive == false && this.d < 40) {
if (mouseIsPressed) {
toolActive = true;
this.toolGrabbed = true;
}
} else {
this.x = this.x1;
this.y = this.y1;
}
if (this.toolGrabbed && toolActive) {
this.x = mouseX;
this.y = mouseY;
} else {
this.x = this.x1;
this.y = this.y1;
}
if (this.x < toolBar - 20) {
toolExit = true;
}
// under the condition that the tool has been grabbed & has exited the tool bar, when it returns to the tool bar reset everything
if (this.toolGrabbed && toolExit) {
if (this.x > toolBar - 20) {
this.x = 555;
this.y = this.y1;
toolExit = false;
this.toolGrabbed = false;
//toolGrabbed = false;
toolActive = false;
}
//this.toolGrabbed = false;
}
fill(this.c);
textSize(11);
text(this.t, this.x1, this.y1 + 30);
// console.log(`tool grabbed is ${this.toolGrabbed} and tool exit is ${toolExit} and mousePress is ${mouseIsPressed} and toolActive is ${toolActive} and thisX1 is ${this.x1} and thisY1 = ${this.y1}`);
//console.log(this.x1);
//console.log(this.y1);
}
}
class Weed {
constructor(x, y) {
this.x = x;
this.y;
this.c = color(172, 232, 144);
this.ySize;
this.centerY;
this.xSize = random(10, 30);
}
body() {
fill(this.c);
this.y = 2 * weedGrow;
this.centerY = groundValue - weedGrow;
//rect(this.x, this.centerY, this.xSize, 2 * weedGrow);
imageMode(CENTER);
image(weed,this.x, this.centerY,this.xSize,2 * weedGrow);
}
}
class Veggie {
constructor(x, y, t) {
this.x = x;
this.y = y;
this.xSize = 30;
this.ySize = 20;
this.seedX = x;
this.seedY = y;
this.c = color(227, 43, 18);
this.seedC = color(145, 78, 49);
this.d;
this.t = t;
this.seedPlanted = false;
this.plantC = color(12, 128, 9);
}
body() {
// fill(this.c);
// rect(this.x, this.y, this.xSize, this.ySize);
imageMode(CENTER);
image(tomato,this.x,this.y,tomato.width/5,tomato.height/5);
// textSize(11);
// text(this.t, this.x, this.y + 20);
//distance calculator
this.d = dist(mouseX, mouseY, this.x, this.y);
// if (this.d < 20 && mouseIsPressed == true) {
// //console.log(this.d);
// }
}
seed() {
fill(this.seedC);
ellipse(this.seedX, this.seedY, seedSizeX, seedSizeY);
this.d = dist(mouseX, mouseY, this.x, this.y);
if (this.d < 20 && mouseIsPressed) {
this.seedPlanted = true;
}
if (this.seedPlanted == true) {
if (this.seedY <= groundValue + planted) {
this.seedY++;
}
//this.xSize = 1;
//this.ySize = 1;
}
}
plant() {
if (this.seedY >= groundValue + planted) {
fill(this.plantC);
//reminder to change the center y point from being static and the growth
//plantGrow = 0;
//rect(this.seedX, groundValue + planted - 45, 10, plantGrow);
imageMode(CENTER);
image(tomatoPlant,this.x,groundValue + planted-70,plantGrow,plantGrow);
}
}
}