xxxxxxxxxx
181
/*
11.18.2024
By Zen Sabio
*/
let player = Player();
let enemy1;
let collisionRadius = 15;
let playerIdleS, playerWalkS, playerWalkleftS, playerAttackS;
let enemyIdleS, enemyWalkS, enemyWalkleftS, enemyAttackS;
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");
backGroundmusic = loadSound("audio/gameAudio.mp3")
attackSound = loadSound("audio/Hit_hurt 3.mp3")
}
function setup() {
createCanvas(400, 400);
imageMode(CENTER);
enemy1 = Enemy();
backGroundmusic.loop(true)
}
function draw() {
background(220);
//delay = new p5.Delay (0.15, 0.7);
player.move();
player.display();
enemy1.move();
enemy1.display();
let enemy1Collide = detectCollision(player, enemy1);
if (enemy1Collide) {
enemy1.attack();
if (!attackSound.isPlaying()) {
attackSound.play();
}
}
}
function Player() {
//pos
let x = 195;
let y = 149;
//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;
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);
}
function getPosition() {
return { x, y };
}
return { move, display, getPosition };
}
function Enemy() {
let x = random(0, 200);
let y = 149;
let animationState = 0;
function move() {
if (animationState != 3) {
x = x + 1;
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);
}
}
function getPosition() {
return { x, y };
}
function attack() {
animationState = 3;
setTimeout(reset, 300);
}
function reset() {
animationState = 1;
}
return { move, display, getPosition, attack };
}
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;
}