xxxxxxxxxx
884
//OBJECT INSTANTIATION
let player;
let ground;
let enemy;
let collectibleIcon;
let platform;
let clouds = [];
let collectibles = [];
let enemies = [];
let collectibleCount = [];
let experience = [];
//TIMING
let jumpInterval;
let counter = 0;
let intervalSpeed = 100;
//TECHNICAL CHECKS
let spacePressed = false;
let collisionBounce = false;
let enemyCollisionHeight = false;
let enemyCollisionSide = false;
let collectibleCollision = false;
let enemyCollisionOnTop = false;
let platformCollisionOnTop = false;
let wallCollision = false;
let moveForward = false;
let moveBackward = false;
let enemyDeathAnimation = false;
//LEVEL CHECKS
let paused = false;
//INPUT STORAGE
let spaceHistory = [];
//LOCATION HISTORY
let objectLocation = []; //coordinate system based checking (x,y,z)
//MATH AND FUNCTIONS
let waveModifier = 0;
let waveAngle = 0;
let cameraRotationX = 0;
let cameraRotationY = 0;
//MISC
let textString = '';
function preload() {
oswald = loadFont('/Oswald.ttf');
bgMusic = loadSound('Retroland.mp3');
collectibleSound = loadSound('sounds/Pop (1).mp3');
jumpSound = loadSound('sounds/Pop (4) [Bubble Light].mp3');
enemyDeathSound = loadSound('sounds/Pop (2) [Defeated].mp3');
}
function setup() {
//createCanvas(1024, 576, WEBGL);
createCanvas(windowWidth, windowHeight, WEBGL);
noCursor();
angleMode(DEGREES);
colorMode(HSB);
noStroke();
audio();
player = new Player(0, 0, 0, 100, 100, 100, 0, 0.25, 0.75, 1);
ground = new Ground(0, (player.h / 2 + 5), 0, 1000, 40000);
platform = new Platform();
collectibleIcon = new Collectible(0, 0, 0);
for (let z = 2000; z < 20000; z = z + 1000/random(2)) {
for (let x = -400; x < 500; x = x + 400/random(2)) {
enemies.push(new Enemy(x , 0, -z, 1))
}}
for (let x = 0; x < 7; x = x + 1) {
for (let z = 0; z < 7; z = z + 1)
clouds.push(new Cloud(random(-width*2,width*2), random(0,-20000), random(-350,-700)))
}
for (let z = 500; z < 20000; z = z + 500) {
for (let x = -400; x < 600; x = x + 200) {
collectibles.push(new Collectible(x, 0, -z))
}
}
}
function draw() {
background(180, 20, 90);
//MATH
waveModifier = waveModifier + 4.5
wave = sin(waveModifier) * 20
//UPDATE
player.update();
ground.update();
//platform.update();
for (let i = 0; i < enemies.length; i = i + 1) {
enemies[i].update();
}
for (let i = 0; i < collectibles.length; i = i + 1) {
collectibles[i].update();
}
//CAMERA
camera(player.position.y + cameraRotationX, player.y - 300, player.position.x + 500,
player.position.y + cameraRotationX, player.y + cameraRotationY, player.position.x,
0, 1, 0);
//LIGHTS
ambientLight(0, 0, 75)
pointLight(0, 0, 500)
//CONTROLS and FUNCTIONS
manageInput();
detectCollision();
tracker();
jump();
generateClouds();
uI();
cameraMovement();
message();
}
//FUNCTIONS
function audio(){
bgMusic.playMode('sustain');
bgMusic.setVolume(0.1);
bgMusic.play();
}
function message(){
if (collectibleCount.length == 0) {
textString = 'Use the WASD keys to Move'
}
if (collectibleCount.length == 1) {
textString = 'Press the SPACE button to Jump'
}
if (experience.length == 1){
textString = 'Jump on enemies to gain Experience'
}
if (experience.length == 1 && collectibleCount == 1){
textString = "Be careful, if you touch an enemy you'll lose your spirit"
}
if (experience.length> 10){
textString = "Don't give up, you can do it"
}
if (collectibleCount.length> 10){
textString = "Don't give up, you can do it"
}
if (player.velocity>0){
textString = "That's the wrong way"
}
}
function cameraMovement() {
if (mouseX > width / 2 - width / 4 && mouseX < width) {
if (cameraRotationX < 200) {
cameraRotationX = cameraRotationX + 2
}
}
if (mouseX < width / 2 + width / 4 && mouseX > 0) {
if (cameraRotationX > -200) {
cameraRotationX = cameraRotationX - 2
}
}
if (mouseY > height / 2 - height / 4 && mouseY < height) {
if (cameraRotationY < 200) {
cameraRotationY = cameraRotationY + 2
}
}
if (mouseY < height / 2 + height / 4 && mouseY > 0) {
if (cameraRotationY > -200) {
cameraRotationY = cameraRotationY - 2
}
}
}
function uI() {
push();
translate(player.position.y - 150, player.y - 80, player.position.x + 75);
collectibleIcon.update();
collectibleIcon.y = 0
pop();
push();
translate(player.position.y - 100, player.y - 50, player.position.x + 75);
rotateX(25)
textFont(oswald);
textSize(75);
fill(0, 0, 100, 0.75);
text(collectibleCount.length, 0, 0);
textAlign(CENTER, CENTER);
pop();
push();
translate(player.position.y - 100, player.y + 15, player.position.x + 75);
rotateX(25)
textFont(oswald);
textSize(35);
fill(0, 0, 100, 0.75);
text(experience.length, 0, 0);
textAlign(CENTER, CENTER);
pop();
push();
translate(player.position.y - 135, player.y, player.position.x + 75);
rotateX(25)
textFont(oswald);
textSize(35);
ambientMaterial(180, 50, 100);
sphere(15)
textAlign(CENTER, CENTER);
pop();
push();
translate(player.position.y - 160, player.y, player.position.x + 185);
rotateX(25)
textFont(oswald);
textSize(15);
fill(0, 0, 100);
text(textString, 0, 0);
textAlign(CENTER, CENTER);
pop();
push();
translate(player.position.y - 42, player.y - 5, player.position.x + 185);
rotateX(45)
fill(0, 0, 0, 0.5);
box(width * 2, 50, 50)
pop();
}
function generateClouds() {
for (let i = 0; i < clouds.length; i = i + 1) {
clouds[i].update();
}
}
function detectCollision() {
//CHECK POSTION IN RELATION TO OBJECT
//ENEMY COLLISION
for (let i = 0; i < enemies.length; i = i + 1) {
if (player.position.y <= enemies[i].x + enemies[i].w / 1.5 && player.position.y >= enemies[i].x - enemies[i].w / 1.5 && player.position.x <= enemies[i].z + enemies[i].d / 2 && player.position.x >= enemies[i].z - enemies[i].d / 2) {
enemyCollisionSide = true;
collectibleCount.splice(1);
} else {
enemyCollisionSide = false;
}
if (player.y <= enemies[i].y + enemies[i].h / 2 && player.y >= enemies[i].y - enemies[i].h / 2) {
enemyCollisionHeight = true;
} else {
enemyCollisionHeight = false;
}
//IF CHECKS ARE TRUE
if (enemyCollisionSide == true && enemyCollisionHeight == true) {
player.velocity = 0;
collisionBounce = true;
}
if (enemyCollisionSide == true && player.y <= enemies[i].y - enemies[i].h) {
enemyCollisionOnTop = true;
enemyDeathAnimation = true;
experience.push(1);
} else {
enemyCollisionOnTop = false;
enemyDeathAnimation = false;
}
if (enemyDeathAnimation === true) {
enemies.splice(i, 1)
enemyDeathSound.play();
}
}
//Platorms
if (platformCollisionOnTop == true) {
player.ground = enemies[i].y - enemies[i].h - 5;
}
if (platformCollisionOnTop == false) {
player.ground = 0;
}
//COLLECTIBLE COLLISION
for (let i = 0; i < collectibles.length; i = i + 1) {
if (player.position.y <= collectibles[i].x + collectibles[i].w * 1.5 && player.position.y >= collectibles[i].x - collectibles[i].w * 1.5 && player.position.x <= collectibles[i].z + collectibles[i].d * 1.5 && player.position.x >= collectibles[i].z - collectibles[i].d * 1.5 && player.y <= collectibles[i].y + collectibles[i].h * 1.5 && player.y >= collectibles[i].y - collectibles[i].h * 1.5) {
collectibleCollision = true;
} else {
collectibleCollision = false;
}
if (collectibleCollision === true) {
collectibles.splice(i, 1)
collectibleCount.push(1)
player.rX = player.rX + 1
collectibleSound.play();
}
}
//BOUNDARY COLLISION
}
function tracker() {
if (counter == 10) {
clearInterval(jumpInterval);
counter = 0;
spacePressed = false;
}
if (spacePressed === false) {
player.upwardVelocity = 0;
player.downwardVelocity = 0;
player.rX = 0;
}
}
function mouseClicked() {
cameraRotationX = 0
mouseX = width / 2
cameraRotationY = 0
mouseY = height / 2
}
function jump() {
if (spacePressed === true) {
if (counter < 3) {
if (spaceHistory.length % 3 == 0) {
player.rX = player.rX + 10
}
if (player.y <= player.ground && player.y >= -350) {
player.upwardVelocity = player.upwardVelocity + player.upwardAcceleration
player.y = player.y - player.upwardVelocity
}
}
if (counter > 3) {
if (player.y < player.ground) {
player.downwardVelocity = player.downwardVelocity + player.downwardAcceleration
player.y = player.y + player.downwardVelocity
}
}
}
if (player.y > player.ground) {
player.y = player.ground
}
}
function timeInput() {
counter++;
}
function manageInput() {
if (keyIsDown(68)) {
if (player.rY > -180) {
player.rY = player.rY - player.rotationSpeed;
}
}
if (keyIsDown(65)) {
if (player.rY < 180) {
player.rY = player.rY + player.rotationSpeed;
}
}
if (keyIsDown(87) && moveBackward === false) {
moveForward = true;
if (player.velocity > -10) {
player.velocity = player.velocity - player.acceleration;
}
} else {
moveForward = false
}
if (keyIsDown(83) && moveForward === false) {
moveBackward = true;
if (player.velocity < 10) {
player.velocity = player.velocity + player.acceleration;
}
} else {
moveBackward = false
}
if (moveBackward === false && moveForward === false) {
if (player.velocity > 0) {
player.velocity = player.velocity - player.acceleration;
}
if (player.velocity < 0) {
player.velocity = player.velocity + player.acceleration;
}
}
}
function keyPressed() {
if (keyCode === 16) {
if (enemyCollisionSide === true) {
enemyCollisionSide = false
}
}
if (spacePressed === false) {
if (keyCode === 32) {
jumpInterval = setInterval(timeInput, intervalSpeed);
spacePressed = true;
spaceHistory.push(1);
// if(counter < 5){player.y = player.y - 100}
}
}
if (keyCode === 80) {
if (paused === false) {
noLoop();
paused = true;
} else {
paused = false
}
}
if (keyCode === ENTER) {
if (paused === true) {
loop();
paused = false;
}
}
}
class Game {
constructor() {}
update() {}
}
class Player {
constructor(x, y, z, w, h, d, velocity, acceleration, upwardAcceleration, rotationSpeed) {
this.position = createVector(this.x, this.z)
this.direction = 0;
this.x = x
this.y = y
this.z = z
this.w = w
this.h = h
this.d = d
this.acceleration = acceleration;
this.upwardAcceleration = upwardAcceleration
this.rotationSpeed = rotationSpeed
this.velocity = 0;
this.upwardVelocity = 0
this.downwardVelocity = 0
this.downwardAcceleration = upwardAcceleration / 2
this.rX = 0
this.rY = 0
this.rZ = 0
this.ground = 0
}
update() {
this.direction = p5.Vector.fromAngle(radians(this.rY));
this.direction.setMag(this.velocity);
this.position.add(this.direction);
push();
translate(this.position.y, this.y, this.position.x);
rotateX(this.rX);
rotateY(this.rY);
rotateZ(this.rZ);
ambientMaterial(180, 50, 100);
box(this.w, this.h, this.d);
pop();
}
}
class Ground {
constructor(x, y, z, w, h) {
this.x = x
this.y = y
this.z = z
this.w = w
this.h = h
this.rX = 90
}
update() {
push();
translate(this.x, this.y, this.z);
rotateX(this.rX);
ambientMaterial(90, 65, 85);
plane(this.w, this.h);
pop();
}
}
class Enemy {
constructor(x, y, z, scale) {
this.x = x
this.y = y
this.z = z
this.w = 75
this.h = 75
this.d = 75
this.scale = scale
}
update() {
push();
scale(this.scale)
translate(this.x, this.y, this.z);
ambientMaterial(360, 100, 100);
box(this.w, this.h, this.d);
pop();
}
}
class Wall {
constructor() {
this.x = 0
this.y = 0
this.z = 5000
this.w = 500
this.h = 5000
this.d = 500
}
update() {
push();
translate(this.x, this.y, -this.z);
ambientMaterial(0, 0, 100, 0.25);
box(this.w * 40, this.h, this.d);
pop();
push();
translate(this.x - 5000, this.y, 0);
ambientMaterial(0, 0, 100, 0.25);
box(this.w, this.h, this.d * 40);
pop();
push();
translate(this.x + 5000, this.y, 0);
ambientMaterial(0, 0, 100, 0.25);
box(this.w, this.h, this.d * 40);
pop();
push();
translate(this.x, this.y, this.z);
ambientMaterial(0, 0, 100, 0.25);
box(this.w * 40, this.h, this.d);
pop();
}
}
class Platform {
constructor() {
this.y = 0
}
update() {
push()
push();
translate(500, this.y - 50, -750);
ambientMaterial(90, 65, 85);
box(1000, 50, 1000);
pop();
push();
translate(500, this.y, -750);
ambientMaterial(45, 100, 55);
box(950, 100, 950);
pop();
pop();
}
}
class House {
constructor() {
this.y = 0
}
update() {
push()
push();
translate(500, this.y - 50, -750);
ambientMaterial(90, 65, 85);
box(1000, 50, 1000);
pop();
push();
translate(500, this.y, -750);
ambientMaterial(45, 100, 55);
box(950, 100, 950);
pop();
pop();
}
}
class Tree {
constructor() {
this.y = 0
}
update() {
push()
push();
translate(500, this.y - 50, -750);
ambientMaterial(90, 65, 85);
box(1000, 50, 1000);
pop();
push();
translate(500, this.y, -750);
ambientMaterial(45, 100, 55);
box(950, 100, 950);
pop();
pop();
}
}
class Cloud {
constructor(x, z, y) {
this.x = x
this.y = y
this.z = z
}
update() {
push()
// scale(random(1,2))
push();
translate(this.x, this.y, this.z);
ambientMaterial(0, 0, 100);
box(50, 50, 50);
pop();
push();
translate(this.x, this.y, this.z);
ambientMaterial(0, 0, 100);
box(200, 50, 200);
pop();
pop();
this.y = wave/5 + this.y
}
}
class Bush {
constructor() {
this.y = 0
}
update() {
push()
push();
translate(500, this.y - 50, -750);
ambientMaterial(90, 65, 85);
box(1000, 50, 1000);
pop();
push();
translate(500, this.y, -750);
ambientMaterial(45, 100, 55);
box(950, 100, 950);
pop();
pop();
}
}
class Explosion {
constructor(x, y, z) {
this.x = x
this.y = y
this.z = z
this.w = 10
this.h = 10
this.d = 10
this.velocity = 0
this.acceleration = random(0.05, 5)
this.scale = random(1, 3)
this.angle = random(-360, 360)
}
update() {
//rotateX(this.angle)
rotateY(this.angle)
//rotateZ(this.angle)
push();
translate(this.x, this.y, this.z);
scale(this.scale)
ambientMaterial(0, 0, 100);
box(this.w, this.h, this.d);
pop();
//this.velocity = this.velocity * random(1,-1)
// this.angle = this.angle + 1;
this.x = this.x + this.velocity;
//this.y = this.y + this.velocity;
//this.z = this.z + this.velocity;
this.velocity = this.velocity + this.acceleration;
}
}
class Collectible {
constructor(x, y, z) {
this.x = x
this.y = y
this.z = z
this.w = 25
this.h = 25
this.d = 25
this.velocity = 1
this.angle = random(-360, 360)
}
update() {
push();
stroke(0, 0, 100)
translate(this.x, this.y, this.z);
rotateY(this.angle)
ambientMaterial(0, 0, 100);
box(this.w, this.h, this.d);
push();
rotateX(this.angle)
ambientMaterial(0, 0, 100, 0);
box(this.w * 2, this.h * 2, this.d * 2);
pop();
pop();
this.angle = this.angle + 1;
this.y = wave;
}
}
//Copyright @ 2019 KeenanDailey All Rights Reserved