xxxxxxxxxx
403
let testVeggie;
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 weedLevel = 1; //static for now. will be used to make differences in levels
let veggies = [];
let veggieType = ['Tomato', 'Carrot', 'Garlic', 'Lettuce'];
let seedSizeX = 10;
let seedSizeY = 15;
let tools = [];
let toolType = ['Water Can', 'Weed Killer', 'Compost', 'Clippers'];
let toolActive = false;
let toolGrabbed = false;
let toolExit = false;
let toolBar = 555;
let weeds = [];
let weedGrow = 90;
let weedWidth;
//top of the ground
let xMiddle = 300;
let groundValue = 300;
let groundMiddle = 350;
let planted = 10;
let plantGrow = 0;
let levelPlants = 0;
let plantWatered = false;
let dp = 100;
function setup() {
createCanvas(600, 400);
rectMode(CENTER);
background(169, 235, 232);
noStroke();
//title Scene
textAlign(CENTER)
textSize(50);
text("Garden Grow", width / 2, 100);
//Description
textSize(30);
text("Take care of your garden", width / 2, 170);
textSize(30);
text("Watch the plants grow", width / 2, 220);
//start game
textSize(20);
text("Click a level to Start Game", width / 2, 300);
//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);
}
//set up veggies/plants/seeds
// testVeggie = new Veggie(100, 100, 'test');
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]);
}
}
function draw() {
//console.log(sceneNum);
switch (sceneNum) {
//Title page
case 0:
//console.log('scene 0');
break;
// plant seed
case 1:
//console.log('scene 1');
break;
// grow / harvest
case 2:
//console.log('scene 2');
break;
// compost
case 3:
//console.log('scene 3');
break;
}
//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
sceneNum = 1;
}
//scene 1 - plants get planted
if (sceneNum >= 1) {
//redraw backround
background(169, 235, 232);
//ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
// tool section
fill(210);
rect(570, height / 2, 100, height);
for (let i = 0; i < 4; i++) {
tools[i].body();
}
//set up veggies
// testVeggie.seed();
// testVeggie.body();
// testVeggie.plant();
//sceneNum = 2;
for (let i = 0; i < level; i++) {
veggies[i].seed();
veggies[i].body();
veggies[i].plant();
//console.log(veggies[i].seedY);
// if (veggies[i].seedY > groundValue + planted) {
// for (let levelPlants = 0; levelPlants < level; levelPlants++) {
// console.log(levelPlants);
// console.log('scene = ' + sceneNum);
// if (levelPlants == level) {
// console.log(levelPlants+' changed');
// sceneNum = 2;
// console.log('scene = ' + sceneNum);
// }
// }
// }
//for (let levelPlants = 0; levelPlants < level; levelPlants++) {
if (veggies[i].seedY > groundValue + planted) {
//console.log(i);
//console.log('scene = ' + sceneNum);
//sceneNum = 2;
//console.log('scene = ' + sceneNum);
}
// }
//}
}
//TODO: nested for loop to go through each of the plants
if (sceneNum == 2) {
for (let i = 0; i < level; i++)
dp = tools[0].x - veggies[i].x;
//console.log(dp);
if (dp < 20) {
//tools[0].water();
plantWatered = true;
}
//plant grow
if (plantWatered == true) {
while (plantGrow < 100) {
plantGrow = plantGrow++;
//console.log(plantGrow);
}
sceneNum = 3;
}
}
if (sceneNum == 3) {
textSize(20);
textAlign(CENTER);
text('Awesome work! Your plant is ready for harvest', 250, 350)
}
}
}
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);
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);
}
}
}
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() {
fill(this.c);
rect(this.x, this.y, 30, 20);
this.d = dist(mouseX, mouseY, this.x, this.y);
//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;
}
textSize(11);
text(this.t, this.x1, this.y1 + 20);
// 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);
}
//will had water fall from the watering can if possible
// water() {
// fill(this.waterC);
// rect(this.waterX, this.waterY, 5, 10);
// this.w = this.waterY - this.y;
// if (this.w < 30) {
// this.waterX = mouseX;
// this.waterY = mouseY;
// this.waterPlant = true;
// }
// if (this.waterPlant == true) {
// if (this.waterY <= groundValue + planted) {
// this.waterY++;
// }
// }
// }
}