Skip to content

Commit

Permalink
refactor: export shared modules in root index file
Browse files Browse the repository at this point in the history
  • Loading branch information
greenpixels committed Apr 3, 2024
1 parent f6b847f commit dc41b3c
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 97 deletions.
3 changes: 1 addition & 2 deletions client/src/classes/Entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Vector2DTO } from '@shared/dtos/Vector2DTO.ts'
import { EntityDTO } from '@shared/dtos/EntityDTO.ts'
import { Vector2DTO, EntityDTO } from '@shared/index'
import { Sprite, Container } from 'pixi.js'

export abstract class Entity<T extends EntityDTO> {
Expand Down
11 changes: 4 additions & 7 deletions client/src/classes/Player.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { PlayerDTO } from '@shared/dtos/PlayerDTO.ts'
import { PlayerDTO, Trigonometry, Vector2DTO, Vector2 } from '@shared/index'
import { Entity } from './Entity'
import { Vector2DTO } from '@shared/dtos/Vector2DTO'
import { Vector2 } from '@shared/classes/Vector2'
import { angleToRadians, lengthdirX, lengthdirY } from '@shared/helpers/trigonometry'
import { Assets, Container, Sprite } from 'pixi.js'
import SniperImage from '@assets/spr_sniper.png'
import PlayerImage from '@assets/spr_human1.png'
Expand Down Expand Up @@ -48,7 +45,7 @@ export class Player extends Entity<PlayerDTO> {
}
this.aimDirection = dto.aimDirection
const angle = new Vector2(this.aimDirection).angle()
this.gunSprite.rotation = angleToRadians(angle)
this.gunSprite.rotation = Trigonometry.angleToRadians(angle)
this.gunSprite.zIndex = this.sprite.zIndex + Math.sign(this.aimDirection.y) / 2
if (this.aimDirection.x !== 0) this.gunSprite.scale.y = Math.sign(this.aimDirection.x)

Expand All @@ -58,11 +55,11 @@ export class Player extends Entity<PlayerDTO> {
x:
this.position.x +
this.sprite.width / 2 +
lengthdirX(gunDistance - Math.min(gunDistance, this.recoilFactor * gunDistance), angle),
Trigonometry.lengthdirX(gunDistance - Math.min(gunDistance, this.recoilFactor * gunDistance), angle),
y:
this.position.y +
this.sprite.height / 2 +
lengthdirY(gunDistance - Math.min(gunDistance, this.recoilFactor * gunDistance), angle),
Trigonometry.lengthdirY(gunDistance - Math.min(gunDistance, this.recoilFactor * gunDistance), angle),
}
this.impactFactor *= 0.85
this.recoilFactor *= 0.75
Expand Down
6 changes: 2 additions & 4 deletions client/src/classes/Projectile.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ProjectileDTO } from '@shared/dtos/ProjectileDTO.ts'
import { ProjectileDTO, Vector2, Trigonometry } from '@shared/index'
import { Entity } from './Entity'
import { Assets, Container, Sprite } from 'pixi.js'
import BulletImage from '@assets/spr_bullet.png'
import { angleToRadians } from '@shared/helpers/trigonometry'
import { Vector2 } from '@shared/classes/Vector2'

