import { Vector } from "../engine/vector.js" import { Engine } from "../engine.js" import { Entity } from "./entity.js" import { HELLO_KITTY } from "../assets.js" import { setEnemy, boshy } from "../main.js" import { Bullet } from "./hello_kitty/bullet.js" import { Laser } from "./hello_kitty/laser.js" export class HelloKitty extends Entity { max_hp = 300 hp = this.max_hp moveInterval = 26 previous = 0 stage = 1 justGotHit = false turningRight = false bulletMarginY = 40 constructor() { super({ size: new Vector(200, 200), hitbox: new Vector(100, 200), position: new Vector(325, 140), sprite: HELLO_KITTY.MAIN[0], pixelated: true, }) this.span.style.zIndex = 1 setEnemy(this) } update() { switch (this.stage) { case 0: this.stage0() case 1: this.stage1(); break case 2: this.stage2(); break } if (Engine.collision.collidingWithBoshy(this.position, this.hitbox)) { boshy.die() } } delayBetweenFrames = 10 timeUntilNextFrame = this.delayBetweenFrames frameAlternator = false draw() { /* hit effect */ if (this.justGotHit) { this.span.style.filter = "brightness(500%)" this.justGotHit = false } else { this.span.style.filter = "brightness(100%)" } if (this.speed > 0) this.span.style.transform = "scaleX(-1)" else if (this.speed < 0) this.span.style.transform = "scaleX(1)" /* frame */ /* this is what it should be doing */ // this.span.src = (Math.floor(Engine.frame / 10 % 2) == 0) ? // HELLO_KITTY.MAIN[0] : // HELLO_KITTY.MAIN[1] /* but no hi ha pressupost for it */ /* here's the alternative: */ if (!this.timeUntilNextFrame) { this.timeUntilNextFrame = this.delayBetweenFrames this.span.src = this.frameAlternator ? HELLO_KITTY.MAIN[0] : HELLO_KITTY.MAIN[1] this.frameAlternator = !this.frameAlternator } this.timeUntilNextFrame-- /* draw */ super.draw() } attacksUntilNextStage = 6 normalAttackDelay = 100 attackWaitTime = this.normalAttackDelay stage0() { this.innerStage = 0 this.timeUntilNextInnerStage = 0 this.attackWaitTime = this.normalAttackDelay // this.prepareMoving() this.stage++ } /* single shoots stage */ stage1() { this.move() if (!this.attackWaitTime) { if (!this.attacksUntilNextStage) { this.stage++ this.attacksUntilNextStage = 4 } else { this.normalAttack() this.attackWaitTime = this.normalAttackDelay this.attacksUntilNextStage-- } } this.attackWaitTime-- } innerStage = 0 timeUntilNextInnerStage = 0 /* laser stage */ stage2() { switch (this.innerStage) { case 0: /*(preparation)*/ this.laserAttackDelay = 120 this.attackWaitTime = this.laserAttackDelay + 100 this.timeUntilNextInnerStage = 120 this.innerStage++ case 1: /*(chasing player)*/ this.moveToPlayer() this.timeUntilNextInnerStage-- if (!this.timeUntilNextInnerStage) { this.timeUntilNextInnerStage = 100 this.innerStage++ this.laserAttack(true) } return case 2: /*(first laser shot, waiting)*/ this.timeUntilNextInnerStage-- if (!this.timeUntilNextInnerStage) { this.prepareMoving() this.innerStage++ } return } /* post-preparation behaviour */ this.move() if (!this.attackWaitTime) { if (!this.attacksUntilNextStage) { this.stage = 0 this.attacksUntilNextStage = 6 } else { this.laserAttack() this.attackWaitTime = this.laserAttackDelay this.laserAttackDelay -= 20 this.attacksUntilNextStage-- } } this.attackWaitTime-- } speed = 9 acceleration = 0.2 prepareMoving() { this.position.x = Engine.screen.viewport.x / 2 this.speed = 9 } move() { this.position.x += this.speed if (this.position.x > 325) this.speed -= this.acceleration else this.speed += this.acceleration } moveToPlayer() { let distance = Math.floor(this.position.x - boshy.position.x) switch (true) { case (distance < 10 && distance > -10): this.speed = 0; break case (distance < -50): this.speed = 10; break case (distance < -20): this.speed = 5; break case (distance < 0): this.speed = 2; break case (distance > 50): this.speed = -10; break case (distance > 20): this.speed = -5; break case (distance > 0): this.speed = -2; break } this.position.x += this.speed } normalAttack() { let angleToPlayer = this.getBulletAngleToPlayer() let bulletCenter = this.position.clone() bulletCenter.y += this.bulletMarginY new Bullet({ position: bulletCenter.clone(), speed: 10, degrees: angleToPlayer + Engine.random(-10, 10), }) Engine.screen.shake() } laserAttack(delayed = false) { new Laser(boshy.position.x, delayed) } getBulletAngleToPlayer() { let v1 = this.position.clone() v1.y += this.bulletMarginY let v2 = boshy.position let diff = Vector.substraction(v2, v1) return Math.floor(diff.toDeg()) } hit() { this.hp-- if (!this.justGotHit) Engine.radio.playSound(HELLO_KITTY.HIT_SOUND) this.justGotHit = true } }