Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ES Lint, Typescript, Prettier and fix up all the files due to this change #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
"es6": true,
},
"plugins": ["prettier"],
"extends": [
"airbnb-base",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
},
"rules": {
"consistent-return": 0,
"import/prefer-default-export": 0,
"import/no-unresolved": 0,
"lines-between-class-members": 0,
"no-plusplus": 0,
"no-undef": 0,
"prettier/prettier": 2,
"quotes": [2, "single"],
"@typescript-eslint/array-type": [2, "generic"],
"@typescript-eslint/indent": [2, 2],
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/explicit-member-accessibility": 0,
}
}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/
build/
.prettierrc
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "es5"
}
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@
"@types/mocha": "^5.2.7",
"@types/node": "^10.12.2",
"@types/webpack": "^4.4.17",
"expect": "^24.8.0",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-config-prettier": "^6.0.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"mocha": "^6.2.0",
"prettier": "1.18.2",
"raw-loader": "^0.5.1",
"ts-loader": "^5.3.0",
"ts-mocha": "^6.0.0",
Expand All @@ -26,6 +37,7 @@
},
"dependencies": {
"copy-webpack-plugin": "^4.6.0",
"expect": "^24.8.0",
"html-webpack-plugin": "^3.2.0",
"phaser": "^3.18.1"
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/pokemon.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const pokemonData = {
} as const;

export type PokemonName = keyof typeof pokemonData;
export const allPokemonNames = Object.keys(pokemonData) as PokemonName[];
export const allPokemonNames = Object.keys(pokemonData) as Array<PokemonName>;

/**
* this is just to typecheck the PokemonData to make sure each object
Expand Down
6 changes: 1 addition & 5 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { GameScene } from './scenes/game/game.scene';
import { MenuScene } from './scenes/menu.scene';
import { PreloadScene } from './scenes/preload.scene';

export class PokemonAutochessGame extends Game {
constructor(config: Phaser.Types.Core.GameConfig) {
super(config);
}
}
export class PokemonAutochessGame extends Game {}

window.onload = () => {
const game = new PokemonAutochessGame({
Expand Down
3 changes: 2 additions & 1 deletion src/objects/floating-text.object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Phaser from 'phaser';

export class FloatingText extends Phaser.GameObjects.Text {
static readonly COMPLETE = 'complete';
private textObject: Phaser.GameObjects.Text;

constructor(scene: Phaser.Scene, x: number, y: number, text: string) {
Expand Down
43 changes: 28 additions & 15 deletions src/objects/pokemon.object.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Scene } from 'phaser';
import * as Phaser from 'phaser';
import { Pokemon, pokemonData, PokemonName } from '../core/pokemon.model';
import { FloatingText } from './floating-text.object';

interface SpriteParams {
readonly scene: Scene;
readonly scene: Phaser.Scene;
readonly x: number;
readonly y: number;
readonly id: string;
Expand All @@ -17,7 +17,6 @@ export type PokemonAnimationType = 'left' | 'right' | 'up' | 'down';
export class PokemonObject extends Phaser.GameObjects.Sprite {
private sprite: Phaser.GameObjects.Sprite;
private hpBar: Phaser.GameObjects.Graphics;

private currentHP: number;
private maxHP: number;

Expand All @@ -34,11 +33,12 @@ export class PokemonObject extends Phaser.GameObjects.Sprite {
this.name = params.name;

// load data from Pokemon data
this.currentHP = this.maxHP = pokemonData[params.name].maxHP;
this.basePokemon = pokemonData[params.name];
this.maxHP = pokemonData[this.name].maxHP;
this.currentHP = this.maxHP;
this.basePokemon = pokemonData[this.name];
this.side = params.side;

this.sprite = this.scene.add.sprite(this.x, this.y, params.name);
this.sprite = this.scene.add.sprite(this.x, this.y, this.name);
// default state is facing the player
this.playAnimation('down');

Expand All @@ -57,15 +57,7 @@ export class PokemonObject extends Phaser.GameObjects.Sprite {
this.hpBar.y = this.y;
this.hpBar.clear();

const hpBarColor =
this.currentHP >= this.maxHP / 2
? // high HP: green
0x32cd32
: this.currentHP >= this.maxHP / 5
? // low HP: orange
0xffa500
: // critical: red
0xdc143c;
const hpBarColor = this.getHPBarColor();
this.hpBar.fillStyle(hpBarColor, 1);
this.hpBar.fillRect(
-this.width / 2,
Expand All @@ -78,6 +70,27 @@ export class PokemonObject extends Phaser.GameObjects.Sprite {
this.hpBar.setDepth(1);
}

getHPBarColor() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like my ternary was perfectly understandable lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still feels like its easier to read it if we have a separate function. Like at least we know what High HP, Low HP and Critical HP ranges are. The ternary made it a bit confusing IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also got lint issues with ternary inside a ternary hahaha.

const fiftyPercentHP = this.maxHP / 2;
const twentyPercentHP = this.maxHP / 5;

// 50% or Higher
if (this.currentHP >= fiftyPercentHP) {
// High HP: Green
return 0x32cd32;
}

// Between 20% and 50%
if (this.currentHP >= twentyPercentHP) {
// Low HP: Orange
return 0xffa500;
}

// Between 0% and 20%
// Critical: Red
return 0xdc143c;
}

playAnimation(type: PokemonAnimationType) {
this.sprite.play(`${this.name}--${type}`);
}
Expand Down
1 change: 1 addition & 0 deletions src/scenes/game/game.helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PokemonObject } from '../../objects/pokemon.object';
import { getNearestTarget } from './game.helpers';

describe('getNearestTarget', () => {
// TODO: Probably want to fix this, @typescript-eslint/no-object-literal-type-assertion
const playerMock = { side: 'player' } as PokemonObject;
const enemyMock = { side: 'enemy' } as PokemonObject;

Expand Down
7 changes: 4 additions & 3 deletions src/scenes/game/game.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getNearestTarget(
return undefined;
}

const side = self.side;
const { side } = self;

// the following code is some pathfinding magic
// don't question it or look too closely
Expand All @@ -41,17 +41,18 @@ export function getNearestTarget(
let x2 = x + i;
let y2 = y;

// TODO: Fix this loop, no-restricted-syntax
// at each loop, there are 4 * dist possible places to go
// and we will go in clockwise order
// eg. (1,0) (0,1) (-1,0) (0,-1)
// or (2,0) (1,1) (0,2) (-1,1) (-2,0) (-1,-1) (0,-2) (1,-1)
// if you observe, we can do (x-=1, y+=1) to iterate around the bottom-right
// then do (x-=1, y-=1) to iterate around the bottom-left
// and so on
for (let step of [[-1, 1], [-1, -1], [1, -1], [1, 1]]) {
for (const step of [[-1, 1], [-1, -1], [1, -1], [1, 1]]) {
for (let k = 0; k < i; k++) {
// if still on board
if (0 <= x2 && x2 < width && 0 <= y2 && y2 < height) {
if (x2 >= 0 && x2 < width && y2 >= 0 && y2 < height) {
const target = board[x2][y2];
// opposite side matched: target this one
if (target && target.side !== side) {
Expand Down
20 changes: 10 additions & 10 deletions src/scenes/game/game.scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ import { Coords, getGridDistance, getNearestTarget } from './game.helpers';
const CELL_WIDTH = 70;
const BOARD_WIDTH = 5;

/**
* Returns the graphical x and y coordinates for a spot in the battle grid.
* @param x
* @param y
*/
function getCoordinatesForGrid({ x, y }: Coords): Coords {
return { x: 400 + (x - 2) * CELL_WIDTH, y: 300 + (y - 2) * CELL_WIDTH };
}

export class GameScene extends Scene {
static readonly KEY = 'GameScene';

private board: (PokemonObject | undefined)[][] = [[], [], [], [], []];
private board: Array<Array<PokemonObject | undefined>> = [[], [], [], [], []];
private grid: Phaser.GameObjects.Grid;

constructor() {
Expand Down Expand Up @@ -125,12 +134,3 @@ export class GameScene extends Scene {
setTimeout(() => this.takeTurn(myCoords), delay);
}
}

/**
* Returns the graphical x and y coordinates for a spot in the battle grid.
* @param x
* @param y
*/
function getCoordinatesForGrid({ x, y }: Coords): Coords {
return { x: 400 + (x - 2) * CELL_WIDTH, y: 300 + (y - 2) * CELL_WIDTH };
}
1 change: 1 addition & 0 deletions src/scenes/menu.scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GameScene } from './game/game.scene';

export class MenuScene extends Scene {
static readonly KEY = 'MenuScene';

private titlePokemon: PokemonObject;
private startButton: Phaser.GameObjects.Text;

Expand Down
Loading