Skip to content

Commit

Permalink
feat: player 2 controls and collisions
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
karjona authored Sep 3, 2022
2 parents 933bc58 + c44032a commit c7bd8a3
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 152 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reapers",
"version": "1.0.0-alpha5",
"version": "1.0.0-alpha6",
"description": "The Reapers: Tournament of Destiny is a JS fighting game for the js13kGames 2022 competition",
"keywords": [
"game",
Expand Down
19 changes: 4 additions & 15 deletions src/code/data/Attacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@ import { Attack } from "../types/Attack";

export const Jab: Attack = {
damage: 10,
startup: 5,
active: 4,
recovery: 4,
startup: 15,
active: 12,
recovery: 12,
width: 18,
height: 10,
y: 10,
advantage: -1,
};

export const Strong: Attack = {
damage: 20,
startup: 5,
active: 8,
recovery: 8,
width: 20,
height: 20,
y: 2,
advantage: 2,
advantage: -10,
};
76 changes: 54 additions & 22 deletions src/code/functions/CheckFighterCollisions.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,75 @@
import { collides } from "kontra";
import { canvas, player1, player2 } from "../data/Instances";
import { fighterWalkSpeed } from "../data/Constants";
import { TrainingData } from "../modules/TrainingPanel/TrainingPanel";
import { GameConfig } from "../data/GameConfig";

export default function CheckFighterCollisions() {
// Player collisions
if (collides(player1.hitbox, player2.hitbox)) {
if (player1.hitbox.dx > 0 && player2.hitbox.dx === 0) {
// Player 1 is moving, player 2 is not
if (player1.hitbox.dx != 0 && player2.hitbox.dx === 0) {
// Push player 2 back
if (player2.hitbox.x + player2.hitbox.width < canvas.width) {
player2.hitbox.x += fighterWalkSpeed;
}

// Don't let player 1 move past player 2 in corner
if (player2.hitbox.x + player2.hitbox.width === canvas.width) {
player1.hitbox.x = player1.hitbox.x - 1;
player1.hitbox.x -= fighterWalkSpeed;
}

// Player 2 is moving, player 1 is not
} else if (player2.hitbox.dx != 0 && player1.hitbox.dx === 0) {
// Push player 1 back
if (player1.hitbox.x > 0) {
player1.hitbox.x -= fighterWalkSpeed;
}

// Don't let player 2 move past player 1 in corner
if (player1.hitbox.x === 0) {
player2.hitbox.x += fighterWalkSpeed;
}

// Both players are colliding, and both are moving or aren't moving at all
} else {
// If both players are pushing each other, don't move them
if (player1.hitbox.dx > 0 && player2.hitbox.dx < 0) {
player1.hitbox.x += -fighterWalkSpeed;
player2.hitbox.x += fighterWalkSpeed;
}
}
}

// Player 1 hits player 2
if (collides(player1.hurtbox, player2.hitbox)) {
player2.hitboxColor = "red";
if (player1.doingAttack) {
if (!player1.attackAlreadyHit) {
if (player1.doingAttack.damage >= player2.health) {
player2.sprite.playAnimation("ko");
player1.attackAlreadyHit = true;
// Player hits
if (
collides(player1.hurtbox, player2.hitbox) ||
collides(player2.hurtbox, player1.hitbox)
) {
const whoIsHurt = collides(player1.hurtbox, player2.hitbox)
? player2
: player1;
const whoIsAttacking = collides(player1.hurtbox, player2.hitbox)
? player1
: player2;

whoIsHurt.hitboxColor = "red";
if (whoIsAttacking.doingAttack) {
if (!whoIsAttacking.attackAlreadyHit) {
if (whoIsAttacking.doingAttack.damage >= whoIsHurt.health) {
whoIsHurt.sprite.playAnimation("ko");
whoIsAttacking.attackAlreadyHit = true;
GameConfig.fightersCanAct = false;
player2.health = 0;
player1.roundsWon += 1;
whoIsHurt.health = 0;
whoIsAttacking.roundsWon += 1;
} else {
player2.sprite.playAnimation("hit");
player2.recoil = 10;
player2.health -= player1.doingAttack.damage;
player2.canMove = false;
player2.stun =
player1.attackingFrames + player1.doingAttack.advantage;
TrainingData.frameAdvantage = player1.doingAttack.advantage;
TrainingData.damage = player1.doingAttack.damage;
player1.attackAlreadyHit = true;
whoIsHurt.sprite.playAnimation("hit");
whoIsHurt.recoil = 10;
whoIsHurt.health -= whoIsAttacking.doingAttack.damage;
whoIsHurt.canMove = false;
whoIsHurt.stun =
whoIsAttacking.attackingFrames +
whoIsAttacking.doingAttack.advantage;
whoIsAttacking.attackAlreadyHit = true;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/code/functions/PrepareFightScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { WinScreen } from "../modules/WinScreen/WinScreen";
import { GameConfig } from "../data/GameConfig";
import CheckGameState from "./CheckGameState";
import CheckFighterCollisions from "./CheckFighterCollisions";
import { toggleTrainingPanel } from "../modules/TrainingPanel/TrainingPanel";

export default function PrepareFightScene() {
fightScene.add([
Expand All @@ -24,6 +25,7 @@ export default function PrepareFightScene() {
]);

fightScene.update = function (dt) {
toggleTrainingPanel();
CheckFighterCollisions();
if (GameConfig.fightersCanAct === false) {
CheckGameState(dt);
Expand Down
87 changes: 1 addition & 86 deletions src/code/modules/TrainingPanel/TrainingPanel.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import { GameConfig } from "../../data/GameConfig";
import { renderText } from "../../data/Instances";
import { onKey, GameObject } from "kontra";

let lastMove = "";
let lastWrite = 0;

interface TrainingData {
attackFrames: number;
frameAdvantage: number;
damage: number;
}

export const TrainingData: TrainingData = {
attackFrames: 0,
frameAdvantage: 0,
damage: 0,
};

const moveList: string[] = [];
import { onKey } from "kontra";

export const toggleTrainingPanel = () => {
onKey("t", () => {
Expand All @@ -28,70 +10,3 @@ export const toggleTrainingPanel = () => {
export const isTrainingPanelEnabled = () => {
return GameConfig.showTrainingData ? true : false;
};

const TrainingPanelText = GameObject({
x: 2,
y: 2,
render: function () {
renderText(`ATTACK FRAMES: ${TrainingData.attackFrames}`, 0, 0, 5, "white");
renderText(
`FRAME ADVANTAGE: ${TrainingData.frameAdvantage}`,
0,
6,
5,
"white"
);
renderText(`DAMAGE: ${TrainingData.damage}`, 0, 12, 5, "white");
},
});

/*
const TrainingPanelMoveList = Text({
text: `${moveList.join("\n")}`,
x: -68,
y: 36,
font: "8px monospace",
});
*/

export const TrainingPanelMoveList = GameObject({});

export const addMoveToTrainingPanel = (moves: string[]) => {
function updateMoves(moves: string) {
moveList.unshift(moves);
TrainingPanelMoveList.text = `${moveList.join("\n")}`;
lastWrite = 8;
}

const currentMove: string = moves.join(" ");
if (lastWrite > 0) {
lastWrite--;
}

if (moves.length > 0) {
if (currentMove !== lastMove) {
updateMoves(currentMove);
} else {
if (lastWrite === 0) {
updateMoves(currentMove);
}
}
}

lastMove = currentMove;
movesToAddToTraining = [];

if (moveList.length > 10) {
moveList.pop();
}
};

export let movesToAddToTraining: string[] = [];

export const TrainingPanel = GameObject({
x: 70,
y: 2,
width: 88,
height: 26,
children: [TrainingPanelText, TrainingPanelMoveList],
});
32 changes: 6 additions & 26 deletions src/code/objects/Fighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import {
} from "../data/Constants";
import { Attack } from "../types/Attack";
import { Jab } from "../data/Attacks";
import {
movesToAddToTraining,
isTrainingPanelEnabled,
addMoveToTrainingPanel,
TrainingData,
} from "../modules/TrainingPanel/TrainingPanel";
import { isTrainingPanelEnabled } from "../modules/TrainingPanel/TrainingPanel";
import { GameConfig } from "../data/GameConfig";
import PlaySfx from "../../sounds/PlaySfx";
import { attackSfx } from "../../sounds/Sfx";
Expand Down Expand Up @@ -63,7 +58,7 @@ export default class Fighter {
this.position = position;

this.sprite = Sprite({
x: this.position === Position.Left ? -18 : -24,
x: this.position === Position.Left ? -12 : -24,
});

this.hurtbox = GameObject({});
Expand Down Expand Up @@ -141,14 +136,6 @@ export default class Fighter {
if (this.canMove === false) {
this.stop();
}

if (this.movingLeft) {
movesToAddToTraining.unshift("⬅️");
}

if (this.movingRight) {
movesToAddToTraining.unshift("➡️");
}
}

if (this.position === Position.Right) {
Expand All @@ -169,8 +156,6 @@ export default class Fighter {
if (this.position === Position.Left) {
onKey("k", () => {
if (GameConfig.fightersCanAct) {
movesToAddToTraining.push("🗡");
TrainingData.attackFrames = Jab.startup + Jab.active + Jab.recovery;
this.attack(Jab);
}
});
Expand All @@ -179,17 +164,13 @@ export default class Fighter {
if (this.position === Position.Right) {
onKey("y", () => {
if (GameConfig.fightersCanAct) {
TrainingData.attackFrames = Jab.startup + Jab.active + Jab.recovery;
this.attack(Jab);
}
});
}

if (this.attackingFrames > 0) {
this.attackingFrames--;
if (this.position === Position.Left) {
TrainingData.attackFrames = this.attackingFrames;
}

if (this.startupFrames > 0) {
this.startupFrames--;
Expand All @@ -206,7 +187,10 @@ export default class Fighter {
height: this.doingAttack.height,
y: this.doingAttack.y,
ttl: this.doingAttack.active,
x: this.hitbox.width,
x:
this.position === Position.Left
? this.hitbox.width
: -this.doingAttack.width,
render: function (this: GameObject) {
if (isTrainingPanelEnabled()) {
const context = getContext();
Expand Down Expand Up @@ -324,10 +308,6 @@ export default class Fighter {
}

update() {
if (this.position === Position.Left) {
addMoveToTrainingPanel(movesToAddToTraining);
}

this.handleMovement();
this.handleAttack();
this.handleStun();
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "./code/data/Instances";
import { LoadAssets } from "./code/functions/LoadAssets";
import { Scene } from "./code/types/Scene";

import PlayMusic from "./sounds/PlayMusic";
import { song } from "./sounds/Music";

Expand Down

0 comments on commit c7bd8a3

Please sign in to comment.