xxxxxxxxxx
293
let sceneNum = 0;
//position of the word Level
let levelStart = 50;
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;
let veggies = [];
let veggieType = ['Tomato', 'Carrot', 'Garlic', 'Lettuce'];
let tools = [];
let toolType = ['Water Can', 'Weed Killer', 'Fertilizer', 'Clippers'];
let weeds = [];
let weedGrow = 0;
//top of the ground
let xMiddle = 300;
let groundValue = 300;
let groundMiddle = 350;
let planted = 10;
let plantGrow = -71;
function setup() {
createCanvas(600, 400);
rectMode(CENTER);
background(169, 235, 232);
noStroke();
//title Scene
textAlign(CENTER)
textSize(50);
text("Garden Grow", width / 2, 200);
//start game
textSize(30);
text("Click level to Start Game", width / 2, 300);
//choose level
textSize(30);
text("Level", levelStart, 50);
//levels
for (let l = 0; l < 3; l++) {
//level buttons
fill(255);
rect(levelStart + (l * levelButtonSpacing) + levelButtonStart, 40, levelButtonSize, levelButtonSize);
//level #
textSize(25);
fill(0);
text(l + 1, levelStart + l * levelButtonSpacing + levelButtonStart, 50);
console.log(level);
}
//set up veggies/plants/seeds
for (let i = 0; i < 5; i++) {
veggies[i] = new Veggie(i * (width - 50) / 5 + 40, 50,veggieType[i]);
// seeds[i] = new Seed(i * (width - 50) / 5 + 40 + 17, 50);
// plants[i] = new Plant(i * (width - 50) / 5 + 40 + 11, groundValue);
}
//tools needed to grow garden
for (let i = 0; i < 4; i++) {
tools[i] = new Tool(555, ((height - 170) / 4) * i + 70, toolType[i]);
}
//weeds that grow in the garden
for (let i = 0; i < 4; i++) {
weeds[i] = new Weed(random(0, width - 100), groundValue);
}
}
function draw() {
switch (sceneNum) {
//Title page
case 0:
//console.log('scene 0');
break;
// plant seed
case 1:
//console.log('scene 1');
break;
// harvest
case 2:
//console.log('scene 2');
break;
// compost
case 3:
//console.log('scene 3');
break;
}
//select level 1 - static for now, will change
let levelDist = dist(mouseX, mouseY, levelStart + levelButtonSpacing + levelButtonStart / 2, 40);
//to select level 1
if (mouseIsPressed == true && levelDist < 10) {
level = 1;
sceneNum = 1;
weedLevel = 2;
//console.log(level);
}
if (sceneNum == 1) {
//redraw backround
background(169, 235, 232);
//ground
fill(115, 71, 10);
rect(xMiddle, groundMiddle, width, height - groundValue);
for (let i = 0; i < level; i++) {
veggies[i].seed();
veggies[i].body();
veggies[i].plant();
}
// tool section
fill(210);
rect(570, height / 2, 100, height);
for (let i = 0; i < 4; i++) {
tools[i].body();
}
let d = dist(tools[0].x, tools[0].y, veggies[0].seedX, groundValue);
//nested for loop to go through each of the plants
// console.log(millis());
// if (millis()>500){
for (let i = 0; i < 4; i++) {
weeds[i].body();
//weedGrow=-91;
weeds[i].move();
}
// }
}
}
class Veggie {
constructor(x, y,t) {
this.x = x;
this.y = y;
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, 30, 20);
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, 15, 10);
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++;
}
}
}
plant() {
if (this.seedY >= groundValue + planted) {
fill(this.plantC);
//reminder to change the center y point from being static and the growth
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;
}
body() {
fill(this.c);
rect(this.x, this.y, 30, 20);
this.d = dist(mouseX, mouseY, this.x, this.y);
if (this.d < 40) {
if (mouseIsPressed == true) {
this.toolGrabbed = true;
} else {
this.toolGrabbed = false;
}
}
if (this.toolGrabbed == true) {
this.x = mouseX;
this.y = mouseY;
} else {
this.x = this.x1;
this.y = this.y1;
}
textSize(11);
text(this.t, this.x, this.y + 20);
}
}
class Weed {
constructor(x, y) {
this.x = x;
this.y = y;
this.c = color(172, 232, 144);
}
body() {
fill(this.c);
rect(this.x, groundValue-45, random(5,15), -weedGrow);
}
move() {
if (this.y < groundValue) {
this.y--;
}
}
// Work in process - do I need collison for the end of the canvas?
checkCollision() {
if (weed.y == 0) {
console.log('weeds grew too much!');
numScene = 0; // reset game
//growLives--;
}
}
}