Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberDex committed Oct 18, 2023
1 parent adadab7 commit 8315b27
Show file tree
Hide file tree
Showing 43 changed files with 1,036 additions and 712 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
}
},
"rules": {
"no-empty-function": 0,
"max-len": 0,
"no-new": 0,
"no-restricted-imports": ["error", { "paths": ["pixi.js"] }],
"@typescript-eslint/no-unused-expressions": [
1,
{ "allowShortCircuit": true, "allowTernary": true }
],
"no-mixed-operators": "off",
"@typescript-eslint/no-parameter-properties": 1,
"jsdoc/multiline-blocks": [
1,
{ "noMultilineBlocks": true, "minimumLengthForMultiline": 115 }
Expand All @@ -35,7 +36,6 @@
"jsdoc/check-values": 1,
"jsdoc/empty-tags": 1,
"jsdoc/implements-on-classes": 1,
"jsdoc/newline-after-description": [1, "never"],
"jsdoc/no-multi-asterisks": [1, { "allowWhitespace": true }],
"jsdoc/require-param": 1,
"jsdoc/require-param-description": 0,
Expand Down
88 changes: 60 additions & 28 deletions src/Game.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable new-cap */
import { Assets } from '@pixi/assets';
import { app } from './main';
import { AppScreen } from './components/basic/AppScreen';
Expand All @@ -10,20 +11,22 @@ export type SceneData = {
window?: Windows,
type?: GameTypes,
restart?: boolean,
}
};

/** Interface for app screens constructors */
interface AppScreenConstructor {
interface AppScreenConstructor
{
new (data?: any): AppScreen;
assetBundles?: string[];
}
/**
* Class for controlling visibility of all scenes,
/**
* Class for controlling visibility of all scenes,
* preload assets for each of them and create/update/resize them.
*
*
* It is also a navigation controller of the app.
**/
class Game { // We DO NOT export this class, as we want to have only one instance of it, it is exported on the bottom of this file
*/
class Game
{ // We DO NOT export this class, as we want to have only one instance of it, it is exported on the bottom of this file
private currentScreen?: AppScreen; // Current screen being displayed
private currentScreenResize?: () => void; // Resize function to avoid problems with scope
private loadScreen?: AppScreen; // Default load screen
Expand All @@ -33,77 +36,106 @@ class Game { // We DO NOT export this class, as we want to have only one instanc

public bg!: Background; // background layout

/** Set the default load screen */
public setLoadScreen(screen: AppScreenConstructor) {
/**
* Set the default load screen
* @param screen
*/
public setLoadScreen(screen: AppScreenConstructor)
{
this.loadScreen = new screen(); // Create a new instance of the load screen given in `screen` parameter
}

/** Create game background */
addBG () {
addBG()
{
if (this.bg) { return; }

this.bg = new Background(); // Create a new instance of the background layout

this.bg.resize(this._w, this._h); // Resize background as it is a layout and it needs to know its size in order it's core functionality to work
app.stage.addChild(this.bg); // Add background to the stage

Check failure on line 56 in src/Game.ts

View workflow job for this annotation

GitHub Actions / deploy

Argument of type 'Background' is not assignable to parameter of type 'DisplayObject'.
}

/** Add screen to the stage, link update & resize functions */
private async addScreen(screen: AppScreen) {
/**
* Add screen to the stage, link update & resize functions
* @param screen
*/
private async addScreen(screen: AppScreen)
{
// Add screen to stage
app.stage.addChild(screen); // Add screen to the stage

Check failure on line 66 in src/Game.ts

View workflow job for this annotation

GitHub Actions / deploy

Argument of type 'AppScreen' is not assignable to parameter of type 'DisplayObject'.

// Add screen's resize handler, if available
if (screen.resize) {
if (screen.resize)
{
this.currentScreenResize = () => screen.resize; // Encapsulate resize in another function that can be removed later, to avoid scope issues with addEventListener
screen.resize(this._w, this._h); // Trigger a first resize
}

if (screen.onUpdate) { // Add update function if it exists
if (screen.onUpdate)
{ // Add update function if it exists
app.ticker.add(screen.onUpdate, screen); // Add update function to the ticker
}

if (screen.show) { // Show the new screen if it has a show method
if (screen.show)
{ // Show the new screen if it has a show method
await screen.show(); // Wait for the screen to be shown
}
}

/** Remove screen from the stage, unlink update & resize functions */
private async removeScreen(screen: AppScreen) {
if (screen.hide) { // Hide screen if method is available
/**
* Remove screen from the stage, unlink update & resize functions
* @param screen
*/
private async removeScreen(screen: AppScreen)
{
if (screen.hide)
{ // Hide screen if method is available
await screen.hide(); // Wait for the screen to be hidden
}

if (this.currentScreenResize) { // Unlink resize handler if exists
if (this.currentScreenResize)
{ // Unlink resize handler if exists
window.removeEventListener('resize', this.currentScreenResize); // Remove resize listener
}

if (screen.onUpdate) { // Unlink update function if method is available
if (screen.onUpdate)
{ // Unlink update function if method is available
app.ticker.remove(screen.onUpdate, screen); // Remove update function from the ticker
}

if (screen.parent) { // Remove screen from its parent (usually app.stage, if not changed)
if (screen.parent)
{ // Remove screen from its parent (usually app.stage, if not changed)
screen.parent.removeChild(screen); // Remove screen from its parent

Check failure on line 109 in src/Game.ts

View workflow job for this annotation

GitHub Actions / deploy

Argument of type 'AppScreen' is not assignable to parameter of type 'DisplayObject'.
}
}

/** Hide current screen (if there is one) and present a new screen. */
public async showScreen(screen: AppScreenConstructor, data?: SceneData) {
if (this.currentScreen) { // If there is a screen already created, hide and destroy it
/**
* Hide current screen (if there is one) and present a new screen.
* @param screen
* @param data
*/
public async showScreen(screen: AppScreenConstructor, data?: SceneData)
{
if (this.currentScreen)
{ // If there is a screen already created, hide and destroy it
await this.removeScreen(this.currentScreen); // Remove current screen
this.currentScreen.destroy(); // Destroy current screen
}

// Load assets for the new screen, if available
if (screen.assetBundles && !areBundlesLoaded(screen.assetBundles)) {
if (screen.assetBundles && !areBundlesLoaded(screen.assetBundles))
{
// If assets are not loaded yet, show loading screen, if there is one
if (this.loadScreen) {
if (this.loadScreen)
{
this.addScreen(this.loadScreen); // Add loading screen to the stage
}

await Assets.loadBundle(screen.assetBundles); // Load all assets required by this new screen

if (this.loadScreen) { // Hide loading screen, if exists
if (this.loadScreen)
{ // Hide loading screen, if exists
this.removeScreen(this.loadScreen); // Remove loading screen from the stage
}
}
Expand Down
46 changes: 28 additions & 18 deletions src/components/CheckBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { Sprite } from '@pixi/sprite';
import { TextStyle } from '@pixi/text';
import { colors } from '../config/colors';

/** Extends a BasicCheckBox class and apply config to it,
* so that instance can be used without need to config it. */
export class CheckBox extends BasicCheckBox {
constructor(options: CheckBoxOptions) {
/** Extends a BasicCheckBox class and apply config to it, so that instance can be used without need to config it. */
export class CheckBox extends BasicCheckBox
{
constructor(options: CheckBoxOptions)
{
super({
style: { // style is an object with checkbox assets and text styles
checked: createCheckBox( // sprite(Container), that shows when checkbox is checked
Expand All @@ -28,54 +29,63 @@ export class CheckBox extends BasicCheckBox {
text: options.text, // text that will be displayed on the checkbox
});

if (options.onChange) { // if callback function is provided
if (options.onChange)
{ // if callback function is provided
this.onCheck.connect(options.onChange); // connect checkbox change event to the provided callback
}
}
}

/** Creates a sprite(Container) with checkbox assets.
* This function is abstracted from a class as it is used inside a `super` call,
/**
* Creates a sprite(Container) with checkbox assets.
* This function is abstracted from a class as it is used inside a `super` call,
* and `this.createCheckBox` can not be called before `super` call.
*/
* @param checkboxBG
* @param checkboxFG
*/
function createCheckBox(
checkboxBG: string, // texture key for the background of the checkbox
checkboxFG?: string // texture key for the foreground of the checkbox
): Sprite {
): Sprite
{
const bg = Sprite.from(checkboxBG); // create a sprite from the provided texture key

if (checkboxBG === 'RoundSubstrate') { // if the background is 'RoundSubstrate' texture

if (checkboxBG === 'RoundSubstrate')
{ // if the background is 'RoundSubstrate' texture
bg.scale.set(0.4); // scale it down
}

if (checkboxFG) { // if the foreground is provided
if (checkboxFG)
{ // if the foreground is provided
const checkBox = Sprite.from(checkboxFG); // create a sprite from the provided texture key

checkBox.anchor.set(0.5); // set anchor to the center

checkBox.x = bg.width / 2; // set x position to the center of the background
checkBox.y = bg.height / 2; // set y position to the center of the background

if (checkboxBG === 'RoundSubstrate' && checkboxFG === 'Radio') { // if the background is 'RoundSubstrate' texture and the foreground is 'Radio' texture
if (checkboxBG === 'RoundSubstrate' && checkboxFG === 'Radio')
{ // if the background is 'RoundSubstrate' texture and the foreground is 'Radio' texture
checkBox.scale.set(1.85); // scale it up
checkBox.x += 37; // move it to the right
checkBox.y += 38; // move it down
}

if (checkboxBG === 'RoundSubstrate' && checkboxFG === 'CheckBox') { // if the background is 'RoundSubstrate' texture and the foreground is 'CheckBox' texture
if (checkboxBG === 'RoundSubstrate' && checkboxFG === 'CheckBox')
{ // if the background is 'RoundSubstrate' texture and the foreground is 'CheckBox' texture
checkBox.scale.set(2); // scale it up
checkBox.x += 50; // move it to the right
checkBox.y += 30; // move it down
}

bg.addChild(checkBox); // add the foreground to the background to get a single sprite(Container)

Check failure on line 81 in src/components/CheckBox.ts

View workflow job for this annotation

GitHub Actions / deploy

Argument of type 'Sprite' is not assignable to parameter of type 'DisplayObject'.
}

return bg; // return the sprite(Container)
}

/** Type for the component settings description. */
export type CheckBoxOptions = {
export type CheckBoxOptions = {
checked: boolean, // initial checkbox state
checkboxBG: string, // texture key for the background of the checkbox
checkboxFG: string, // texture key for the foreground of the checkbox
Expand All @@ -86,4 +96,4 @@ export type CheckBoxOptions = {
y: number, // y offset
}
onChange?: (checked: boolean) => void, // callback function that will be called when checkbox state is changed
}
};
23 changes: 13 additions & 10 deletions src/components/CloseButton.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { FancyButton } from "@pixi/ui";
import { Counter } from "./basic/Counter";
/* eslint-disable accessor-pairs */
import { FancyButton } from '@pixi/ui';
import { Counter } from './basic/Counter';

/** FancyButton config to be applied on FancyButton,
* so that component can be used without setting all the configs. */
export class CloseButton extends FancyButton {
/** FancyButton config to be applied on FancyButton, so that component can be used without setting all the configs. */
export class CloseButton extends FancyButton
{
counter!: Counter; // counter component that will be added to the button. It will show the number of notifications

constructor(onclick: () => void) {

constructor(onclick: () => void)
{
super({
defaultView: `SmallButton-pressed`, // this is a key to the texture atlas for default button state view
hoverView: `SmallButton-hover`, // this is a key to the texture atlas for hover button state view
Expand Down Expand Up @@ -34,12 +36,13 @@ export class CloseButton extends FancyButton {
});

this.onPress.connect(onclick); // connect button press event to the provided callback

this.anchor.set(0.5); // set button anchor to the center, this is needed for the button to scale correctly when animated
this.scale.set(0.8); // scale down the button
}

set notifications(amount: number) { // set the number of notifications
set notifications(amount: number)
{ // set the number of notifications
this.counter.number.text = String(amount); // set the text of the counter
}
};
}
Loading

0 comments on commit 8315b27

Please sign in to comment.