arena/src/game/fighterFactory.js

74 lines
2.1 KiB
JavaScript

import Phaser from "phaser";
import { FIGHTER_SCALE } from "../constants.js";
import { fighterAnimationKey, fighterSheetKey } from "./fighterAssets.js";
export function createFighter(scene, { faceLeft, name, skin, team, teamIndex, x, y }) {
const fighter = scene.physics.add.sprite(x, y, fighterSheetKey(skin, "idle"), 0);
fighter.setScale(FIGHTER_SCALE);
fighter.setDepth(2);
fighter.setCollideWorldBounds(true);
fighter.setFlipX(faceLeft);
fighter.body.setSize(22, 20);
fighter.body.setOffset(39, 60);
fighter.nameLabel = scene.add
.text(x, y - 68, name, {
color: "#fff2c2",
fontFamily: "Inter, Pretendard, sans-serif",
fontSize: "18px",
fontStyle: "700",
stroke: team.color,
strokeThickness: 4,
})
.setOrigin(0.5)
.setDepth(4);
fighter.healthBack = scene.add
.rectangle(x, y - 44, 72, 8, 0x17180e, 0.92)
.setDepth(4);
fighter.healthBar = scene.add
.rectangle(x - 34, y - 44, 68, 4, 0xd95f3f, 1)
.setOrigin(0, 0.5)
.setDepth(5);
fighter.skin = skin;
fighter.team = team;
fighter.teamIndex = teamIndex;
fighter.hp = 100;
fighter.nextAttackAt = 0;
fighter.isLocked = false;
fighter.isDead = false;
fighter.play(fighterAnimationKey(skin, "walk"));
fighter.on(Phaser.Animations.Events.ANIMATION_COMPLETE, (animation) => {
if (fighter.isDead) {
return;
}
if (animation.key.includes("-attack") || animation.key.endsWith("-hurt-anim")) {
fighter.isLocked = false;
}
});
attachHudCleanup(fighter);
return fighter;
}
export function syncFighterHud(fighter) {
fighter.nameLabel.setPosition(fighter.x, fighter.y - 68);
fighter.healthBack.setPosition(fighter.x, fighter.y - 44);
fighter.healthBar.setPosition(fighter.x - 34, fighter.y - 44);
fighter.healthBar.width = Math.max(0, 68 * (fighter.hp / 100));
}
function attachHudCleanup(fighter) {
const originalDestroy = fighter.destroy.bind(fighter);
fighter.destroy = (...args) => {
fighter.nameLabel.destroy();
fighter.healthBack.destroy();
fighter.healthBar.destroy();
originalDestroy(...args);
};
}