Skip to content

Commit

Permalink
chore: trees sync, energy on screen (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 authored Dec 16, 2024
1 parent 276b144 commit 9322165
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 26 deletions.
1 change: 1 addition & 0 deletions apps/telegram-game/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ declare global {
const useId: typeof import('vue')['useId']
const useLink: typeof import('vue-router')['useLink']
const useModel: typeof import('vue')['useModel']
const useRoom: typeof import('./src/composables/useRoom')['useRoom']
const useRoute: typeof import('vue-router')['useRoute']
const useRouter: typeof import('vue-router')['useRouter']
const useSlots: typeof import('vue')['useSlots']
Expand Down
Binary file added apps/telegram-game/public/energy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 23 additions & 4 deletions apps/telegram-game/src/components/Game.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<template>
<div class="absolute top-0 left-0 right-0 bottom-0 font-serif overflow-hidden select-none bg-orange-200" :class="{ hidden: !isOpened }">
<div class="absolute top-0 left-0 right-0 bottom-0 overflow-hidden select-none bg-orange-200" :class="{ hidden: !isOpened }">
<div ref="canvas" class="absolute w-full h-full bottom-10" />
<div class="absolute w-full h-35 bottom-0 bg-amber-950" />

<div class="tg-content-safe-area-top font-serif touch-pan-x absolute top-0 left-0 right-0 w-full h-16">
<div class="tg-content-safe-area-top touch-pan-x absolute top-0 left-0 right-0 w-full h-16">
<div class="max-w-[28rem] mx-auto px-5">
<div>Энергия: ??</div>
<div v-if="profile?.energy >= 0" class="w-fit px-4 py-1 flex flex-row items-center gap-1 bg-orange-100 text-amber-600 rounded-full">
<img src="/energy.png" alt="avatar" class="w-auto h-8">
<p class="text-xl font-semibold leading-none tracking-tight">
{{ profile?.energy }}
</p>
</div>

<div class="opacity-15">
<div class="hidden">
{{ game.websocketService.socket.status }} {{ roomConnected }}
</div>
</div>
Expand All @@ -20,6 +25,9 @@ import type { BaseGameAddon } from '@chat-game/game'
import { initData } from '@telegram-apps/sdk-vue'
import { gameClient, roomConnected } from '../utils/gameClient'
const { hmbanan666 } = useRoom()
const { profile } = useTelegramProfile()
const data = initData.user()
const router = useRouter()
const canvas = ref<HTMLElement>()
Expand All @@ -40,4 +48,15 @@ onMounted(async () => {
watch(router.currentRoute, (value) => {
isOpened.value = value.path === '/'
})
watch(
() => hmbanan666.value && game.value.player,
() => {
const isActiveStream = hmbanan666.value?.find((s: any) => s.service === 'HMBANAN666_TWITCH' && s.status === 'RUNNING')
if (isActiveStream && !roomConnected.value) {
game.value.websocketService.connect('12345')
roomConnected.value = '12345'
}
},
)
</script>
2 changes: 1 addition & 1 deletion apps/telegram-game/src/components/Navigation.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<nav class="bg z-50 font-serif touch-pan-x fixed bottom-0 left-0 right-0 w-full h-[100px]">
<nav class="bg z-50 touch-pan-x fixed bottom-0 left-0 right-0 w-full h-[100px]">
<div class="max-w-[28rem] mx-auto">
<div class="mt-3 grid grid-cols-5">
<button v-for="route in routes" :key="route.path" class="flex flex-col items-center justify-center gap-1 px-4 cursor-pointer" :class="{ 'button-active': router.currentRoute.value.path === route.path }" @click="handleClick(route.path)">
Expand Down
2 changes: 1 addition & 1 deletion apps/telegram-game/src/components/PageContainer.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="tg-secondary-bg tg-text tg-content-safe-area overflow-hidden z-30 h-full min-h-dvh w-full font-serif">
<div class="tg-secondary-bg tg-text tg-content-safe-area overflow-hidden z-30 h-full min-h-dvh w-full">
<div class="px-4 py-2 max-w-[28rem] mx-auto flex flex-col">
<slot />
</div>
Expand Down
11 changes: 11 additions & 0 deletions apps/telegram-game/src/composables/useRoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useFetch } from '@vueuse/core'

export function useRoom() {
const { data: hmbanan666 } = useFetch<{ service: 'HMBANAN666_TWITCH', status: 'RUNNING' | 'STOPPED' }[]>('https://chatgame.space/api/twitch/status', {
async onFetchError(ctx) {
return ctx
},
}).get().json()

return { hmbanan666 }
}
4 changes: 2 additions & 2 deletions apps/telegram-game/src/views/QuestView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{{ room.description }}
</div>

<button class="mt-4 p-3 tg-button w-full rounded-2xl" @click="connectToToom(room.roomId)">
<button class="mt-4 p-3 tg-button w-full rounded-2xl" @click="connectToRoom(room.roomId)">
Подключиться
</button>
</div>
Expand All @@ -29,7 +29,7 @@ import { gameClient, roomConnected } from '../utils/gameClient'
const router = useRouter()
function connectToToom(roomId: string) {
function connectToRoom(roomId: string) {
if (hapticFeedback.impactOccurred.isAvailable()) {
hapticFeedback.impactOccurred('light')
}
Expand Down
14 changes: 13 additions & 1 deletion apps/website/server/api/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,22 @@ export default defineWebSocketHandler({
wagon.x = parsed.data.x
}
}
if (parsed.type === 'NEW_PLAYER_TARGET') {
const player = activeRoom.objects.find((obj) => obj.type === 'PLAYER' && obj.id === parsed.data.id)
if (player) {
player.x = parsed.data.x
}
}
if (parsed.type === 'NEW_TREE') {
const tree = activeRoom.objects.find((obj) => obj.type === 'TREE' && obj.id === parsed.data.id)
if (!tree) {
activeRoom.addTree(parsed.data.id, parsed.data.x, parsed.data.zIndex, parsed.data.treeType)
activeRoom.addTree({
id: parsed.data.id,
x: parsed.data.x,
zIndex: parsed.data.zIndex,
treeType: parsed.data.treeType,
maxSize: parsed.data.maxSize,
})
}
}
if (parsed.type === 'DESTROY_TREE') {
Expand Down
12 changes: 7 additions & 5 deletions apps/website/server/core/rooms/wagon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class WagonRoom extends BaseRoom {
health: 100,
speedPerSecond: 0,
size: 75,
maxSize: getRandomInRange(75, 145),
zIndex: getRandomInRange(-10, 1),
variant: 'GREEN',
treeType: this.getRandomTreeType(),
Expand All @@ -68,18 +69,19 @@ export class WagonRoom extends BaseRoom {
})
}

addTree(id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5') {
addTree(data: { id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5', maxSize: number }) {
this.objects.push({
type: 'TREE',
id,
x,
id: data.id,
x: data.x,
state: 'IDLE',
health: 100,
speedPerSecond: 0,
size: 75,
zIndex,
maxSize: data.maxSize,
zIndex: data.zIndex,
variant: 'GREEN',
treeType,
treeType: data.treeType,
})
}

Expand Down
9 changes: 7 additions & 2 deletions packages/game/src/lib/baseGameAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class BaseGameAddon extends Container implements GameAddon {
async init(telegramId: string) {
await this.app.init({
backgroundAlpha: 0,
antialias: true,
roundPixels: true,
antialias: false,
roundPixels: false,
resolution: 1,
resizeTo: window,
})
Expand Down Expand Up @@ -99,6 +99,11 @@ export class BaseGameAddon extends Container implements GameAddon {
return
}

const isTargetAnObject = e.target.children.length === 0
if (isTargetAnObject) {
return
}

const middle = this.app.screen.width / 2
const offsetX = e.clientX - middle
const serverX = offsetX + this.leftX
Expand Down
7 changes: 4 additions & 3 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
maxSize?: number
treeType?: '1' | '2' | '3' | '4' | '5'
}

Expand All @@ -18,17 +19,17 @@ export class TreeObject extends BaseObject implements GameObjectTree {
isReadyToChop!: boolean
isAnObstacleToWagon = false
minSizeToChop = 75
maxSize = 145
maxSize: number
growSpeedPerSecond = getRandomInRange(2, 4)
animationAngle = getRandomInRange(-1, 1)
animationSlowSpeed = 0.04
animationHighSpeed = 0.5

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

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

this.health = 100
this.variant = 'GREEN'
Expand Down
7 changes: 4 additions & 3 deletions packages/game/src/lib/services/baseTreeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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, treeType: data.treeType })
create(data: { id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5', size: number, maxSize: number }) {
const tree = new TreeObject({ id: data.id, addon: this.addon, x: data.x, y: this.addon.bottomY, size: data.size, maxSize: data.maxSize, zIndex: data.zIndex, treeType: data.treeType })
this.addon.app.stage.addChild(tree)
this.addon.addChild(tree)
}
Expand Down Expand Up @@ -50,10 +50,11 @@ export class BaseTreeService implements TreeService {
zIndex: getRandomInRange(-10, 1),
treeType: this.getNewType(),
size: getRandomInRange(4, 8),
maxSize: getRandomInRange(75, 145),
}
this.create(tree)

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

getNewType(): GameObjectTree['treeType'] {
Expand Down
6 changes: 3 additions & 3 deletions packages/game/src/lib/services/baseWebSocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class BaseWebSocketService implements WebSocketService {
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 })
this.addon.treeService.create({ id: obj.id, x: obj.x, zIndex: obj.zIndex, treeType: obj.treeType, size: 75, maxSize: obj.maxSize })
} else {
this.addon.createObject({ type: obj.type, id: obj.id, x: obj.x, zIndex: obj?.zIndex })
}
Expand Down Expand Up @@ -91,8 +91,8 @@ export class BaseWebSocketService implements WebSocketService {
}
}
if (message.type === 'NEW_TREE') {
const { id, x, zIndex, treeType } = message.data
this.addon.treeService.create({ id, x, zIndex, treeType, size: 8 })
const { id, x, zIndex, treeType, maxSize } = message.data
this.addon.treeService.create({ id, x, zIndex, treeType, size: 8, maxSize })
}
if (message.type === 'DESTROY_TREE') {
const { id } = message.data
Expand Down
3 changes: 2 additions & 1 deletion packages/game/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface GameObjectTree extends GameObject {
isAnObstacleToWagon: boolean
variant: 'GREEN' | 'VIOLET' | 'STONE' | 'TEAL' | 'TOXIC' | 'BLUE'
treeType: '1' | '2' | '3' | '4' | '5'
maxSize: number
}

export interface GameObjectWagon extends GameObject {
Expand Down Expand Up @@ -124,7 +125,7 @@ export interface AssetService {
}

export interface TreeService {
create: (data: { id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5', size: number }) => void
create: (data: { id: string, x: number, zIndex: number, treeType: '1' | '2' | '3' | '4' | '5', size: number, maxSize: number }) => void
update: () => void
getNearestObstacle: (x: number) => GameObjectTree | undefined
}
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/lib/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface GameObjectTree {
type: 'TREE'
variant: 'GREEN' | 'VIOLET' | 'STONE' | 'TEAL' | 'TOXIC' | 'BLUE'
treeType: '1' | '2' | '3' | '4' | '5'
maxSize: number
}

export interface GameObjectFlag {
Expand Down Expand Up @@ -146,6 +147,7 @@ export interface WebSocketNewTree {
x: number
zIndex: number
treeType: '1' | '2' | '3' | '4' | '5'
maxSize: number
}
}

Expand Down

0 comments on commit 9322165

Please sign in to comment.