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

chore: tree type from server state #293

Merged
merged 1 commit into from
Dec 13, 2024
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
7 changes: 0 additions & 7 deletions packages/game/src/lib/baseGameAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import { createId } from '@paralleldrive/cuid2'
import { Application, Container, Rectangle, TextureStyle } from 'pixi.js'
import { BaseWagonObject } from './objects/baseWagonObject'
import { FlagObject } from './objects/flagObject'
import { TreeObject } from './objects/treeObject'
import { MoveToFlagScript } from './scripts/moveToFlagScript'
import { BaseAssetService } from './services/baseAssetService'
import { BasePlayerService } from './services/basePlayerService'
import { BaseServerService } from './services/baseServerService'
import { BaseTreeService } from './services/baseTreeService'
import { BaseWebSocketService } from './services/baseWebSocketService'
import { getRandomInRange } from './utils/random'

interface BaseGameAddonOptions {
websocketUrl: string
Expand Down Expand Up @@ -158,11 +156,6 @@ export class BaseGameAddon extends Container implements GameAddon {
this.cameraTarget = this.wagon
}
}
if (data.type === 'TREE') {
const tree = new TreeObject({ id: data.id, addon: this, x: data.x, y: this.bottomY, size: getRandomInRange(50, 100), zIndex: data?.zIndex })
this.app.stage.addChild(tree)
this.addChild(tree)
}
}

createPlayerObject(data: { id: string, x: number, zIndex?: number, telegramId: string }) {
Expand Down
5 changes: 3 additions & 2 deletions packages/game/src/lib/objects/treeObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface TreeObjectOptions {
id?: string
zIndex?: number
size?: number
treeType?: '1' | '2' | '3' | '4' | '5'
}

export class TreeObject extends BaseObject implements GameObjectTree {
Expand All @@ -23,15 +24,15 @@ export class TreeObject extends BaseObject implements GameObjectTree {
animationSlowSpeed = 0.04
animationHighSpeed = 0.5

constructor({ addon, x, y, size, id, zIndex }: TreeObjectOptions) {
constructor({ addon, x, y, size, id, zIndex, treeType }: TreeObjectOptions) {
super({ id, addon, x, y, type: 'TREE' })

this.size = size ?? 100
this.maxSize = getRandomInRange(this.minSizeToChop, this.maxSize)

this.health = 100
this.variant = 'GREEN'
this.treeType = this.getNewType()
this.treeType = treeType ?? this.getNewType()

this.zIndex = zIndex ?? getRandomInRange(-10, 1)
this.isAnObstacleToWagon = this.zIndex >= -5
Expand Down
27 changes: 22 additions & 5 deletions packages/game/src/lib/services/baseTreeService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GameAddon, TreeService } from '../types'
import type { GameAddon, GameObjectTree, TreeService } from '../types'
import { createId } from '@paralleldrive/cuid2'
import { TreeObject } from '../objects/treeObject'
import { getRandomInRange } from '../utils/random'

Expand All @@ -8,6 +9,13 @@ export class BaseTreeService implements TreeService {

constructor(readonly addon: GameAddon) {}

create(data: { id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5', size: number }) {
const tree = new TreeObject({ id: data.id, addon: this.addon, x: data.x, y: this.addon.bottomY, size: data.size, zIndex: data.zIndex })
this.addon.app.stage.addChild(tree)
this.addon.addChild(tree)
this.trees.push(tree)
}

update() {
if (!this.addon.wagon) {
return
Expand Down Expand Up @@ -55,14 +63,23 @@ export class BaseTreeService implements TreeService {
return
}

const tree = new TreeObject({ addon: this.addon, x, y: this.addon.bottomY, size: getRandomInRange(4, 8) })
this.addon.app.stage.addChild(tree)
this.addon.addChild(tree)
this.trees.push(tree)
const tree = {
id: createId(),
x,
zIndex: getRandomInRange(-10, 1),
treeType: this.getNewType(),
size: getRandomInRange(4, 8),
}
this.create(tree)

this.addon.websocketService.send({ type: 'NEW_TREE', data: { x: tree.x, id: tree.id, zIndex: tree.zIndex, treeType: tree.treeType } })
}

getNewType(): GameObjectTree['treeType'] {
const items = ['1', '2', '3', '4', '5'] as const
return items[Math.floor(Math.random() * items.length)] as GameObjectTree['treeType']
}

treesInArea(x: number, offset: number) {
return this.trees.filter((tree) => {
return tree.x > x - offset && tree.x < x + offset
Expand Down
10 changes: 8 additions & 2 deletions packages/game/src/lib/services/baseWebSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ export class BaseWebSocketService implements WebSocketService {
const { id, type, objects } = message.data

for (const obj of objects) {
if (this.addon.findObject(obj.id)) {
continue
}

if (obj.type === 'PLAYER') {
this.addon.createPlayerObject({ id: obj.id, telegramId: obj.telegramId, x: obj.x, zIndex: obj?.zIndex })
} else if (obj.type === 'TREE') {
this.addon.treeService.create({ id: obj.id, x: obj.x, zIndex: obj.zIndex, treeType: obj.treeType, size: 75 })
} else {
this.addon.createObject({ type: obj.type, id: obj.id, x: obj.x, zIndex: obj?.zIndex })
}
Expand Down Expand Up @@ -85,8 +91,8 @@ export class BaseWebSocketService implements WebSocketService {
}
}
if (message.type === 'NEW_TREE') {
const { id, x, zIndex } = message.data
this.addon.createObject({ type: 'TREE', id, x, zIndex })
const { id, x, zIndex, treeType } = message.data
this.addon.treeService.create({ id, x, zIndex, treeType, size: 8 })
}
if (message.type === 'DESTROY_TREE') {
const { id } = message.data
Expand Down
1 change: 1 addition & 0 deletions packages/game/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface AssetService {
}

export interface TreeService {
create: (data: { id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5', size: number }) => void
update: () => void
init: () => void
getNearestObstacle: (x: number) => GameObjectTree | undefined
Expand Down
Loading