xxxxxxxxxx
617
const dom = {
reButton: document.getElementById('reButton'),
stButton: document.getElementById('stButton'),
cnvContainer: document.getElementsByClassName('cnvcontainer'),
controlHerb: document.getElementById('controlHerb'),
herb: false,
controlCarn: document.getElementById('controlCarn'),
carn: false,
go: false,
nameContainer:document.getElementsByClassName("nameContainer"),
playerName: document.getElementById('playerName'),
subButton: document.getElementById('subButton'),
playerNameInPut:document.getElementsByClassName('inputName'),
howToPlay:document.getElementsByClassName('howToPlay'),
startGame:document.getElementById("startGame"),
score:document.getElementsByClassName("score"),
scorePoint: 0,
scoreSheet:"https://sheetsu.com/a/f/17fc14a4da00",
gameover:document.getElementsByClassName("gameover"),
scoreTable :document.getElementById("scoreTable"),
resetButton: document.getElementById('resetButton'),
}
const store = {
frame: {
cols: "",
rows: "",
pix: "",
x: "",
y: "",
count: ""
},
world: [],
cells: {
water: [],
tree: [],
herb: [],
carn: [],
human: [],
dead: []
},
lifes: ['#FFFFFF', '#8BD4FF', '#5EE886', '#FFFFA5', '#6338A1', '#000000', '#D3D3D3']
}
const setter = {
create2DArray(_cols, _rows) {
let arr = new Array(_cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(_rows);
for (let j = 0; j < arr[i].length; j++) {
arr[i][j] = new Array(0, 100);
}
}
return arr
},
createWorld(_cols, _rows) {
store.world = setter.create2DArray(_cols, _rows);
setter.creareWater(_cols, _rows);
getter.species(store.cells.water, 1);
setter.createTree(_cols, _rows);
getter.species(store.cells.tree, 2);
setter.Animals(_cols, _rows, store.cells.herb);
getter.species(store.cells.herb, 3);
setter.Animals(_cols, _rows, store.cells.carn);
getter.species(store.cells.carn, 4);
},
creareWater(_cols, _rows) {
let a = floor(random(_rows / 3)); //start point
let b = random(0.25, 0.6); //alt rate
//create water
if (floor(random(2)) == 1) { //start top or bottom
//draw from top
for (let i = 0; i < _cols; i++) {
//move down a cell
while (random(1) < b) {
if (a < _rows - 1) {
a++;
// _world[i][j][0] = 1
store.cells.water.push([i, a])
}
}
// _world[i][j][0] = 1
store.cells.water.push([i, a])
}
} else {
let j = _rows - 1 - a
for (let i = 0; i < _cols; i++) {
if (j >= 0)
while (random(1) < 0.25) {
if (j >= 0) {
j--;
// _world[i][j][0] = 1;
store.cells.water.push([i, j])
}
}
// _world[i][j][0] = 1;
store.cells.water.push([i, j])
}
}
},
createTree(_cols, _rows) {
let a = floor(random(6,10)); //number of forest
let counter = floor(random(30, 50));
// i = index
let i = 0
while (i < a) { //count
//random x y
let x = floor(random(10, _cols - 10));
let y = floor(random(10, _rows - 10));
//if x y isnt water
if (store.world[x][y][0] != 1) {
//expand
let _x;
_cols / 2 > x ? _x = 1 : _x = -1;
for (let j = 0; j < counter; j++) {
random(1) > 0.6 ? x += _x : x % 2 == 1 ? y++ : y--;
if (x >= 0 && x < _cols && y >= 0 && y < _rows) {
if (store.world[x][y][0] == 1) {
continue;
}
// _world[x][y][0] = 2
store.cells.tree.push([x, y])
}
}
i++ //count index
}
}
},
//temp
Animals(_cols, _rows, animal) {
let a = floor(random(4,8)); //number of forest
let counter = floor(random(8,15));
// i = index
let i = 0
while (i < a) { //count
//random x y
let x = floor(random(10, _cols - 10));
let y = floor(random(10, _rows - 10));
//if x y isnt water
if (store.world[x][y][0] != 1 && store.world[x][y][0] != 2) {
//expand
let _x;
_cols / 2 > x ? _x = 1 : _x = -1;
for (let j = 0; j < counter; j++) {
random(1) > 0.6 ? x += _x : x % 2 == 1 ? y++ : y--;
if (x >= 0 && x < _cols && y >= 0 && y < _rows) {
if (store.world[x][y][0] == 1 && store.world[x][y][0] == 2) {
continue;
}
// _world[x][y][0] = 2
animal.push([x, y])
}
}
i++ //count index
}
}
},
createMan() {
store.cells.human.push([0, store.frame.rows / 2])
// console.log(store.frame.rows/2)
return false;
},
manMove(a) {
// if ( store.cells.human[0]>=5){a = -1}
if (a <= 3) {
store.cells.human[0][0]++
} else if (a <= 6) {} else if (a < 9) {
store.cells.human[0][0]--;
} else {
store.cells.human.splice(0, 1)
}
},
createMan2() {
store.cells.human.push([1, store.frame.rows / 2])
store.cells.human.push([0, store.frame.rows / 2] - 1)
store.cells.human.push([0, store.frame.rows / 2] + 1)
store.cells.human.push([0, store.frame.rows / 2])
// console.log(store.frame.rows/2)
},
}
const grow = {
boarder(_n, _min, _max) {
if (_n < _min) {
return _min;
} else if (_n > _max) {
return _max;
} else {
return _n;
}
},
countSpecies(index, x, y, a) {
let counter = 0;
for (let u = -a; u <= a; u++) {
for (let v = -a; v <= a; v++) {
if (u != 0 || v != 0) {
store.world[grow.boarder(x + u, 0, store.frame.cols - 1)][grow.boarder(y + v, 0, store.frame.rows - 1)][0] == index ? counter++ : counter = counter;
}
}
}
return counter;
},
nextGen(_cols, _rows) {
for (let i = 0; i < _cols; i++) {
for (let j = 0; j < _rows; j++) {
//count live neighbers
let sumWater = 0
let sumTree = 0
let sumHerb = 0
let sumCarn = 0
let sumMan = 0
//count Neughbors
sumWater = grow.countSpecies(1, i, j, 1);
sumTree = grow.countSpecies(2, i, j, 2); // tree
sumHerb = grow.countSpecies(3, i, j, 2); // herb
sumCarn = grow.countSpecies(4, i, j, 2); // carn
sumMan = grow.countSpecies(5, i, j, 2); // human
if (store.world[i][j][0] == 1) {
//water
} else if (store.world[i][j][0] == 2) {
//tree
// if (sumTree < 2 || sumTree > 5 || sumHerb > 0) {
// store.cells.tree = store.cells.tree.filter((a => a[0] != i || a[1] != j));
// }
if (sumWater >= 2) {
store.world[i][j][1] = store.world[i][j][1] + 5
}
if (sumWater < 2) {
store.world[i][j][1] = store.world[i][j][1] -10
}
if (sumMan > 0) {
store.world[i][j][1] = store.world[i][j][1] - sumMan * 30
}
if (sumHerb > 0) {
store.world[i][j][1] = store.world[i][j][1] - sumHerb * 10
}
if (sumTree < 2) {
store.world[i][j][1] = store.world[i][j][1] -= 10;
} else if (sumTree < 5) {
store.world[i][j][1] -= 4
} else if (sumTree < 12) {
store.world[i][j][1] -= 2
} else if (sumTree < 8) {
store.world[i][j][1] -= 3
} else if (sumTree < 14) {
store.world[i][j][1] -= 4
} else if (sumTree < 20) {
store.world[i][j][1] -= 5
} else {
store.world[i][j][1] -= 8
}
if (store.world[i][j][1] <= 0) {
store.cells.tree = store.cells.tree.filter((a => a[0] != i || a[1] != j));
}
} else if (store.world[i][j][0] == 3) {
//herb
// if (sumHerb < 2 || sumHerb > 3 || sumCarn > 0) {
// store.cells.herb = store.cells.herb.filter((a => a[0] != i || a[1] != j));
//
if (sumTree > 1) {
store.world[i][j][1] = store.world[i][j][1] + 10
}
if (sumTree <= 1) {
store.world[i][j][1] = store.world[i][j][1] - 15
}
if (sumMan > 0) {
store.world[i][j][1] = store.world[i][j][1] - sumMan * 20
}
if (sumCarn > 0) {
store.world[i][j][1] = store.world[i][j][1] - sumCarn * 5
}
if (sumHerb < 2) {
store.world[i][j][1] = store.world[i][j][1] -= 12;
} else if (sumHerb < 4) {
store.world[i][j][1] = store.world[i][j][1] -= 5;
} else if (sumHerb < 7) {
store.world[i][j][1] -= 2
} else if (sumHerb < 15) {
store.world[i][j][1] -= 1
} else if (sumHerb < 8) {
store.world[i][j][1] -= 2
} else if (sumHerb < 14) {
store.world[i][j][1] -= 3
} else if (sumHerb < 20) {
store.world[i][j][1] -= 4
} else {
store.world[i][j][1] -= 5
}
if (store.world[i][j][1] <= 0) {
store.cells.herb = store.cells.herb.filter((a => a[0] != i || a[1] != j));
}
} else if (store.world[i][j][0] == 4) {
//carn
// if (sumCarn < 2 || sumCarn > 3) {
// store.cells.carn = store.cells.carn.filter((a => a[0] != i || a[1] != j));
// }
if (sumTree > 1) {
store.world[i][j][1] = store.world[i][j][1] + 3
}
if (sumHerb > 1) {
store.world[i][j][1] = store.world[i][j][1] + 5
}
if (sumHerb <= 1) {
store.world[i][j][1] = store.world[i][j][1] -15
}
if (sumMan > 0) {
store.world[i][j][1] = store.world[i][j][1] - sumMan * 10
}
if (sumCarn > 0) {
store.world[i][j][1] = store.world[i][j][1] - sumMan * 5
}
if (sumHerb < 2) {
store.world[i][j][1] = store.world[i][j][1] -= 7;
} else if (sumHerb < 3) {
store.world[i][j][1] = store.world[i][j][1] -= 3;
} else if (sumHerb < 6) {
store.world[i][j][1] -= 2
} else if (sumHerb < 9) {
store.world[i][j][1] -= 1
} else if (sumHerb < 8) {
store.world[i][j][1] -= 2
} else if (sumHerb < 14) {
store.world[i][j][1] -= 3
} else if (sumHerb < 20) {
store.world[i][j][1] -= 4
} else {
store.world[i][j][1] -= 5
}
if (store.world[i][j][1] <= 0) {
store.cells.carn = store.cells.carn.filter((a => a[0] != i || a[1] != j));
}
} else if (store.world[i][j][0] == 5) {
//human
if (sumTree > 0) {
store.world[i][j][1] = store.world[i][j][1] + 1
}
if (sumHerb > 0) {
store.world[i][j][1] = store.world[i][j][1] + 1
}
if (sumCarn > 0) {
store.world[i][j][1] = store.world[i][j][1] +1
}else if (sumCarn > 20) {
store.world[i][j][1] = store.world[i][j][1] -200
}
if (sumMan < 3) {
store.world[i][j][1] = store.world[i][j][1] -= 8;
} else if (sumMan < 6) {
store.world[i][j][1] -= 6
} else if (sumMan < 9) {
store.world[i][j][1] -= 2
} else if (sumMan < 8) {
store.world[i][j][1] -= 4
} else if (sumMan < 14) {
store.world[i][j][1] -= 6
} else if (sumMan < 20) {
store.world[i][j][1] -= 8
} else {
store.world[i][j][1] -= 10
}
if (store.world[i][j][1] <= 0) {
store.cells.human = store.cells.human.filter((a => a[0] != i || a[1] != j));
store.cells.dead.push([i, j]);
}
} else if (store.world[i][j][0] == 6) {
if (sumMan >= 6 && sumMan <= 10) {
store.cells.dead = store.cells.dead.filter((a => a[0] != i || a[1] != j));
store.cells.human.push([i, j]);
}
//deadcell
} else if (store.world[i][j][0] == 0) {
if (sumMan >= 3 && sumMan <= 18) {
store.cells.human.push([i, j]);
store.world[i][j][1] = 100;
}else if (sumTree >= 6 && sumTree <= 15) {
store.cells.tree.push([i, j]);
store.world[i][j][1] = 100;
} else if (sumHerb >= 6 && sumHerb <= 15) {
store.cells.herb.push([i, j]);
store.world[i][j][1] = 25;
} else if (sumCarn >= 4 && sumCarn <= 10) {
store.cells.carn.push([i, j]);
store.world[i][j][1] = 25;
}
}
}
}
}
}
const getter = {
render(cols, rows, pix) {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let x = i * pix;
let y = j * pix;
let c = store.lifes[store.world[i][j][0]]
fill(c)
rect(x, y, pix, pix);
}
}
},
species(s, index) {
for (let i = 0; i < s.length; i++) {
let x = s[i][0];
let y = s[i][1];
store.world[grow.boarder(x, 0, store.frame.cols - 1)][grow.boarder(y, 0, store.frame.rows - 1)][0] = index;
}
},
allSpecies() {
// store.nextworld = setter.create2DArray(store.frame.cols, store.frame.rows);
for (let i = 0; i < store.frame.cols; i++) {
for (let j = 0; j < store.frame.rows; j++) {
store.world[i][j][0] = 0;
}
}
getter.species(store.cells.water, 1);
getter.species(store.cells.tree, 2);
getter.species(store.cells.herb, 3);
getter.species(store.cells.carn, 4);
getter.species(store.cells.human, 5);
getter.species(store.cells.dead, 6);
}
}
function preload() {
store.frame.cols = 80;
store.frame.rows = 40;
store.frame.pix = 20;
store.frame.x = store.frame.cols * store.frame.pix+1;
store.frame.y = store.frame.rows * store.frame.pix+1;
setter.createWorld(store.frame.cols, store.frame.rows);
}
let man;
function setup() {
loadJSON(dom.scoreSheet)
frameRate(12);
let cnv = createCanvas(store.frame.x, store.frame.y);
cnv.parent(dom.cnvContainer[0]);
// setter.createWorld(store.frame.cols, store.frame.rows);
// console.log(store.world)
man = true
}
function draw() {
// background(220);
getter.render(store.frame.cols, store.frame.rows, store.frame.pix, 255);
// grow.treeGrows();
if (frameCount%4 == 0){
if (store.frame.count >= 10 && store.frame.count <= 20) {
if (store.world[0][ store.frame.rows / 2][1] !=5) {
man = setter.createMan(man);
// } else {
// setter.manMove(store.frame.count - 10);
}
}
if (store.frame.count == 30) {
// if (man == true) {
// man = setter.createMan2();
man = setter.createMan(man);
}
// }
}
if (dom.go == true) {
// grow.nextGen(store.frame.cols, store.frame.rows);
// getter.allSpecies();p
if (frameCount%4 == 0){
grow.nextGen(store.frame.cols, store.frame.rows);
store.frame.count++;
dom.scorePoint = store.frame.count;
}
}
getter.allSpecies();
dom.score[1].innerHTML = dom.scorePoint;
if (store.cells.tree.length < 10) {
gameover();
}
}
function gameover() {
dom.nameContainer[0].classList.remove("hide");
dom.gameover[0].classList.remove("hide");
dom.scoreTable.innerHTML=dom.playerName.value+" ------ "+dom.scorePoint
dom.go = false;
stButton.value = "Play";
// valReset();
}
//-----------------------DOM--------------------------------//
dom.reButton.addEventListener("click", valReset);
dom.stButton.addEventListener("click", st);
dom.controlHerb.addEventListener("click", addHerb);
dom.controlCarn.addEventListener("click", addCarn);
dom.resetButton.addEventListener("click", valReset);
function valReset() {
var r = confirm("Do you want ot start over?");
if (r == true) {
dom.go = false;
stButton.value = "start!"
store.frame.count = 0;
store.cells.water = [];
store.cells.tree = [];
store.cells.herb = [];
store.cells.carn = [];
store.cells.human = []
store.cells.dead = []
setter.createWorld(store.frame.cols, store.frame.rows);
dom.herb = false;
controlHerb.style.border = "outset"
dom.carn = false;
controlCarn.style.border = "outset"
dom.nameContainer[0].classList.remove("hide");
dom.playerNameInPut[0].classList.remove("hide");
dom.gameover[0].classList.add("hide");
dom.scorePoint = 0
}
}
function st() {
if (dom.go == true) {
dom.go = false
stButton.value = "Play"
// stButton.value = "Pause"
} else if (dom.go == false) {
dom.go = true
// stButton.value = "Play"
stButton.value = "Pause"
}
}
function addHerb() {
dom.herb = true;
dom.carn = false;
controlHerb.style.border = "inset"
controlCarn.style.border = "outset"
}
function addCarn() {
dom.carn = true;
controlCarn.style.border = "inset"
dom.herb = false;
controlHerb.style.border = "outset"
}
function mousePressed() {
if (dom.herb == true) {
// console.log(mouseX,mouseY)
let p = store.frame.pix;
let x = floor(mouseX / p);
let y = floor(mouseY / p);
store.cells.herb.push([x, y])
store.cells.herb.push([x + 1, y])
store.cells.herb.push([x - 1, y])
store.cells.herb.push([x + 1, y + 1])
store.cells.herb.push([x - 1, y + 1])
store.cells.herb.push([x + 1, y - 1])
store.cells.herb.push([x - 1, y - 1])
store.cells.herb.push([x, y + 1])
store.cells.herb.push([x, y - 1])
store.cells.herb.push([x + 2, y])
store.cells.herb.push([x - 2, y])
store.cells.herb.push([x, y + 2])
store.cells.herb.push([x, y - 2])
// dom.herb = false
// controlHerb.style.border = "outset"
// console.log(x,y)
}
if (dom.carn == true) {
// console.log(mouseX,mouseY)
let p = store.frame.pix;
let x = floor(mouseX / p);
let y = floor(mouseY / p);
store.cells.carn.push([x + 1, y])
store.cells.carn.push([x, y + 1])
store.cells.carn.push([x, y - 1])
store.cells.carn.push([x - 1, y])
// dom.carn = false
// controlCarn.style.border = "outset"
// console.log(x,y)
}
}