xxxxxxxxxx
305
/*
11.18.2024
By Zen Sabio
*/
let player = Player();
let enemy1;
let collisionRadius = 15;
let collisionRadius2 = 0;
let score = 0;
let hp = 100;
let gameState = 2;
let playerIdleS, playerWalkS, playerWalkleftS, playerAttackS;
let enemyIdleS, enemyWalkS, enemyWalkleftS, enemyAttackS, enemyDeath;
let titleScreen, playButtonImg, instrButtonImg, backgroundGame, controlsImg;
let titlelogo;
let backGroundmusic, attackSound;
function preload() {
playerIdleS = loadImage("sprites/animation_idle_move.gif");
playerWalkS = loadImage("sprites/animation_walk_test_2.gif");
playerWalkleftS = loadImage("sprites/animation_walk_test_left.gif");
playerAttackS = loadImage("sprites/attack_animation _Blue2.gif");
enemyIdleS = loadImage("sprites/animation_idle_red.gif");
enemyWalkS = loadImage("sprites/animation_walk_red.gif");
enemyWalkleftS = loadImage("sprites/animation_walk_red_left.gif");
enemyAttackS = loadImage("sprites/red_attack_animation.gif");
enemyDying = loadImage("sprites/animation_red_death.gif");
backGroundmusic = loadSound("audio/gameAudio.mp3");
attackSound = loadSound("audio/Hit_hurt 3.mp3");
titleScreen = loadImage("ui/title.png");
playButtonImg = loadImage("ui/playbtn.png");
instrButtonImg = loadImage("ui/controlsbtn.png");
backgroundGame = loadImage("sprites/background.gif");
controlsImg = loadImage("sprites/mmp210-controlscrn.gif");
logoImg = loadImage("sprites/tilelogo.gif");
}
function setup() {
createCanvas(600, 400);
imageMode(CENTER);
enemy1 = Enemy();
backGroundmusic.loop(true);
startButton = Button(75, 370, playButtonImg);
controlsButton = Button(533, 370, instrButtonImg);
}
function draw() {
background(220);
if (gameState == 0) {
startMenu();
} else if (gameState == 1) {
instructionMenu();
} else if (gameState == 2) {
game();
}
}
function startMenu() {
image(titleScreen, width / 2, height / 2);
image(logoImg, width / 2, height - 100);
startButton.display();
controlsButton.display();
if (startButton.clicked()) {
gameState = 2;
}
if (controlsButton.clicked()) {
gameState = 1;
}
}
function instructionMenu() {
image(controlsImg, width / 2, height / 1.8);
startButton.display();
controlsButton.display();
if (startButton.clicked()) {
gameState = 2;
}
if (controlsButton.clicked()) {
gameState = 1;
}
}
function Button(x, y, img) {
let h = 400;
let w = 400;
let isClicked = false;
function display() {
image(img, x, y);
}
function clicked() {
if (mouseIsPressed) {
if (
mouseX > x - w / 2 &&
mouseX < x + w / 2 &&
mouseY > y - h / 2 &&
mouseY < y + h / 2
) {
if (!isClicked) {
isClicked = true;
return true;
}
}
} else if (isClicked) {
isClicked = false;
}
return false;
}
return { display, clicked };
}
function game() {
image(backgroundGame, width / 2, height / 4);
player.move();
player.display();
enemy1.move();
enemy1.display();
let enemy1Collide = detectCollision(player, enemy1);
if (enemy1Collide) {
enemy1.attack();
if (!attackSound.isPlaying()) {
attackSound.play();
}
}
let playerCollide = detectCollisionSword(player, enemy1);
//console.log(playerCollide);
if (playerCollide) {
enemy1.death();
score = score + 1
}
textSize(55);
text(score, 20, 20);
console.log(score)
}
function Player() {
//pos
let x = 195;
let y = 239;
//animation
let animationState = 0;
// 0 = idle
// 1 = walk
// 2 = walk left
// 3= attack
function move() {
animationState = 0;
if (keyIsDown(LEFT_ARROW)) {
x = x - 5;
animationState = 2;
}
if (keyIsDown(RIGHT_ARROW)) {
x = x + 5;
animationState = 1;
}
if (keyIsDown(32)) {
animationState = 3;
collisionRadius2 = 50;
setTimeout(reset, 300);
if (!attackSound.isPlaying()) {
attackSound.play();
}
}
}
function display() {
//image(playerIdleS, x, y);
if (animationState == 0) {
image(playerIdleS, x, y);
} else if (animationState == 1) {
image(playerWalkS, x, y);
} else if (animationState == 2) {
image(playerWalkleftS, x, y);
} else if (animationState == 3) {
image(playerAttackS, x, y);
}
//debug collison shape
noFill();
circle(x, y, collisionRadius * 2);
circle(x, y, collisionRadius2 * 2);
}
function getPosition() {
return { x, y };
}
function reset() {
collisionRadius2 = 0;
}
return { move, display, getPosition };
}
function Enemy() {
let x = random(0, 200);
let y = 239;
let animationState = 0;
function move() {
if (animationState != 3) {
x = x + 2;
animationState = 1;
}
if (x > width) {
x = 0;
}
}
function display() {
if (animationState == 0) {
image(enemyWalkS, x, y);
} else if (animationState == 1) {
image(enemyWalkS, x, y);
} else if (animationState == 2) {
image(enemyWalkleftS, x, y);
} else if (animationState == 3) {
image(enemyAttackS, x, y);
} else if (animationState == 4) {
image(enemyDying, x, y);
}
}
function getPosition() {
return { x, y };
}
function death() {
animationState = 4;
}
function attack() {
animationState = 3;
setTimeout(reset, 300);
function reset() {
animationState = 1;
}
}
return { move, display, getPosition, attack, death };
}
function detectCollision(objPL, objE) {
let positionA = objPL.getPosition();
let positionB = objE.getPosition();
let d = dist(positionA.x, positionA.y, positionB.x, positionB.y);
let isColliding = d < collisionRadius;
return isColliding;
}
function detectCollisionSword(objPL, objE) {
let positionA = objPL.getPosition();
let positionB = objE.getPosition();
let d = dist(positionA.x, positionA.y, positionB.x, positionB.y);
let isColliding = d < collisionRadius2;
return isColliding;
}