export class Projectile extends Entity<ProjectileDTO> {
constructor(stage: Container, dto: ProjectileDTO) {
Expand All @@ -24,7 +22,7 @@ export class Projectile extends Entity<ProjectileDTO> {
public sync(dto: ProjectileDTO) {
this.position = dto.position
const angle = new Vector2(dto.direction).angle()
this.sprite.rotation = angleToRadians(angle)
this.sprite.rotation = Trigonometry.angleToRadians(angle)
}

public cleanup(stage: Container): void {
Expand Down
2 changes: 1 addition & 1 deletion client/src/classes/effects/ProjectileExplosionEffect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vector2DTO } from '@shared/dtos/Vector2DTO'
import { Vector2DTO } from '@shared/index'
import { Container } from 'pixi.js'
import ExplosionImage from '@assets/spr_explosion.gif'
import { ImportHelper } from '../ImportHelper'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlayerDTO } from '@shared/dtos/PlayerDTO'
import { PlayerDTO } from '@shared/index'
import { useState } from 'react'
import Style from './PlayerList.module.css'
import ChevronDown from '@assets/ui_chevron_down.svg'
Expand Down
16 changes: 9 additions & 7 deletions client/src/handlers/ClientGameHandler/ClientGameHandler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { GameEventHandler } from '@shared/GameEventHandler'
import {
GameEventHandler,
PlayerDTO,
ProjectileDTO,
DTOConverter,
Vector2DTO,
KeyMap,
FormatHelper,
} from '@shared/index'
import { Socket } from 'socket.io-client'
import { PlayerDTO } from '@shared/dtos/PlayerDTO'
import { ProjectileDTO } from '@shared/dtos/ProjectileDTO'
import { DTOConverter } from '@shared/classes/DTOConverter'
import { Vector2DTO } from '@shared/dtos/Vector2DTO'
import { Application, Renderer } from 'pixi.js'
import { PlayerHandler } from '../PlayerHandler/PlayerHandler'
import { KeyMap } from '@shared/types/KeyMap'
import { ProjectileHandler } from '../ProjectileHandler/ProjectileHandler'
import { InputHandler } from '../InputHandler/InputHandler'
import { FormatHelper } from '@shared/helpers/FormatHelper'

export type ClientGameHandlerProps = {
socket: Socket & { id: string }
Expand Down
2 changes: 1 addition & 1 deletion client/src/handlers/InputHandler/InputHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vector2DTO } from '@shared/dtos/Vector2DTO'
import { Vector2DTO } from '@shared/index'
import { InputHandler } from './InputHandler'
const MOCK_SCREEN_SIZE = { x: 1000, y: 1000 }

Expand Down
2 changes: 1 addition & 1 deletion client/src/handlers/InputHandler/InputHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vector2DTO } from '@shared/dtos/Vector2DTO'
import { Vector2DTO } from '@shared/index'

export class InputHandler {
moveVector: Vector2DTO = { x: 0, y: 0 }
Expand Down
3 changes: 1 addition & 2 deletions client/src/handlers/PlayerHandler/PlayerHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { PlayerDTO } from '@shared/dtos/PlayerDTO'
import { PlayerDTO, KeyMap } from '@shared/index'
import { ApplicationMock } from '../../__mocks__/Application.mock'
import { PlayerHandler } from './PlayerHandler'
import { Player } from '../../classes/Player'
import { KeyMap } from '@shared/types/KeyMap'

describe(`Testing PlayerHandler`, () => {
const application = ApplicationMock()
Expand Down
4 changes: 1 addition & 3 deletions client/src/handlers/PlayerHandler/PlayerHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Application } from 'pixi.js'
import { Player } from '../../classes/Player'
import { PlayerDTO } from '@shared/dtos/PlayerDTO'
import { KeyMap } from '@shared/types/KeyMap'
import { ProjectileDTO } from '@shared/dtos/ProjectileDTO'
import { PlayerDTO, KeyMap, ProjectileDTO } from '@shared/index'

export class PlayerHandler {
players: { [key: string]: Player } = {}
Expand Down
3 changes: 1 addition & 2 deletions client/src/handlers/ProjectileHandler/ProjectileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ProjectileDTO } from '@shared/dtos/ProjectileDTO'
import { KeyMap } from '@shared/types/KeyMap'
import { ProjectileDTO, KeyMap } from '@shared/index'
import { Projectile } from '../../classes/Projectile'
import { Application } from 'pixi.js'
import { ProjectileExplosionEffect } from '../../classes/effects/ProjectileExplosionEffect'
Expand Down
2 changes: 1 addition & 1 deletion client/src/types/GameInformation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlayerDTO } from '@shared/dtos/PlayerDTO'
import { PlayerDTO } from '@shared/index'

export interface GameInformation {
players: Array<PlayerDTO>
Expand Down
24 changes: 13 additions & 11 deletions server/src/PlayerHandler/PlayerHandler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Socket } from 'socket.io'
import DTOConverter from '../../../shared/classes/DTOConverter'
import { Vector2 } from '../../../shared/classes/Vector2'
import { PlayerDTO } from '../../../shared/dtos/PlayerDTO'
import { lengthdirX, lengthdirY } from '../../../shared/helpers/trigonometry'
import {
DTOConverter,
Trigonometry,
Vector2DTO,
GameEventHandler,
Vector2,
PlayerDTO,
KeyMap,
} from '../../../shared/index'
import { Player } from '../classes/Player'
import { Projectile } from '../classes/Projectile'
import { Vector2DTO } from '../../../shared/dtos/Vector2DTO'
import { KeyMap } from '../../../shared/types/KeyMap'
import { GameEventHandler } from '../../../shared/GameEventHandler'
import { GlobalValuesMap } from '../classes/GlobalValuesMap'

export class PlayerHandler {
Expand Down Expand Up @@ -54,8 +56,8 @@ export class PlayerHandler {
player.lastShotAt = now
const projectile = new Projectile(socketId, { ...player.position }, { ...player.aimDirection })
const angle = new Vector2(projectile.direction).angle()
projectile.position.x += lengthdirX(60, angle)
projectile.position.y += lengthdirY(60, angle)
projectile.position.x += Trigonometry.lengthdirX(60, angle)
projectile.position.y += Trigonometry.lengthdirY(60, angle)
return projectile
}

Expand All @@ -74,8 +76,8 @@ export class PlayerHandler {
if (Math.abs(player.velocity.x) + Math.abs(player.velocity.y) > 0) {
const angle = new Vector2(player.velocity).angle()
const baseSpeed = GlobalValuesMap.PLAYER_BASE_SPEED
player.position.x += lengthdirX(baseSpeed, angle)
player.position.y += lengthdirY(baseSpeed, angle)
player.position.x += Trigonometry.lengthdirX(baseSpeed, angle)
player.position.y += Trigonometry.lengthdirY(baseSpeed, angle)
}
playerDtoMap[player.id] = DTOConverter.toPlayerDTO(player)
}
Expand Down
11 changes: 3 additions & 8 deletions server/src/ProjectileHandler/ProjectileHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import DTOConverter from '../../../shared/classes/DTOConverter'
import { Vector2 } from '../../../shared/classes/Vector2'
import { ProjectileDTO } from '../../../shared/dtos/ProjectileDTO'
import { projectileDTOSchema } from '../../../shared/dtos/ProjectileDTO.zod'
import { lengthdirX, lengthdirY } from '../../../shared/helpers/trigonometry'
import { KeyMap } from '../../../shared/types/KeyMap'
import { DTOConverter, Vector2, ProjectileDTO, projectileDTOSchema, Trigonometry, KeyMap } from '../../../shared/index'
import { ServerGameHandler } from '../ServerGameHandler/ServerGameHandler'
import { GlobalValuesMap } from '../classes/GlobalValuesMap'
import { Projectile } from '../classes/Projectile'
Expand All @@ -30,8 +25,8 @@ export class ProjectileHandler {
projectileDTOSchema.parse(projectile)
const angle = new Vector2(projectile.direction).angle()
const baseSpeed = GlobalValuesMap.PROJECTILE_BASE_SPEED
projectile.position.x += lengthdirX(baseSpeed, angle)
projectile.position.y += lengthdirY(baseSpeed, angle)
projectile.position.x += Trigonometry.lengthdirX(baseSpeed, angle)
projectile.position.y += Trigonometry.lengthdirY(baseSpeed, angle)
if (Date.now() - projectile.createdAt > 500) {
this.removeProjectile(projectile.id, false)
isRemoved = true
Expand Down
16 changes: 9 additions & 7 deletions server/src/ServerGameHandler/ServerGameHandler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { GameEventHandler } from '../../../shared/GameEventHandler'
import {
GameEventHandler,
PlayerDTO,
Vector2DTO,
ProjectileDTO,
DTOConverter,
vector2DTOSchema,
Valid,
} from '../../../shared/index'
import { Server } from 'socket.io'
import { PlayerDTO } from '../../../shared/dtos/PlayerDTO'
import { Player } from '../classes/Player'
import { DefaultEventsMap } from 'socket.io/dist/typed-events'
import { Vector2DTO } from '../../../shared/dtos/Vector2DTO'
import { Projectile } from '../classes/Projectile'
import { ProjectileDTO } from '../../../shared/dtos/ProjectileDTO'
import { DTOConverter } from '../../../shared/classes/DTOConverter'
import { z } from 'zod'
import { vector2DTOSchema } from '../../../shared/dtos/Vector2DTO.zod'
import { Valid } from '../../../shared/decorators/Valid'
import { PlayerHandler } from '../PlayerHandler/PlayerHandler'
import { ProjectileHandler } from '../ProjectileHandler/ProjectileHandler'

Expand Down
3 changes: 1 addition & 2 deletions server/src/classes/Player.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PlayerDTO } from '../../../shared/dtos/PlayerDTO'
import { Vector2DTO } from '../../../shared/dtos/Vector2DTO'
import { PlayerDTO, Vector2DTO } from '../../../shared/index'

export class Player implements PlayerDTO {
id: string
Expand Down
3 changes: 1 addition & 2 deletions server/src/classes/Projectile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Vector2DTO } from '../../../shared/dtos/Vector2DTO'
import { ProjectileDTO } from '../../../shared/dtos/ProjectileDTO'
import { Vector2DTO, ProjectileDTO } from '../../../shared/index'
import { randomUUID } from 'crypto'

export class Projectile implements ProjectileDTO {
Expand Down
3 changes: 1 addition & 2 deletions shared/classes/DTOConverter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PlayerDTO } from '../dtos/PlayerDTO'
import { ProjectileDTO } from '../dtos/ProjectileDTO'
import { PlayerDTO, ProjectileDTO } from '../'

export class DTOConverter {
static toPlayerDTO(player: PlayerDTO) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProjectileDTO } from './dtos/ProjectileDTO'
import { PlayerDTO } from './dtos/PlayerDTO'
import { Vector2DTO } from './dtos/Vector2DTO'
import { ProjectileDTO } from '../dtos/ProjectileDTO'
import { PlayerDTO } from '../dtos/PlayerDTO'
import { Vector2DTO } from '../dtos/Vector2DTO'

export abstract class GameEventHandler {
EVENT_GAME_TICK = 'game_tick_event'
Expand Down
37 changes: 18 additions & 19 deletions shared/helpers/trigonometry.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
import { angleToRadians, lengthdirX, lengthdirY } from './trigonometry'

import { Trigonometry } from './Trigonometry'
describe('Test angle to radians functionnality', () => {
test('An angle of 180 should be PI radians', () => {
expect(angleToRadians(180)).toBeCloseTo(Math.PI)
expect(Trigonometry.angleToRadians(180)).toBeCloseTo(Math.PI)
})

test('An angle of 360 should be zero radians', () => {
expect(angleToRadians(360)).toBe(0)
expect(Trigonometry.angleToRadians(360)).toBe(0)
})

test('An angle of 0 should be the same as an angle of 360', () => {
expect(angleToRadians(0)).toBe(angleToRadians(360))
expect(Trigonometry.angleToRadians(0)).toBe(Trigonometry.angleToRadians(360))
})

test('An angle close to 360 should be close to to 2PI radians', () => {
expect(angleToRadians(359.999)).toBeCloseTo(Math.PI * 2)
expect(Trigonometry.angleToRadians(359.999)).toBeCloseTo(Math.PI * 2)
})

test('An angle close to 360 should be close to to 2PI radians', () => {
expect(angleToRadians(359.999)).toBeCloseTo(Math.PI * 2)
expect(Trigonometry.angleToRadians(359.999)).toBeCloseTo(Math.PI * 2)
})

test('A negative value (-45) should be the same as (315)', () => {
expect(angleToRadians(-45)).toBeCloseTo(angleToRadians(315))
expect(Trigonometry.angleToRadians(-45)).toBeCloseTo(Trigonometry.angleToRadians(315))
})

test('A negative value (-180) should be the same as (180)', () => {
expect(angleToRadians(-180)).toBeCloseTo(angleToRadians(180))
expect(Trigonometry.angleToRadians(-180)).toBeCloseTo(Trigonometry.angleToRadians(180))
})

test('A negative value (-1) should be the same as (359)', () => {
expect(angleToRadians(-1)).toBeCloseTo(angleToRadians(359))
expect(Trigonometry.angleToRadians(-1)).toBeCloseTo(Trigonometry.angleToRadians(359))
})

test('A value exceeding 360 by 180 should be the same as (180)', () => {
expect(angleToRadians(360 + 180)).toBeCloseTo(angleToRadians(180))
expect(Trigonometry.angleToRadians(360 + 180)).toBeCloseTo(Trigonometry.angleToRadians(180))
})

test('A value exceeding 152 by 180 should be the same as (152)', () => {
expect(angleToRadians(360 + 152)).toBeCloseTo(angleToRadians(152))
expect(Trigonometry.angleToRadians(360 + 152)).toBeCloseTo(Trigonometry.angleToRadians(152))
})

test('A negative value exceeding -360 by 50 should be the same as (310)', () => {
expect(angleToRadians(-360 - 50)).toBeCloseTo(angleToRadians(310))
expect(Trigonometry.angleToRadians(-360 - 50)).toBeCloseTo(Trigonometry.angleToRadians(310))
})
})

describe('Test lengthdir_x functionnality', () => {
test('An angle of 0, 360 and 720 should all return the same value', () => {
const arbitaryLength = 10
const resultOf0Angle = lengthdirX(arbitaryLength, 0)
const resultOf360Angle = lengthdirX(arbitaryLength, 360)
const resultOf720Angle = lengthdirX(arbitaryLength, 720)
const resultOf0Angle = Trigonometry.lengthdirX(arbitaryLength, 0)
const resultOf360Angle = Trigonometry.lengthdirX(arbitaryLength, 360)
const resultOf720Angle = Trigonometry.lengthdirX(arbitaryLength, 720)
expect(resultOf0Angle).toBeCloseTo(resultOf360Angle)
expect(resultOf360Angle).toBeCloseTo(resultOf720Angle)
})
Expand All @@ -60,9 +59,9 @@ describe('Test lengthdir_x functionnality', () => {
describe('Test lengthdir_y functionnality', () => {
test('An angle of 0, 360 and 720 should all return the same value', () => {
const arbitaryLength = 10
const resultOf0Angle = lengthdirY(arbitaryLength, 0)
const resultOf360Angle = lengthdirY(arbitaryLength, 360)
const resultOf720Angle = lengthdirY(arbitaryLength, 720)
const resultOf0Angle = Trigonometry.lengthdirY(arbitaryLength, 0)
const resultOf360Angle = Trigonometry.lengthdirY(arbitaryLength, 360)
const resultOf720Angle = Trigonometry.lengthdirY(arbitaryLength, 720)
expect(resultOf0Angle).toBeCloseTo(resultOf360Angle)
expect(resultOf360Angle).toBeCloseTo(resultOf720Angle)
})
Expand Down
24 changes: 13 additions & 11 deletions shared/helpers/trigonometry.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export function angleToRadians(angle: number) {
let realAngle = angle % 360
if (Math.sign(realAngle) === -1) {
realAngle = realAngle + 360
export class Trigonometry {
static angleToRadians(angle: number) {
let realAngle = angle % 360
if (Math.sign(realAngle) === -1) {
realAngle = realAngle + 360
}
return (realAngle / 180) * Math.PI
}
return (realAngle / 180) * Math.PI
}

export function lengthdirY(len: number, angle: number) {
return Math.sin(angleToRadians(angle)) * len
}
static lengthdirY(len: number, angle: number) {
return Math.sin(Trigonometry.angleToRadians(angle)) * len
}

export function lengthdirX(len: number, angle: number) {
return Math.cos(angleToRadians(angle)) * len
static lengthdirX(len: number, angle: number) {
return Math.cos(Trigonometry.angleToRadians(angle)) * len
}
}
19 changes: 19 additions & 0 deletions shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export { FormatHelper } from './helpers/FormatHelper'
export { Trigonometry } from './helpers/Trigonometry'

export { GameEventHandler } from './classes/GameEventHandler'
export { DTOConverter } from './classes/DTOConverter'
export { Vector2 } from './classes/Vector2'

export { Valid } from './decorators/Valid'

export type { KeyMap } from './types/KeyMap'

export type { EntityDTO } from './dtos/EntityDTO'
export { entityDTOSchema } from './dtos/EntityDTO.zod'
export type { PlayerDTO } from './dtos/PlayerDTO'
export { playerDTOSchema } from './dtos/PlayerDTO.zod'
export type { ProjectileDTO } from './dtos/ProjectileDTO'
export { projectileDTOSchema } from './dtos/ProjectileDTO.zod'
export type { Vector2DTO } from './dtos/Vector2DTO'
export { vector2DTOSchema } from './dtos/Vector2DTO.zod'

0 comments on commit dc41b3c

Please sign in to comment.