mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 00:23:12 +09:00
Add player position tracking
This commit is contained in:
1 parent
57f89de746
commit
047d3c22f5
8 files changed
+850
-16
No files matched your search
@@ -1,12 +1,13 @@
|
||||
import React from "react";
|
||||
import { BaseWebPlugin } from "@clusterio/web_ui";
|
||||
import CanvasMinimapPage from "./CanvasMinimapPage";
|
||||
import { TileDataEvent, ChartTagDataEvent, RecipeDataEvent } from "../messages";
|
||||
import { TileDataEvent, ChartTagDataEvent, RecipeDataEvent, PlayerPositionEvent } from "../messages";
|
||||
|
||||
export class WebPlugin extends BaseWebPlugin {
|
||||
tileUpdateCallbacks: Array<(event: TileDataEvent) => void> = [];
|
||||
chartTagUpdateCallbacks: Array<(event: ChartTagDataEvent) => void> = [];
|
||||
recipeUpdateCallbacks: Array<(event: RecipeDataEvent) => void> = [];
|
||||
playerPositionUpdateCallbacks: Array<(event: PlayerPositionEvent) => void> = [];
|
||||
|
||||
async init() {
|
||||
this.pages = [
|
||||
@@ -22,6 +23,7 @@ export class WebPlugin extends BaseWebPlugin {
|
||||
this.control.handle(TileDataEvent, this.handleTileDataEvent.bind(this));
|
||||
this.control.handle(ChartTagDataEvent, this.handleChartTagDataEvent.bind(this));
|
||||
this.control.handle(RecipeDataEvent, this.handleRecipeDataEvent.bind(this));
|
||||
this.control.handle(PlayerPositionEvent, this.handlePlayerPositionEvent.bind(this));
|
||||
}
|
||||
|
||||
async handleTileDataEvent(event: TileDataEvent) {
|
||||
@@ -32,7 +34,6 @@ export class WebPlugin extends BaseWebPlugin {
|
||||
}
|
||||
|
||||
async handleChartTagDataEvent(event: ChartTagDataEvent) {
|
||||
// Notify all registered callbacks
|
||||
for (let callback of this.chartTagUpdateCallbacks) {
|
||||
callback(event);
|
||||
}
|
||||
@@ -44,6 +45,12 @@ export class WebPlugin extends BaseWebPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
async handlePlayerPositionEvent(event: PlayerPositionEvent) {
|
||||
for (let callback of this.playerPositionUpdateCallbacks) {
|
||||
callback(event);
|
||||
}
|
||||
}
|
||||
|
||||
onTileUpdate(callback: (event: TileDataEvent) => void) {
|
||||
this.tileUpdateCallbacks.push(callback);
|
||||
}
|
||||
@@ -79,4 +86,16 @@ export class WebPlugin extends BaseWebPlugin {
|
||||
}
|
||||
this.recipeUpdateCallbacks.splice(index, 1);
|
||||
}
|
||||
|
||||
onPlayerPositionUpdate(callback: (event: PlayerPositionEvent) => void) {
|
||||
this.playerPositionUpdateCallbacks.push(callback);
|
||||
}
|
||||
|
||||
offPlayerPositionUpdate(callback: (event: PlayerPositionEvent) => void) {
|
||||
const index = this.playerPositionUpdateCallbacks.lastIndexOf(callback);
|
||||
if (index === -1) {
|
||||
throw new Error("callback is not registered");
|
||||
}
|
||||
this.playerPositionUpdateCallbacks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user