This commit is contained in:
ntr 2018-12-05 19:41:13 +11:00
parent e1acdc6fa7
commit 16c838db8b
6 changed files with 144 additions and 58 deletions

View File

@ -31,35 +31,14 @@ function registerEvents(registry, events) {
registry.set('gameList', gameList); registry.set('gameList', gameList);
} }
events.on('SET_ACTIVE_SKILL', function skillActive(skill) { events.on('SEND_SKILL', function skillActive(gameId, crypId, targetTeamId, skill) {
const activeSkill = registry.get('activeSkill'); const ws = registry.get('ws');
if (activeSkill) { ws.sendGameSkill(gameId, crypId, targetTeamId, skill);
activeSkill.clearTint();
} skill.setTint(0x00ff00);
return setActiveSkill(skill);
}); });
events.on('SEND_ACTIVE_SKILL', function skillActive(cryp) { events.on('SEND_TARGET', function skillActive(gameId, crypId, skillId) {
const activeSkill = registry.get('activeSkill');
const game = registry.get('game');
const ws = registry.get('ws'); const ws = registry.get('ws');
if (activeSkill) { ws.sendGameTarget(gameId, crypId, skillId);
// const friendlyTarget = activeSkill.cryp.account === cryp.account;
// if (!friendlyTarget) {
if (game.phase === 'Skill') {
console.log(cryp.account);
ws.sendGameSkill(game.id, activeSkill.cryp.id, cryp.account, activeSkill.skill.skill);
} else if (game.phase === 'Target') {
ws.sendGameTarget(game.id, cryp.id, activeSkill.skill.id);
}
activeSkill.setTint(0xff0000);
registry.set('activeSkill', null);
// } else if (friendlyTarget && activeSkill.skill.self_targeting) {
// ws.sendGameSkill(game.id, activeSkill.cryp.id, null, activeSkill.skill.skill);
// } else {
// activeSkill.clearTint();
// }
}
}); });
events.on('CRYP_ACTIVE', function crypActiveCb(cryp) { events.on('CRYP_ACTIVE', function crypActiveCb(cryp) {

View File

@ -30,7 +30,7 @@ class Combat extends Phaser.Scene {
create() { create() {
this.registry.events.off('changedata', this.updateData); this.registry.events.off('changedata', this.updateData);
this.registry.events.on('changedata', this.updateData, this); this.registry.events.on('changedata', this.updateData, this);
this.input.keyboard.on('keydown_S', () => { this.input.keyboard.on('keydown_BACKSPACE', () => {
this.endGame(); this.endGame();
}, 0, this); }, 0, this);
this.registry.set('gamePhase', false); this.registry.set('gamePhase', false);
@ -61,7 +61,8 @@ class Combat extends Phaser.Scene {
this.registry.set('game', null); this.registry.set('game', null);
this.scene.get('CombatLog').cleanUp(); this.scene.get('CombatLog').cleanUp();
this.scene.get('CombatCryps').cleanUp(); this.scene.get('CombatCryps').cleanUp();
this.scene.get('CombatSkills').cleanUp();
// this.scene.manager.remove('CombatSkills');
this.scene.remove(); this.scene.remove();
return true; return true;
} }

View File

@ -4,6 +4,7 @@ const { TEXT, POSITIONS: { COMBAT } } = require('./constants');
const CRYP_KEY_MAP = ['keydown_ONE', 'keydown_TWO', 'keydown_THREE']; const CRYP_KEY_MAP = ['keydown_ONE', 'keydown_TWO', 'keydown_THREE'];
const SKILL_KEY_MAP = ['keydown_Q', 'keydown_W', 'keydown_E', 'keydown_R']; const SKILL_KEY_MAP = ['keydown_Q', 'keydown_W', 'keydown_E', 'keydown_R'];
const TARGET_KEY_MAP = ['keydown_SEVEN', 'keydown_EIGHT', 'keydown_NINE', 'keydown_ZERO'];
const calcMargin = () => { const calcMargin = () => {
const CRYP_MARGIN = COMBAT.height() / 5; const CRYP_MARGIN = COMBAT.height() / 5;
@ -36,12 +37,28 @@ class CrypSkill extends Phaser.GameObjects.Text {
this.cryp = cryp; this.cryp = cryp;
this.skill = skill; this.skill = skill;
this.scene = scene; this.scene = scene;
this.activeSkill = null;
this.activeTarget = null;
this.setInteractive(); this.setInteractive();
} }
clickHandler() { clickHandler() {
this.scene.game.events.emit('SET_ACTIVE_SKILL', this); this.scene.game.events.emit('SET_ACTIVE_SKILL', this);
} }
select() {
this.setTint(0x00ff00);
}
activate() {
this.setTint(0xff0000);
}
deselect() {
this.clearTint();
}
} }
class CombatSkills extends Phaser.Scene { class CombatSkills extends Phaser.Scene {
@ -52,36 +69,44 @@ class CombatSkills extends Phaser.Scene {
create() { create() {
this.registry.events.on('changedata', this.updateData, this); this.registry.events.on('changedata', this.updateData, this);
this.account = this.registry.get('account'); this.account = this.registry.get('account');
// can't set this.game cause of phaser class named the same
const game = this.registry.get('game');
this.phase = game.phase;
this.renderSkills(game);
return true; return true;
} }
updateData(parent, key, data) { updateData(parent, key, data) {
if (key === 'game' && data) { if (key === 'game' && data) {
this.renderSkills(data); const shouldUpdate = data.phase !== this.phase;
if (shouldUpdate) return this.scene.restart();
return false;
} }
return true; return true;
} }
renderSkills(game) { renderSkills(game) {
const shouldUpdate = game.phase !== this.phase;
if (!shouldUpdate) return false;
this.phase = game.phase;
if (game.phase === 'Skill') return this.renderSkillPhase(game); if (game.phase === 'Skill') return this.renderSkillPhase(game);
if (game.phase === 'Target') return this.renderTargetPhase(game); if (game.phase === 'Target') return this.renderTargetPhase(game);
return false;
} }
renderSkillPhase(game) { renderSkillPhase(game) {
const { account, scene } = this; const { account, scene } = this;
const { keyboard } = this.input;
const addSkill = (i, j, skill, cryp) => { const addSkill = (i, j, skill, cryp) => {
const skillTextPos = skillTextPosition(i, j + 2); const skillTextPos = skillTextPosition(i, j + 2);
const skillObj = new CrypSkill(this, skillTextPos[0], skillTextPos[1], skill, cryp); const skillObj = new CrypSkill(this, skillTextPos[0], skillTextPos[1], skill, cryp);
this.add.existing(skillObj); this.add.existing(skillObj);
return skillObj;
}; };
const team = game.teams.find(t => t.id === account.id); const team = game.teams.find(t => t.id === account.id);
const enemyTeam = game.teams.find(t => t.id !== account.id);
team.cryps.forEach((cryp, i) => { team.cryps.forEach((cryp, i) => {
const namePos = skillTextPosition(i, 0); const namePos = skillTextPosition(i, 0);
@ -93,7 +118,17 @@ class CombatSkills extends Phaser.Scene {
if (cryp.hp.base === 0) return true; if (cryp.hp.base === 0) return true;
// draw the skills // draw the skills
cryp.skills.forEach((skill, j) => addSkill(i, j, skill, cryp)); const skillButtons = cryp.skills.map((skill, j) => addSkill(i, j, skill, cryp));
keyboard.on(CRYP_KEY_MAP[i],
() => this.mapSkillKeys(skillButtons, game.id, cryp.id, team.id, enemyTeam.id),
this
);
keyboard.on('keydown_ESC',
() => this.mapSkillKeys(skillButtons, game.id, cryp.id, team.id, enemyTeam.id),
this
);
return true; return true;
}); });
@ -101,13 +136,60 @@ class CombatSkills extends Phaser.Scene {
return true; return true;
} }
renderBlockPhase(game) { mapSkillKeys(skillButtons, gameId, crypId, alliesId, enemyId) {
const { account, scene } = this; const { keyboard } = this.input;
SKILL_KEY_MAP.forEach(k => keyboard.removeListener(k));
const skills = game.stack.find(skill => skill.target_team_id === account.id); // deselect any currently active button
if (this.activeSkill) {
this.activeSkill.deselect();
}
skillButtons.forEach((button, i) => {
keyboard.on(SKILL_KEY_MAP[i], () => {
this.activeSkill = button;
button.select();
// clear existing keys
CRYP_KEY_MAP.forEach(k => keyboard.removeListener(k));
TARGET_KEY_MAP.forEach(k => keyboard.removeListener(k));
CRYP_KEY_MAP.forEach(k => keyboard.on(k, () => {
this.game.events.emit('SEND_SKILL', gameId, crypId, alliesId, button.skill.skill);
button.activate();
this.activeSkill = null;
}));
TARGET_KEY_MAP.forEach(k => keyboard.on(k, () => {
this.game.events.emit('SEND_SKILL', gameId, crypId, enemyId, button.skill.skill);
button.activate();
this.activeSkill = null;
}));
keyboard.on('keydown_ESC',
() => {
CRYP_KEY_MAP.forEach(k => keyboard.removeListener(k));
TARGET_KEY_MAP.forEach(k => keyboard.removeListener(k));
},
this
);
}, this);
});
return true;
}
renderTargetPhase(game) {
const { account, scene } = this;
const { keyboard } = this.input;
const skills = game.stack.filter(skill => skill.target_team_id === account.id);
const friendlyCryps = game.teams.find(t => t.id === account.id).cryps;
skills.forEach((skill, i) => { skills.forEach((skill, i) => {
const cryp = game.teams.find(t => t.cryps.find(c => c.id === skill.source_cryp_id)); const crypTeam = game.teams.find(t => t.cryps.find(c => c.id === skill.source_cryp_id));
const cryp = crypTeam.cryps.find(c => c.id === skill.source_cryp_id);
// Draw the cryp name // Draw the cryp name
const namePos = skillTextPosition(i, 0); const namePos = skillTextPosition(i, 0);
@ -116,28 +198,50 @@ class CombatSkills extends Phaser.Scene {
const skillPos = skillTextPosition(i, 2); const skillPos = skillTextPosition(i, 2);
const skillObj = new CrypSkill(this, skillPos[0], skillPos[1], skill, cryp); const skillObj = new CrypSkill(this, skillPos[0], skillPos[1], skill, cryp);
this.add.existing(skillObj); this.add.existing(skillObj);
keyboard.on(
TARGET_KEY_MAP[i],
() => this.mapTargetKeys(skillObj, game.id, friendlyCryps, i),
this
);
}); });
return true; return true;
} }
crypKeyHandler(cryp, i) { mapTargetKeys(button, gameId, friendlyCryps) {
if (CRYP_KEY_MAP[i]) { const { keyboard } = this.input;
this.input.keyboard.removeListener(CRYP_KEY_MAP[i]);
if (cryp.skills.length > 0) { // check there are cryp skills CRYP_KEY_MAP.forEach(tKey => this.input.keyboard.removeListener(tKey));
this.input.keyboard.on(CRYP_KEY_MAP[i], () => {
SKILL_KEY_MAP.forEach(k => this.input.keyboard.removeListener(k)); // deselect any currently active button
cryp.skills.forEach((skill, i) => { if (this.activeTarget) {
this.input.keyboard.on(SKILL_KEY_MAP[i], () => { this.activeTarget.deselect();
this.game.events.emit('SET_ACTIVE_SKILL', skill);
skill.setActive();
}, this);
});
}, this);
}
} }
button.select();
this.activeTarget = button;
friendlyCryps.forEach((cryp, j) => {
keyboard.on(
CRYP_KEY_MAP[j],
() => {
this.game.events.emit('SEND_TARGET', gameId, cryp.id, button.skill.id);
button.activate();
this.activeTarget = null;
},
this
);
});
return true; return true;
} }
clearKeys() {
TARGET_KEY_MAP.forEach(tKey => this.input.keyboard.removeListener(tKey));
CRYP_KEY_MAP.forEach(tKey => this.input.keyboard.removeListener(tKey));
SKILL_KEY_MAP.forEach(tKey => this.input.keyboard.removeListener(tKey));
}
} }
module.exports = CombatSkills; module.exports = CombatSkills;

View File

@ -185,9 +185,9 @@ function createSocket(events) {
}); });
if (!account) events.loginPrompt(); if (!account) events.loginPrompt();
// if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
// send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } }); send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } });
// } }
return true; return true;
}); });

View File

@ -218,7 +218,9 @@ impl Cryp {
} }
pub fn available_skills(&self) -> Vec<&CrypSkill> { pub fn available_skills(&self) -> Vec<&CrypSkill> {
self.skills.iter().filter(|s| !self.disabled(s.skill).disabled).collect() self.skills.iter()
.filter(|s| s.skill.cd().is_none())
.filter(|s| !self.disabled(s.skill).disabled).collect()
} }
pub fn knows(&self, skill: Skill) -> bool { pub fn knows(&self, skill: Skill) -> bool {

View File

@ -35,7 +35,7 @@ impl Team {
.filter(|c| !c.is_ko()) .filter(|c| !c.is_ko())
.filter(|c| c.available_skills().len() > 0) .filter(|c| c.available_skills().len() > 0)
.collect::<Vec<&Cryp>>().len(); .collect::<Vec<&Cryp>>().len();
// println!("{:?} requires {:?} skills this turn", self.id, required); println!("{:?} requires {:?} skills this turn", self.id, required);
return required; return required;
} }