mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-08 00:00:42 +00:00
add GameConfig to Game
This commit is contained in:
+25
-25
@@ -1,11 +1,11 @@
|
||||
import { Executor } from "../core/execution/ExecutionManager";
|
||||
import { Cell, MutableGame, PlayerEvent, PlayerID, MutablePlayer, TileEvent, Player, Game, UnitEvent, Tile, PlayerType, GameMap, Difficulty } from "../core/game/Game";
|
||||
import { Cell, MutableGame, PlayerEvent, PlayerID, MutablePlayer, TileEvent, Player, Game, UnitEvent, Tile, PlayerType, GameMap, Difficulty, GameType } from "../core/game/Game";
|
||||
import { createGame } from "../core/game/GameImpl";
|
||||
import { EventBus } from "../core/EventBus";
|
||||
import { Config, getConfig } from "../core/configuration/Config";
|
||||
import { createRenderer, GameRenderer } from "./graphics/GameRenderer";
|
||||
import { InputHandler, MouseUpEvent, ZoomEvent, DragEvent, MouseDownEvent } from "./InputHandler"
|
||||
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, ClientMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ServerSyncMessage, Turn } from "../core/Schemas";
|
||||
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, ClientMessageSchema, GameConfig, GameID, Intent, ServerMessage, ServerMessageSchema, ServerSyncMessage, Turn } from "../core/Schemas";
|
||||
import { loadTerrainMap, TerrainMapImpl } from "../core/game/TerrainMapLoader";
|
||||
import { and, bfs, dist, manhattanDist } from "../core/Util";
|
||||
import { WinCheckExecution } from "../core/execution/WinCheckExecution";
|
||||
@@ -17,7 +17,7 @@ import { WorkerClient } from "../core/worker/WorkerClient";
|
||||
|
||||
|
||||
export interface LobbyConfig {
|
||||
isLocal: boolean
|
||||
gameType: GameType
|
||||
playerName: () => string
|
||||
gameID: GameID
|
||||
ip: string | null
|
||||
@@ -25,19 +25,21 @@ export interface LobbyConfig {
|
||||
difficulty: Difficulty | null
|
||||
}
|
||||
|
||||
export interface GameConfig {
|
||||
map: GameMap
|
||||
difficulty: Difficulty
|
||||
clientID: ClientID,
|
||||
gameID: GameID,
|
||||
}
|
||||
|
||||
export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => void {
|
||||
const clientID = uuidv4()
|
||||
const playerID = uuidv4()
|
||||
const eventBus = new EventBus()
|
||||
const config = getConfig()
|
||||
const transport = new Transport(lobbyConfig.isLocal, eventBus, lobbyConfig.gameID, lobbyConfig.ip, clientID, playerID, config, lobbyConfig.playerName)
|
||||
const transport = new Transport(
|
||||
lobbyConfig.gameType == GameType.Singleplayer,
|
||||
eventBus,
|
||||
lobbyConfig.gameID,
|
||||
lobbyConfig.ip,
|
||||
clientID,
|
||||
playerID,
|
||||
config,
|
||||
lobbyConfig.playerName
|
||||
)
|
||||
|
||||
const onconnect = () => {
|
||||
console.log(`Joined game lobby ${lobbyConfig.gameID}`);
|
||||
@@ -48,13 +50,11 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
|
||||
console.log('lobby: game started')
|
||||
onjoin()
|
||||
const gameConfig = {
|
||||
map: message.config?.gameMap || lobbyConfig.map,
|
||||
gameMap: message.config?.gameMap || lobbyConfig.map,
|
||||
difficulty: message.config?.difficulty || lobbyConfig.difficulty,
|
||||
clientID: clientID,
|
||||
gameID: lobbyConfig.gameID,
|
||||
ip: lobbyConfig.ip,
|
||||
gameType: lobbyConfig.gameType
|
||||
}
|
||||
createClientGame(gameConfig, eventBus, transport).then(r => r.start())
|
||||
createClientGame(gameConfig, eventBus, transport, lobbyConfig.gameID, clientID).then(r => r.start())
|
||||
};
|
||||
}
|
||||
transport.connect(onconnect, onmessage)
|
||||
@@ -65,30 +65,30 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
|
||||
}
|
||||
|
||||
|
||||
export async function createClientGame(gameConfig: GameConfig, eventBus: EventBus, transport: Transport): Promise<GameRunner> {
|
||||
export async function createClientGame(gameConfig: GameConfig, eventBus: EventBus, transport: Transport, gameID: GameID, clientID: ClientID): Promise<GameRunner> {
|
||||
const config = getConfig()
|
||||
|
||||
const terrainMap = await loadTerrainMap(gameConfig.map)
|
||||
const terrainMap = await loadTerrainMap(gameConfig.gameMap)
|
||||
|
||||
let game = createGame(terrainMap, eventBus, config)
|
||||
let game = createGame(terrainMap, eventBus, config, gameConfig)
|
||||
|
||||
const worker = new WorkerClient(game, gameConfig.map)
|
||||
const worker = new WorkerClient(game, gameConfig.gameMap)
|
||||
console.log('going to init path finder')
|
||||
await worker.initialize()
|
||||
console.log('inited path finder')
|
||||
const canvas = createCanvas()
|
||||
let gameRenderer = createRenderer(canvas, game, eventBus, gameConfig.clientID)
|
||||
let gameRenderer = createRenderer(canvas, game, eventBus, clientID)
|
||||
|
||||
|
||||
console.log(`creating private game got difficulty: ${gameConfig.difficulty}`)
|
||||
|
||||
return new GameRunner(
|
||||
gameConfig,
|
||||
clientID,
|
||||
eventBus,
|
||||
game,
|
||||
gameRenderer,
|
||||
new InputHandler(canvas, eventBus),
|
||||
new Executor(game, gameConfig.difficulty, gameConfig.gameID, worker),
|
||||
new Executor(game, gameID, worker),
|
||||
transport,
|
||||
)
|
||||
}
|
||||
@@ -106,7 +106,7 @@ export class GameRunner {
|
||||
private hasJoined = false
|
||||
|
||||
constructor(
|
||||
private gameConfig: GameConfig,
|
||||
private clientID: ClientID,
|
||||
private eventBus: EventBus,
|
||||
private gs: Game,
|
||||
private renderer: GameRenderer,
|
||||
@@ -187,7 +187,7 @@ export class GameRunner {
|
||||
|
||||
private playerEvent(event: PlayerEvent) {
|
||||
console.log('received new player event!')
|
||||
if (event.player.clientID() == this.gameConfig.clientID) {
|
||||
if (event.player.clientID() == this.clientID) {
|
||||
console.log('setting name')
|
||||
this.myPlayer = event.player
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { LitElement, html, css } from 'lit';
|
||||
import { customElement, property, state } from 'lit/decorators.js';
|
||||
import { Difficulty, GameMap } from '../core/game/Game';
|
||||
import { Difficulty, GameMap, GameType } from '../core/game/Game';
|
||||
import { Lobby } from '../core/Schemas';
|
||||
|
||||
@customElement('host-lobby-modal')
|
||||
@@ -146,7 +146,7 @@ export class HostLobbyModal extends LitElement {
|
||||
}).then(() => {
|
||||
this.dispatchEvent(new CustomEvent('join-lobby', {
|
||||
detail: {
|
||||
singlePlayer: false,
|
||||
gameType: GameType.Private,
|
||||
lobby: {
|
||||
id: this.lobbyId,
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {LitElement, html, css} from 'lit';
|
||||
import {customElement, property, state, query} from 'lit/decorators.js';
|
||||
import {GameMap} from '../core/game/Game';
|
||||
import { LitElement, html, css } from 'lit';
|
||||
import { customElement, property, state, query } from 'lit/decorators.js';
|
||||
import { GameMap, GameType } from '../core/game/Game';
|
||||
|
||||
@customElement('join-private-lobby-modal')
|
||||
export class JoinPrivateLobbyModal extends LitElement {
|
||||
@@ -138,7 +138,7 @@ export class JoinPrivateLobbyModal extends LitElement {
|
||||
this.hasJoined = false
|
||||
this.message = ""
|
||||
this.dispatchEvent(new CustomEvent('leave-lobby', {
|
||||
detail: {lobby: this.lobbyIdInput.value},
|
||||
detail: { lobby: this.lobbyIdInput.value },
|
||||
bubbles: true,
|
||||
composed: true
|
||||
}));
|
||||
@@ -171,8 +171,8 @@ export class JoinPrivateLobbyModal extends LitElement {
|
||||
this.hasJoined = true
|
||||
this.dispatchEvent(new CustomEvent('join-lobby', {
|
||||
detail: {
|
||||
lobby: {id: lobbyId},
|
||||
singlePlayer: false,
|
||||
lobby: { id: lobbyId },
|
||||
gameType: GameType.Private,
|
||||
map: GameMap.World,
|
||||
},
|
||||
bubbles: true,
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ class Client {
|
||||
}
|
||||
this.gameStop = joinLobby(
|
||||
{
|
||||
isLocal: event.detail.singlePlayer,
|
||||
gameType: event.detail.gameType,
|
||||
playerName: (): string => this.usernameInput.getCurrentUsername(),
|
||||
gameID: lobby.id,
|
||||
ip: clientIP,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { LitElement, html, css } from 'lit';
|
||||
import { customElement, state } from 'lit/decorators.js';
|
||||
import { Lobby } from "../core/Schemas";
|
||||
import { Difficulty, GameMap } from '../core/game/Game';
|
||||
import { Difficulty, GameMap, GameType } from '../core/game/Game';
|
||||
|
||||
@customElement('public-lobby')
|
||||
export class PublicLobby extends LitElement {
|
||||
@@ -111,7 +111,7 @@ export class PublicLobby extends LitElement {
|
||||
this.dispatchEvent(new CustomEvent('join-lobby', {
|
||||
detail: {
|
||||
lobby: lobby,
|
||||
singlePlayer: false,
|
||||
gameType: GameType.Public,
|
||||
map: GameMap.World,
|
||||
difficulty: Difficulty.Medium,
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { LitElement, html, css } from 'lit';
|
||||
import { customElement, property, state } from 'lit/decorators.js';
|
||||
import { Difficulty, GameMap } from '../core/game/Game';
|
||||
import { Difficulty, GameMap, GameType } from '../core/game/Game';
|
||||
|
||||
@customElement('single-player-modal')
|
||||
export class SinglePlayerModal extends LitElement {
|
||||
@@ -125,7 +125,7 @@ export class SinglePlayerModal extends LitElement {
|
||||
console.log(`Starting single player game with map: ${GameMap[this.selectedMap]}`);
|
||||
this.dispatchEvent(new CustomEvent('join-lobby', {
|
||||
detail: {
|
||||
singlePlayer: true,
|
||||
gameType: GameType.Singleplayer,
|
||||
lobby: {
|
||||
id: "LOCAL",
|
||||
},
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import {GameEnv, Theme} from "../../../core/configuration/Config";
|
||||
import {EventBus} from "../../../core/EventBus";
|
||||
import {WinEvent} from "../../../core/execution/WinCheckExecution";
|
||||
import {AllianceRequest, AllianceRequestReplyEvent, Game, Player} from "../../../core/game/Game";
|
||||
import {ClientID} from "../../../core/Schemas";
|
||||
import {ContextMenuEvent} from "../../InputHandler";
|
||||
import {Layer} from "./Layer";
|
||||
import {TransformHandler} from "../TransformHandler";
|
||||
import {MessageType} from "./EventsDisplay";
|
||||
import {SendBreakAllianceIntentEvent} from "../../Transport";
|
||||
import { GameEnv, Theme } from "../../../core/configuration/Config";
|
||||
import { EventBus } from "../../../core/EventBus";
|
||||
import { WinEvent } from "../../../core/execution/WinCheckExecution";
|
||||
import { AllianceRequest, AllianceRequestReplyEvent, Game, Player } from "../../../core/game/Game";
|
||||
import { ClientID } from "../../../core/Schemas";
|
||||
import { ContextMenuEvent } from "../../InputHandler";
|
||||
import { Layer } from "./Layer";
|
||||
import { TransformHandler } from "../TransformHandler";
|
||||
import { MessageType } from "./EventsDisplay";
|
||||
import { SendBreakAllianceIntentEvent } from "../../Transport";
|
||||
|
||||
interface MenuOption {
|
||||
label: string;
|
||||
@@ -38,7 +38,7 @@ export class UILayer implements Layer {
|
||||
const barHeight = 15;
|
||||
const barBackgroundWidth = this.transformHandler.width();
|
||||
|
||||
const ratio = this.game.ticks() / this.game.config().numSpawnPhaseTurns()
|
||||
const ratio = this.game.ticks() / this.game.config().numSpawnPhaseTurns(this.game.gameConfig().gameType)
|
||||
|
||||
// Draw bar background
|
||||
context.fillStyle = 'rgba(0, 0, 0, 0.5)';
|
||||
|
||||
Reference in New Issue
Block a user