From 668df62d37a8a686cc2dc0554cf25337d0027514 Mon Sep 17 00:00:00 2001 From: Charlize Date: Sat, 16 Mar 2024 03:27:05 -0700 Subject: [PATCH] Cleaning up Comments and Adding Stats --- index.html | 1 + src/main.js | 2 +- src/prefabs/Character.js | 21 ++-- src/prefabs/Enemy.js | 23 ++-- src/scenes/BossFighting.js | 218 +++++++++++++++++++++++++++++++++++++ src/scenes/Fighting.js | 13 +-- 6 files changed, 248 insertions(+), 30 deletions(-) create mode 100644 src/scenes/BossFighting.js diff --git a/index.html b/index.html index 1470644..0ebf7f2 100644 --- a/index.html +++ b/index.html @@ -16,6 +16,7 @@ + diff --git a/src/main.js b/src/main.js index a7b94c8..e7c5724 100644 --- a/src/main.js +++ b/src/main.js @@ -50,7 +50,7 @@ let config = { // } } }, - scene: [ Menu, Tutorial, Fighting, Credits] + scene: [ Menu, Tutorial, Fighting, BossFighting, Credits] } diff --git a/src/prefabs/Character.js b/src/prefabs/Character.js index 01b7038..5077212 100644 --- a/src/prefabs/Character.js +++ b/src/prefabs/Character.js @@ -7,7 +7,6 @@ class Character extends Phaser.Physics.Arcade.Sprite { this.body.setImmovable(true) this.x = x this.y = y - // console.log('the y value is : ' + this.y) // setting character properties this.index = index this.health = health @@ -23,8 +22,7 @@ class Character extends Phaser.Physics.Arcade.Sprite { this.collapsed = false // creating an array of attacks - // might make a dictionary - this.attackList = Array(2).fill(-1) + this.attackList = Array(2).fill(-1) // Note: might make a dictionary // debugging this.state = 'idle' @@ -87,11 +85,15 @@ class AttackState extends State { // character will play a temporary attack animation where they throw their character specific attack enter (scene, character) { // remove the enemies health - // scene.enemy.damaged = true + console.log(scene.selectionMenu.current_attack) + if (scene.selectionMenu.current_attack == 1){ + // if you have selected a magic attack then play the magic attack + character.mana -= 10 + scene.characters_mp[character.index].match(character.mana) + + } character.state = 'attack' - character.mana -= 10 - // change mana amt later - scene.characters_mp[character.index].match(character.mana) + scene.dmgToEnemy = character.attack_dmg @@ -132,6 +134,7 @@ class HurtState extends State { this.attackText_below = scene.add.bitmapText(centerX, centerY+1, 'font', `${character.name} takes ${-scene.enemy.dmgToPlayer} damage`, 12).setOrigin(0.5).setTint(0x1a1200) this.attackText = scene.add.bitmapText(centerX, centerY, 'font', `${character.name} takes ${-scene.enemy.dmgToPlayer} damage`, 12).setOrigin(0.5) + this.ready = false if (character.health > 0){ scene.time.delayedCall(character.hurtTimer, () => { character.clearTint() @@ -144,6 +147,7 @@ class HurtState extends State { console.log(`${character.name} has reached idle`) this.stateMachine.transition('idle') } + this.ready = true // character.checkCollision.none = true character.body.enable = false }) @@ -161,6 +165,9 @@ class HurtState extends State { this.stateMachine.transition('collapse') }) } + if(this.ready == true){ + this.stateMachine.transition('idle') + } } } diff --git a/src/prefabs/Enemy.js b/src/prefabs/Enemy.js index 95bc213..0768dbc 100644 --- a/src/prefabs/Enemy.js +++ b/src/prefabs/Enemy.js @@ -8,18 +8,17 @@ class Enemy extends Phaser.Physics.Arcade.Sprite { this.health = health this.name = name this.damagedTimer = temp_timer - // set up an attack - this.attacking = false - this.dmgToPlayer = 0 + this.dmgToPlayer = 0 // init to allow the dmgToPlayer to change this.attack_dmg = power - // variable to check + this.startY = y + // boolean values to check + this.attacking = false this.hasAttacked = false // setting up the character to attack // this.selectedChar = -1 this.projectile = new Projectile(scene, this.x + this.width/2, this.y - this.height * 1.5, `${this.name}_projectile`, this) - this.startY = y // setting up state machines scene.enemyFSM = new StateMachine('default', { @@ -56,11 +55,7 @@ class DefaultState extends State { // console.log(`${enemy.name} (boss) defaulting, damage = ${enemy.dmgToPlayer}`) } execute(scene, enemy) { - const { left, right, up, down, space, shift } = scene.keys - // if the enemy's y is not at the original location move it back - - // Note: might make a brand new state - + const { left, right, up, down, space, shift } = scene.keys // if it is not the player's turn and there exists no currently attacking player AND there exists no summon if(scene.player_turn == false && enemy.hasAttacked == false && !scene.selectionMenu.attackingPlayer && !scene.selectionMenu.summonSelect){ console.log("ENEMY ENTERING ATTACK") @@ -68,10 +63,11 @@ class DefaultState extends State { this.stateMachine.transition('single_attack') } - // save the used projectile from the input from selectionmenu + // if a character has been selected to attack if (scene.selectionMenu.attackingPlayer){ - // console.log(scene.selectionMenu.attackingPlayer.projectile.x) + // add a collider to collide with the incoming player projectile scene.physics.add.collider(scene.selectionMenu.attackingPlayer.projectile, enemy, () => { + // create a collision to transition into damaged let collision = scene.selectionMenu.attackingPlayer.projectile.handleCollision(enemy, scene.dmgToEnemy) if ( collision == true){ scene.selectionMenu.attackingPlayer.projectile.resetProj(scene.selectionMenu.attackingPlayer.projectile.startX, scene.selectionMenu.attackingPlayer.projectile.startY) @@ -134,9 +130,6 @@ class SingleAttackState extends State { else{ if (enemy.y >= enemy.startY && enemy.hasAttacked == true){ enemy.body.setVelocity(0) - } - if (scene.characters[enemy.selectedChar].hurt == true && enemy.y >= enemy.startY) { - //selection menu scene.selectionMenu.allowSelect = true scene.changeTurn() console.log('player turn is ' + scene.player_turn) diff --git a/src/scenes/BossFighting.js b/src/scenes/BossFighting.js new file mode 100644 index 0000000..8df5401 --- /dev/null +++ b/src/scenes/BossFighting.js @@ -0,0 +1,218 @@ +class BossFighting extends Phaser.Scene { + constructor(){ + super('bossfightingScene') + } + + init() { + this.hp = HP + this.mp = MP + // boolean to check if it is the player's turn (start off with the player going first) + this.player_turn = true + + this.active_players = 3 // Note: with more scenes turn this into a global variable + this.active_enemies = 1 // Note: in future scenes it may change the amount + this.FSM_holder = Array(3).fill(0) + this.textAdded = false + // initializing the amt the player will do to the enemy + this.dmgToEnemy = 0 + this.enemy_dmg = 0 + + this.enemyX = leftPos - tileSize + this.enemyY = floorY + tileSize + + this.music_playing = false + + // setting up buttons + // see: https://blog.ourcade.co/posts/2020/phaser-3-ui-menu-selection-cursor-selector/ + + } + + create() { + // initializing a background + // see: https://www.youtube.com/watch?v=OOo69t_-uok + this.background = this.add.image(this.scale.width / 2,this.scale.height / 2 - tileSize * 1.55, 'background') + // adding music + this.music = this.sound.add('fight_music').setLoop(true).setVolume(0.4) + + // adding a character to scene - each character should have their own HP + this.gumball = new Character(this, rightPos-tileSize, floorY + tileSize /1.5, 'gumball', 0, this.hp, MP, 30, 'GUMBALL', 'MAGIC', 'physical', 0).setOrigin(0,1) + this.gumball.attackList = [ 'SCRATCH', 'MAGIC'] + + this.anais = new Character(this, rightPos, floorY +tileSize / 1.5, 'anais', 0, this.hp, MP, 50, 'ANAIS', 'SCIENCE', 'mage', 1).setOrigin(0,1) + this.anais.attackList = [ 'PUNCH', 'SCIENCE' ] + + this.darwin = new Character(this, rightPos + tileSize, floorY + tileSize / 1.5, 'darwin', 0, this.hp, MP, 10, 'DARWIN', 'SUPPORT', 'mage', 2).setOrigin(0,1) + this.darwin.attackList = [ 'SLAP', 'SUPPORT' ] + // adding each character health + this.gumball_hp = new HealthBar(this, centerX, floorY + tileSize, this.gumball, 0) + this.gumball_mp = new ManaBar(this, centerX + tileSize * 3 + 12, floorY + tileSize, this.gumball, 0) + + this.anais_hp = new HealthBar(this, centerX,floorY+ tileSize *1.5, this.anais, 0) + this.anais_mp = new ManaBar(this, centerX + tileSize * 3 + 12, floorY+ tileSize *1.5, this.anais, 1) + + this.darwin_hp = new HealthBar(this, centerX, floorY + tileSize * 2, this.darwin, 2) + this.darwin_mp = new ManaBar(this, centerX + tileSize * 3 + 12, floorY + tileSize * 2, this.darwin, 1) + + // adding all characters into an array to loop all the characters + this.characters = [ this.gumball, this.anais, this.darwin ] + this.characters_hp = [ this.gumball_hp, this.anais_hp, this.darwin_hp ] + this.characters_mp = [ this.gumball_mp, this.anais_mp, this.darwin_mp ] + + // adding enemy to scene - enemy has their own prefab + this.enemy = new Enemy(this, leftPos - tileSize, floorY + tileSize / 1.5, 'penny', 0, HP, MP, 152, 'PENNY').setOrigin(0,1).setFlipX(true) + this.enemy_hp = new HealthBar(this, centerX, tileSize / 4, this.enemy) + // enemy does not need mana + + this.summon = new Summon(this, rightPos - tileSize * 3, game.config.height + 100, 'nicole', 200).setOrigin(0,1) + + + // setting up keyboard inputs + this.keys = this.input.keyboard.createCursorKeys() + + // this.selectionMenu = new SelectionMenu(this, game.config.width - tileSize - 5, floorY + tileSize + 25, this.characters) + this.selectionMenu = new SelectionMenu(this, game.config.width - tileSize - 5, tileSize + 25, this.characters) + + // Game OVER flag + this.gameOver = false + + } + + update() { + const { left, right, up, down, space, shift } = this.keys + // check for game over + if (this.active_players == 0 || this.active_enemies == 0){ + this.gameOver = true + } + // restart game if game over + if (this.gameOver){ + // check gameover condition + if (this.active_enemies == 0){ + if (!this.textAdded){ + this.add.bitmapText(centerX, centerY, 'font', 'YOU WIN', 20).setOrigin(0.5) + this.add.bitmapText(centerX, centerY - tileSize, 'font', 'up for menu right to restart', 8).setOrigin(0.5) + this.textAdded = true + + } + } + if (this.active_players == 0){ + // use boolean value to ensure that browser does not lag + if (!this.textAdded){ + this.add.bitmapText(centerX, centerY, 'font', 'GAME OVER', 20).setOrigin(0.5) + this.add.bitmapText(centerX, centerY - tileSize, 'font', 'up for menu right to restart', 8).setOrigin(0.5) + + this.textAdded = true + } + } + if (up.isDown){ + this.music.stop() + this.scene.start('menuScene') + } + if (right.isDown){ + this.music.stop() + this.scene.restart() + } + } + if (!this.gameOver){ + // for debugging + // console.log("enemy attacked=" + this.enemy.hasAttacked +', ' + this.characters[0].name + this.characters[0].state + ' , ' + this.characters[1].name + this.characters[1].state + ' , ' + this.characters[2].name + this.characters[2].state) + + if (!this.music_playing){ + this.music.play() + this.music_playing = true + } + this.FSM_holder[0].step() + this.FSM_holder[1].step() + this.FSM_holder[2].step() + this.enemyFSM.step() + + if (Phaser.Input.Keyboard.JustDown(space)){ + this.selectionMenu.select() + } + if (this.selectionMenu.current_selection == 0){ + if (Phaser.Input.Keyboard.JustDown(right)){ + this.selectionMenu.attackChange(1) + } + if (Phaser.Input.Keyboard.JustDown(left)){ + this.selectionMenu.attackChange(-1) + } + } + + if (this.selectionMenu.current_selection == 2){ + if (Phaser.Input.Keyboard.JustDown(right)){ + this.selectionMenu.charChange(1) + } + if (Phaser.Input.Keyboard.JustDown(left)){ + this.selectionMenu.charChange(-1) + } + } + + if (Phaser.Input.Keyboard.JustDown(up)){ + this.selectionMenu.lookChoice(1) + } + if (Phaser.Input.Keyboard.JustDown(down)){ + this.selectionMenu.lookChoice(-1) + } + } + } + checkActive(){ + let availableChar = Array(0); + for (let i = 0; i < this.characters.length; i++) { + if (!this.characters[i].hasAttacked && !this.characters[i].collapsed){ + // if character not collapsed nor attacked put into array + availableChar.push(this.characters[i].index) + } + } + // if availableChar's length == 0 then end turn + if (availableChar.length == 0){ + this.changeTurn() + // reset the move amount + availableChar = this.resetAttacks() + } + return availableChar + } + + resetAttacks() { + let availableCharacters = Array(0) + for (let i = 0; i < this.characters.length; i++) { + // if this character has not attacked + if (!this.characters[i].collapsed){ + this.characters[i].hasAttacked = false + availableCharacters.push(this.characters[i].index) + } + } + return availableCharacters + } + + changeTurn(){ + // when turn is changed reset the count of how many times character can be used + if (this.player_turn == false){ + this.selectionMenu.moves = 3 + + this.player_turn = true + this.selectionMenu.setVisibility(true) + // this.selectionMenu.allowSelect = true + } + else if (this.player_turn == true){ + this.player_turn = false + this.selectionMenu.setVisibility(false) + // this.selectionMenu.allowSelect = true + } + + } + + checkLiving(){ + // function to update living characters + let livingCharacters = Array(3).fill(-1); + + for (let i = 0; i < this.characters.length; i++) { + if (!this.characters[i].collapsed){ + // if character not collapsed put into array + livingCharacters[i] = this.characters[i].index + } + } + return livingCharacters + + } + + +} \ No newline at end of file diff --git a/src/scenes/Fighting.js b/src/scenes/Fighting.js index 694b7ad..783dcdd 100644 --- a/src/scenes/Fighting.js +++ b/src/scenes/Fighting.js @@ -114,12 +114,11 @@ class Fighting extends Phaser.Scene { } if (!this.gameOver){ // for debugging - console.log("enemy attacked=" + this.enemy.hasAttacked +', ' + this.characters[0].name + this.characters[0].state + ' , ' + this.characters[1].name + this.characters[1].state + ' , ' + this.characters[2].name + this.characters[2].state) - // console.log(this.characters[1].name + this.characters[1].state + ) - // console.log(this.characters[2].name + this.characters[2].state) - - - + // console.log("enemy attacked=" + this.enemy.hasAttacked +', ' + this.characters[0].name + this.characters[0].state + ' , ' + this.characters[1].name + this.characters[1].state + ' , ' + this.characters[2].name + this.characters[2].state) + // DEBUGGING + if (Phaser.Input.Keyboard.JustDown(shift)){ + this.scene.start('bossfightingScene') + } if (!this.music_playing){ this.music.play() @@ -129,7 +128,7 @@ class Fighting extends Phaser.Scene { this.FSM_holder[1].step() this.FSM_holder[2].step() this.enemyFSM.step() - + if (Phaser.Input.Keyboard.JustDown(space)){ this.selectionMenu.select() }