mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-06-13 13:04:03 +00:00
fixed weird asset issues we had in our fork
This commit is contained in:
parent
366a2a9f58
commit
6b643f66d4
@ -9,7 +9,7 @@ export default {
|
|||||||
// noArtificialDelays: true,
|
// noArtificialDelays: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
// Disables writing of savegames, useful for testing the same savegame over and over
|
// Disables writing of savegames, useful for testing the same savegame over and over
|
||||||
// disableSavegameWrite: true,
|
disableSavegameWrite: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
// Shows bounds of all entities
|
// Shows bounds of all entities
|
||||||
// showEntityBounds: true,
|
// showEntityBounds: true,
|
||||||
@ -33,7 +33,7 @@ export default {
|
|||||||
// allBuildingsUnlocked: true,
|
// allBuildingsUnlocked: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
// Disables cost of blueprints
|
// Disables cost of blueprints
|
||||||
// blueprintsNoCost: true,
|
blueprintsNoCost: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
// Disables cost of upgrades
|
// Disables cost of upgrades
|
||||||
// upgradesNoCost: true,
|
// upgradesNoCost: true,
|
||||||
@ -75,7 +75,7 @@ export default {
|
|||||||
// instantMiners: true,
|
// instantMiners: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
// When using fastGameEnter, controls whether a new game is started or the last one is resumed
|
// When using fastGameEnter, controls whether a new game is started or the last one is resumed
|
||||||
// resumeGameOnFastEnter: true,
|
resumeGameOnFastEnter: true,
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
// Special option used to render the trailer
|
// Special option used to render the trailer
|
||||||
// renderForTrailer: true,
|
// renderForTrailer: true,
|
||||||
|
|||||||
@ -169,7 +169,7 @@ export class ReadWriteProxy {
|
|||||||
// Check for errors during read
|
// Check for errors during read
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
if (err === FILE_NOT_FOUND) {
|
if (err === FILE_NOT_FOUND) {
|
||||||
logger.log("File not found, using default data");
|
logger.error("File not found, using default data");
|
||||||
|
|
||||||
// File not found or unreadable, assume default file
|
// File not found or unreadable, assume default file
|
||||||
return Promise.resolve(null);
|
return Promise.resolve(null);
|
||||||
|
|||||||
@ -671,6 +671,44 @@ export function smoothPulse(time) {
|
|||||||
return Math.sin(time * 4) * 0.5 + 0.5;
|
return Math.sin(time * 4) * 0.5 + 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let logIntervals = {};
|
||||||
|
const intervalStyle = "color: grey; font-style: inherit";
|
||||||
|
const keyStyle = "color: purple; font-style: italic";
|
||||||
|
const revertStyle = "color: inherit; font-style: inherit";
|
||||||
|
|
||||||
|
export function logInterval(key, frames, message, ...args) {
|
||||||
|
let interval = logIntervals[key] || 0;
|
||||||
|
if (++interval > frames) {
|
||||||
|
console.log(
|
||||||
|
`%clogInterval [%c${key}%c]: \t%c` + message,
|
||||||
|
intervalStyle,
|
||||||
|
keyStyle,
|
||||||
|
intervalStyle,
|
||||||
|
revertStyle,
|
||||||
|
...args
|
||||||
|
);
|
||||||
|
interval = 0;
|
||||||
|
}
|
||||||
|
logIntervals[key] = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dirInterval(key, frames, object, premessage, ...args) {
|
||||||
|
let interval = logIntervals[key] || 0;
|
||||||
|
if (++interval > frames) {
|
||||||
|
console.log(
|
||||||
|
`%cdirInterval [%c${key}%c]: \t%c` + (premessage || ""),
|
||||||
|
intervalStyle,
|
||||||
|
keyStyle,
|
||||||
|
intervalStyle,
|
||||||
|
revertStyle,
|
||||||
|
...args
|
||||||
|
);
|
||||||
|
console.dir(object);
|
||||||
|
interval = 0;
|
||||||
|
}
|
||||||
|
logIntervals[key] = interval;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fills in a <link> tag
|
* Fills in a <link> tag
|
||||||
* @param {string} translation
|
* @param {string} translation
|
||||||
|
|||||||
@ -565,9 +565,10 @@ export class BeltPath extends BasicSerializableObject {
|
|||||||
beltComp.assignedPath = null;
|
beltComp.assignedPath = null;
|
||||||
|
|
||||||
const entityLength = beltComp.getEffectiveLengthTiles();
|
const entityLength = beltComp.getEffectiveLengthTiles();
|
||||||
assert(this.entityPath.indexOf(entity) >= 0, "Entity not contained for split");
|
const index = this.entityPath.indexOf(entity);
|
||||||
assert(this.entityPath.indexOf(entity) !== 0, "Entity is first");
|
assert(index >= 0, "Entity not contained for split");
|
||||||
assert(this.entityPath.indexOf(entity) !== this.entityPath.length - 1, "Entity is last");
|
assert(index !== 0, "Entity is first");
|
||||||
|
assert(index !== this.entityPath.length - 1, "Entity is last");
|
||||||
|
|
||||||
let firstPathEntityCount = 0;
|
let firstPathEntityCount = 0;
|
||||||
let firstPathLength = 0;
|
let firstPathLength = 0;
|
||||||
|
|||||||
@ -57,6 +57,37 @@ export class Blueprint {
|
|||||||
return new Blueprint(newEntities);
|
return new Blueprint(newEntities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new blueprint from the given entity uids
|
||||||
|
* @param {Array<Entity>} entities
|
||||||
|
*/
|
||||||
|
static fromEntities(entities) {
|
||||||
|
const newEntities = [];
|
||||||
|
|
||||||
|
let averagePosition = new Vector();
|
||||||
|
|
||||||
|
// First, create a copy
|
||||||
|
for (let i = entities.length - 1; i >= 0; --i) {
|
||||||
|
const entity = entities[i];
|
||||||
|
|
||||||
|
const clone = entity.clone();
|
||||||
|
newEntities.push(clone);
|
||||||
|
|
||||||
|
const pos = entity.components.StaticMapEntity.getTileSpaceBounds().getCenter();
|
||||||
|
averagePosition.addInplace(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
averagePosition.divideScalarInplace(entities.length);
|
||||||
|
const blueprintOrigin = averagePosition.subScalars(0.5, 0.5).floor();
|
||||||
|
|
||||||
|
for (let i = newEntities.length - 1; i >= 0; --i) {
|
||||||
|
newEntities[i].components.StaticMapEntity.origin.subInplace(blueprintOrigin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now, make sure the origin is 0,0
|
||||||
|
return new Blueprint(newEntities);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cost of this blueprint in shapes
|
* Returns the cost of this blueprint in shapes
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -421,7 +421,7 @@ export class GameCore {
|
|||||||
this.overlayAlpha = lerp(this.overlayAlpha, desiredOverlayAlpha, 0.25);
|
this.overlayAlpha = lerp(this.overlayAlpha, desiredOverlayAlpha, 0.25);
|
||||||
|
|
||||||
// On low performance, skip the fade
|
// On low performance, skip the fade
|
||||||
if (this.root.entityMgr.entities.length > 5000 || this.root.dynamicTickrate.averageFps < 50) {
|
if (this.root.entityMgr.entities.size > 5000 || this.root.dynamicTickrate.averageFps < 50) {
|
||||||
this.overlayAlpha = desiredOverlayAlpha;
|
this.overlayAlpha = desiredOverlayAlpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,9 @@ const logger = createLogger("entity_manager");
|
|||||||
// NOTICE: We use arrayDeleteValue instead of fastArrayDeleteValue since that does not preserve the order
|
// NOTICE: We use arrayDeleteValue instead of fastArrayDeleteValue since that does not preserve the order
|
||||||
// This is slower but we need it for the street path generation
|
// This is slower but we need it for the street path generation
|
||||||
|
|
||||||
|
/** @typedef {number} EntityUid */
|
||||||
|
/** @typedef {string} ComponentId */
|
||||||
|
|
||||||
export class EntityManager extends BasicSerializableObject {
|
export class EntityManager extends BasicSerializableObject {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
super();
|
super();
|
||||||
@ -20,8 +23,14 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
/** @type {GameRoot} */
|
/** @type {GameRoot} */
|
||||||
this.root = root;
|
this.root = root;
|
||||||
|
|
||||||
/** @type {Array<Entity>} */
|
/** @type {Set<Entity>} */
|
||||||
this.entities = [];
|
this.entities = new Set();
|
||||||
|
|
||||||
|
/** @type {Map<EntityUid, Entity>} */
|
||||||
|
this.entitiesByUid = new Map();
|
||||||
|
|
||||||
|
/** @type {Map<ComponentId, Set<Entity>>} */
|
||||||
|
this.entitiesByComponent = new Map();
|
||||||
|
|
||||||
// We store a separate list with entities to destroy, since we don't destroy
|
// We store a separate list with entities to destroy, since we don't destroy
|
||||||
// them instantly
|
// them instantly
|
||||||
@ -30,8 +39,8 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
|
|
||||||
// Store a map from componentid to entities - This is used by the game system
|
// Store a map from componentid to entities - This is used by the game system
|
||||||
// for faster processing
|
// for faster processing
|
||||||
/** @type {Object.<string, Array<Entity>>} */
|
///** @type {Object.<string, Array<Entity>>} */
|
||||||
this.componentToEntity = newEmptyMap();
|
//this.componentToEntity = newEmptyMap();
|
||||||
|
|
||||||
// Store the next uid to use
|
// Store the next uid to use
|
||||||
this.nextUid = 10000;
|
this.nextUid = 10000;
|
||||||
@ -48,7 +57,7 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getStatsText() {
|
getStatsText() {
|
||||||
return this.entities.length + " entities [" + this.destroyList.length + " to kill]";
|
return this.entities.size + " entities [" + this.destroyList.length + " to kill]";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main update
|
// Main update
|
||||||
@ -56,6 +65,19 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
this.processDestroyList();
|
this.processDestroyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @param {ComponentId} componentId
|
||||||
|
*/
|
||||||
|
addToComponentMap(entity, componentId) {
|
||||||
|
let set;
|
||||||
|
if ((set = this.entitiesByComponent.get(componentId))) {
|
||||||
|
set.add(entity);
|
||||||
|
} else {
|
||||||
|
this.entitiesByComponent.set(componentId, new Set([entity]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a new entity
|
* Registers a new entity
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
@ -63,7 +85,7 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
*/
|
*/
|
||||||
registerEntity(entity, uid = null) {
|
registerEntity(entity, uid = null) {
|
||||||
if (G_IS_DEV && !globalConfig.debug.disableSlowAsserts) {
|
if (G_IS_DEV && !globalConfig.debug.disableSlowAsserts) {
|
||||||
assert(this.entities.indexOf(entity) < 0, `RegisterEntity() called twice for entity ${entity}`);
|
assert(!this.entities.has(entity), `RegisterEntity() called twice for entity ${entity}`);
|
||||||
}
|
}
|
||||||
assert(!entity.destroyed, `Attempting to register destroyed entity ${entity}`);
|
assert(!entity.destroyed, `Attempting to register destroyed entity ${entity}`);
|
||||||
|
|
||||||
@ -72,21 +94,17 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
assert(uid >= 0 && uid < Number.MAX_SAFE_INTEGER, "Invalid uid passed: " + uid);
|
assert(uid >= 0 && uid < Number.MAX_SAFE_INTEGER, "Invalid uid passed: " + uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.entities.push(entity);
|
// Give each entity a unique id
|
||||||
|
entity.uid = uid || this.generateUid();
|
||||||
|
|
||||||
|
this.entities.add(entity);
|
||||||
|
this.entitiesByUid.set(uid, entity);
|
||||||
|
|
||||||
// Register into the componentToEntity map
|
// Register into the componentToEntity map
|
||||||
for (const componentId in entity.components) {
|
for (const componentId in entity.components) {
|
||||||
if (entity.components[componentId]) {
|
this.addToComponentMap(entity, componentId);
|
||||||
if (this.componentToEntity[componentId]) {
|
|
||||||
this.componentToEntity[componentId].push(entity);
|
|
||||||
} else {
|
|
||||||
this.componentToEntity[componentId] = [entity];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give each entity a unique id
|
|
||||||
entity.uid = uid ? uid : this.generateUid();
|
|
||||||
entity.registered = true;
|
entity.registered = true;
|
||||||
|
|
||||||
this.root.signals.entityAdded.dispatch(entity);
|
this.root.signals.entityAdded.dispatch(entity);
|
||||||
@ -108,11 +126,8 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
attachDynamicComponent(entity, component) {
|
attachDynamicComponent(entity, component) {
|
||||||
entity.addComponent(component, true);
|
entity.addComponent(component, true);
|
||||||
const componentId = /** @type {typeof Component} */ (component.constructor).getId();
|
const componentId = /** @type {typeof Component} */ (component.constructor).getId();
|
||||||
if (this.componentToEntity[componentId]) {
|
|
||||||
this.componentToEntity[componentId].push(entity);
|
this.addToComponentMap(entity, componentId);
|
||||||
} else {
|
|
||||||
this.componentToEntity[componentId] = [entity];
|
|
||||||
}
|
|
||||||
this.root.signals.entityGotNewComponent.dispatch(entity);
|
this.root.signals.entityGotNewComponent.dispatch(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +140,7 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
entity.removeComponent(component, true);
|
entity.removeComponent(component, true);
|
||||||
const componentId = /** @type {typeof Component} */ (component.constructor).getId();
|
const componentId = /** @type {typeof Component} */ (component.constructor).getId();
|
||||||
|
|
||||||
fastArrayDeleteValue(this.componentToEntity[componentId], entity);
|
this.entitiesByComponent.get(componentId).delete(entity);
|
||||||
this.root.signals.entityComponentRemoved.dispatch(entity);
|
this.root.signals.entityComponentRemoved.dispatch(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,18 +151,15 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
* @returns {Entity}
|
* @returns {Entity}
|
||||||
*/
|
*/
|
||||||
findByUid(uid, errorWhenNotFound = true) {
|
findByUid(uid, errorWhenNotFound = true) {
|
||||||
const arr = this.entities;
|
const entity = this.entitiesByUid.get(uid);
|
||||||
for (let i = 0, len = arr.length; i < len; ++i) {
|
if (entity) {
|
||||||
const entity = arr[i];
|
if (entity.queuedForDestroy || entity.destroyed) {
|
||||||
if (entity.uid === uid) {
|
if (errorWhenNotFound) {
|
||||||
if (entity.queuedForDestroy || entity.destroyed) {
|
logger.warn("Entity with UID", uid, "not found (destroyed)");
|
||||||
if (errorWhenNotFound) {
|
|
||||||
logger.warn("Entity with UID", uid, "not found (destroyed)");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
return entity;
|
return null;
|
||||||
}
|
}
|
||||||
|
return entity;
|
||||||
}
|
}
|
||||||
if (errorWhenNotFound) {
|
if (errorWhenNotFound) {
|
||||||
logger.warn("Entity with UID", uid, "not found");
|
logger.warn("Entity with UID", uid, "not found");
|
||||||
@ -162,15 +174,7 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
* @returns {Map<number, Entity>}
|
* @returns {Map<number, Entity>}
|
||||||
*/
|
*/
|
||||||
getFrozenUidSearchMap() {
|
getFrozenUidSearchMap() {
|
||||||
const result = new Map();
|
return this.entitiesByUid;
|
||||||
const array = this.entities;
|
|
||||||
for (let i = 0, len = array.length; i < len; ++i) {
|
|
||||||
const entity = array[i];
|
|
||||||
if (!entity.queuedForDestroy && !entity.destroyed) {
|
|
||||||
result.set(entity.uid, entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -179,7 +183,9 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
* @returns {Array<Entity>} entities
|
* @returns {Array<Entity>} entities
|
||||||
*/
|
*/
|
||||||
getAllWithComponent(componentHandle) {
|
getAllWithComponent(componentHandle) {
|
||||||
return this.componentToEntity[componentHandle.getId()] || [];
|
const set = this.entitiesByComponent.get(componentHandle.getId());
|
||||||
|
if (!set) return [];
|
||||||
|
else return [...set.values()];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -188,20 +194,20 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
*/
|
*/
|
||||||
unregisterEntityComponents(entity) {
|
unregisterEntityComponents(entity) {
|
||||||
for (const componentId in entity.components) {
|
for (const componentId in entity.components) {
|
||||||
if (entity.components[componentId]) {
|
const set = this.entitiesByComponent.get(componentId);
|
||||||
arrayDeleteValue(this.componentToEntity[componentId], entity);
|
if (set) set.delete(entity);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Processes the entities to destroy and actually destroys them
|
// Processes the entities to destroy and actually destroys them
|
||||||
/* eslint-disable max-statements */
|
/* eslint-disable max-statements */
|
||||||
processDestroyList() {
|
processDestroyList() {
|
||||||
for (let i = 0; i < this.destroyList.length; ++i) {
|
for (let i = this.destroyList.length - 1; i >= 0; --i) {
|
||||||
const entity = this.destroyList[i];
|
const entity = this.destroyList[i];
|
||||||
|
|
||||||
// Remove from entities list
|
// Remove from entities list
|
||||||
arrayDeleteValue(this.entities, entity);
|
this.entities.delete(entity);
|
||||||
|
this.entitiesByUid.delete(entity.uid);
|
||||||
|
|
||||||
// Remove from componentToEntity list
|
// Remove from componentToEntity list
|
||||||
this.unregisterEntityComponents(entity);
|
this.unregisterEntityComponents(entity);
|
||||||
@ -230,12 +236,8 @@ export class EntityManager extends BasicSerializableObject {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.destroyList.indexOf(entity) < 0) {
|
this.destroyList.push(entity);
|
||||||
this.destroyList.push(entity);
|
entity.queuedForDestroy = true;
|
||||||
entity.queuedForDestroy = true;
|
this.root.signals.entityQueuedForDestroy.dispatch(entity);
|
||||||
this.root.signals.entityQueuedForDestroy.dispatch(entity);
|
|
||||||
} else {
|
|
||||||
assert(false, "Trying to destroy entity twice");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,137 +1,142 @@
|
|||||||
/* typehints:start */
|
/* typehints:start */
|
||||||
import { Component } from "./component";
|
import { Component } from "./component";
|
||||||
import { Entity } from "./entity";
|
import { Entity } from "./entity";
|
||||||
/* typehints:end */
|
/* typehints:end */
|
||||||
|
|
||||||
import { GameRoot } from "./root";
|
import { GameRoot } from "./root";
|
||||||
import { GameSystem } from "./game_system";
|
import { GameSystem } from "./game_system";
|
||||||
import { arrayDelete, arrayDeleteValue } from "../core/utils";
|
import { arrayDelete, arrayDeleteValue, fastArrayDelete } from "../core/utils";
|
||||||
import { globalConfig } from "../core/config";
|
|
||||||
|
export class GameSystemWithFilter extends GameSystem {
|
||||||
export class GameSystemWithFilter extends GameSystem {
|
/**
|
||||||
/**
|
* Constructs a new game system with the given component filter. It will process
|
||||||
* Constructs a new game system with the given component filter. It will process
|
* all entities which have *all* of the passed components
|
||||||
* all entities which have *all* of the passed components
|
* @param {GameRoot} root
|
||||||
* @param {GameRoot} root
|
* @param {Array<typeof Component>} requiredComponents
|
||||||
* @param {Array<typeof Component>} requiredComponents
|
*/
|
||||||
*/
|
constructor(root, requiredComponents) {
|
||||||
constructor(root, requiredComponents) {
|
super(root);
|
||||||
super(root);
|
this.requiredComponents = requiredComponents;
|
||||||
this.requiredComponents = requiredComponents;
|
this.requiredComponentIds = requiredComponents.map(component => component.getId());
|
||||||
this.requiredComponentIds = requiredComponents.map(component => component.getId());
|
|
||||||
|
/**
|
||||||
/**
|
* All entities which match the current components
|
||||||
* All entities which match the current components
|
* @type {Set<Entity>}
|
||||||
* @type {Array<Entity>}
|
*/
|
||||||
*/
|
this.allEntitiesSet = new Set();
|
||||||
this.allEntities = [];
|
this.allEntitiesArray = [];
|
||||||
|
this.allEntitiesArrayIsOutdated = true;
|
||||||
this.root.signals.entityAdded.add(this.internalPushEntityIfMatching, this);
|
this.entitiesQueuedToDelete = [];
|
||||||
this.root.signals.entityGotNewComponent.add(this.internalReconsiderEntityToAdd, this);
|
|
||||||
this.root.signals.entityComponentRemoved.add(this.internalCheckEntityAfterComponentRemoval, this);
|
this.root.signals.entityAdded.add(this.internalPushEntityIfMatching, this);
|
||||||
this.root.signals.entityQueuedForDestroy.add(this.internalPopEntityIfMatching, this);
|
this.root.signals.entityGotNewComponent.add(this.internalReconsiderEntityToAdd, this);
|
||||||
|
this.root.signals.entityComponentRemoved.add(this.internalCheckEntityAfterComponentRemoval, this);
|
||||||
this.root.signals.postLoadHook.add(this.internalPostLoadHook, this);
|
this.root.signals.entityQueuedForDestroy.add(this.internalPopEntityIfMatching, this);
|
||||||
this.root.signals.bulkOperationFinished.add(this.refreshCaches, this);
|
|
||||||
}
|
this.root.signals.postLoadHook.add(this.internalPostLoadHook, this);
|
||||||
|
this.root.signals.bulkOperationFinished.add(this.refreshCaches, this);
|
||||||
/**
|
}
|
||||||
* @param {Entity} entity
|
|
||||||
*/
|
tryUpdateEntitiesArray() {
|
||||||
internalPushEntityIfMatching(entity) {
|
if (this.allEntitiesArrayIsOutdated) {
|
||||||
for (let i = 0; i < this.requiredComponentIds.length; ++i) {
|
this.allEntitiesArray = [...this.allEntitiesSet.values()];
|
||||||
if (!entity.components[this.requiredComponentIds[i]]) {
|
this.allEntitiesArrayIsOutdated = false;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
// This is slow!
|
* @param {Entity} entity
|
||||||
if (G_IS_DEV && !globalConfig.debug.disableSlowAsserts) {
|
*/
|
||||||
assert(this.allEntities.indexOf(entity) < 0, "entity already in list: " + entity);
|
internalPushEntityIfMatching(entity) {
|
||||||
}
|
for (let i = 0; i < this.requiredComponentIds.length; ++i) {
|
||||||
|
if (!entity.components[this.requiredComponentIds[i]]) {
|
||||||
this.internalRegisterEntity(entity);
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
*
|
assert(!this.allEntitiesSet.has(entity), "entity already in list: " + entity);
|
||||||
* @param {Entity} entity
|
this.internalRegisterEntity(entity);
|
||||||
*/
|
}
|
||||||
internalCheckEntityAfterComponentRemoval(entity) {
|
|
||||||
if (this.allEntities.indexOf(entity) < 0) {
|
/**
|
||||||
// Entity wasn't interesting anyways
|
*
|
||||||
return;
|
* @param {Entity} entity
|
||||||
}
|
*/
|
||||||
|
internalCheckEntityAfterComponentRemoval(entity) {
|
||||||
for (let i = 0; i < this.requiredComponentIds.length; ++i) {
|
if (!this.allEntitiesSet.has(entity)) {
|
||||||
if (!entity.components[this.requiredComponentIds[i]]) {
|
// Entity wasn't interesting anyways
|
||||||
// Entity is not interesting anymore
|
return;
|
||||||
arrayDeleteValue(this.allEntities, entity);
|
}
|
||||||
}
|
|
||||||
}
|
for (let i = 0; i < this.requiredComponentIds.length; ++i) {
|
||||||
}
|
if (!entity.components[this.requiredComponentIds[i]]) {
|
||||||
|
// Entity is not interesting anymore
|
||||||
/**
|
//arrayDeleteValue(this.allEntities, entity);
|
||||||
*
|
this.allEntitiesArrayIsOutdated = this.allEntitiesSet.delete(entity);
|
||||||
* @param {Entity} entity
|
}
|
||||||
*/
|
}
|
||||||
internalReconsiderEntityToAdd(entity) {
|
}
|
||||||
for (let i = 0; i < this.requiredComponentIds.length; ++i) {
|
|
||||||
if (!entity.components[this.requiredComponentIds[i]]) {
|
/**
|
||||||
return;
|
*
|
||||||
}
|
* @param {Entity} entity
|
||||||
}
|
*/
|
||||||
if (this.allEntities.indexOf(entity) >= 0) {
|
internalReconsiderEntityToAdd(entity) {
|
||||||
return;
|
for (let i = 0; i < this.requiredComponentIds.length; ++i) {
|
||||||
}
|
if (!entity.components[this.requiredComponentIds[i]]) {
|
||||||
this.internalRegisterEntity(entity);
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
refreshCaches() {
|
if (this.allEntitiesSet.has(entity)) {
|
||||||
// Remove all entities which are queued for destroy
|
return;
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
}
|
||||||
const entity = this.allEntities[i];
|
this.internalRegisterEntity(entity);
|
||||||
if (entity.queuedForDestroy || entity.destroyed) {
|
}
|
||||||
this.allEntities.splice(i, 1);
|
|
||||||
i -= 1;
|
refreshCaches() {
|
||||||
}
|
//this.allEntities.sort((a, b) => a.uid - b.uid);
|
||||||
}
|
// Remove all entities which are queued for destroy
|
||||||
|
if (this.entitiesQueuedToDelete.length > 0) {
|
||||||
this.allEntities.sort((a, b) => a.uid - b.uid);
|
for (let i = this.entitiesQueuedToDelete.length - 1; i >= 0; --i) {
|
||||||
}
|
this.allEntitiesSet.delete(this.entitiesQueuedToDelete[i]);
|
||||||
|
}
|
||||||
/**
|
this.entitiesQueuedToDelete = [];
|
||||||
* Recomputes all target entities after the game has loaded
|
}
|
||||||
*/
|
|
||||||
internalPostLoadHook() {
|
// called here in case a delete executed mid frame
|
||||||
this.refreshCaches();
|
this.tryUpdateEntitiesArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Recomputes all target entities after the game has loaded
|
||||||
* @param {Entity} entity
|
*/
|
||||||
*/
|
internalPostLoadHook() {
|
||||||
internalRegisterEntity(entity) {
|
this.refreshCaches();
|
||||||
this.allEntities.push(entity);
|
}
|
||||||
|
|
||||||
if (this.root.gameInitialized && !this.root.bulkOperationRunning) {
|
/**
|
||||||
// Sort entities by uid so behaviour is predictable
|
*
|
||||||
this.allEntities.sort((a, b) => a.uid - b.uid);
|
* @param {Entity} entity
|
||||||
}
|
*/
|
||||||
}
|
internalRegisterEntity(entity) {
|
||||||
|
this.allEntitiesSet.add(entity);
|
||||||
/**
|
this.allEntitiesArray.push(entity);
|
||||||
*
|
|
||||||
* @param {Entity} entity
|
// if (this.root.gameInitialized && !this.root.bulkOperationRunning) {
|
||||||
*/
|
// // Sort entities by uid so behaviour is predictable
|
||||||
internalPopEntityIfMatching(entity) {
|
// this.allEntities.sort((a, b) => a.uid - b.uid);
|
||||||
if (this.root.bulkOperationRunning) {
|
// }
|
||||||
// We do this in refreshCaches afterwards
|
}
|
||||||
return;
|
|
||||||
}
|
/**
|
||||||
const index = this.allEntities.indexOf(entity);
|
*
|
||||||
if (index >= 0) {
|
* @param {Entity} entity
|
||||||
arrayDelete(this.allEntities, index);
|
*/
|
||||||
}
|
internalPopEntityIfMatching(entity) {
|
||||||
}
|
if (this.root.bulkOperationRunning) {
|
||||||
}
|
this.entitiesQueuedToDelete.push(entity);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.allEntitiesArrayIsOutdated = this.allEntitiesSet.delete(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -44,6 +44,7 @@ import { HUDWireInfo } from "./parts/wire_info";
|
|||||||
import { HUDLeverToggle } from "./parts/lever_toggle";
|
import { HUDLeverToggle } from "./parts/lever_toggle";
|
||||||
import { HUDLayerPreview } from "./parts/layer_preview";
|
import { HUDLayerPreview } from "./parts/layer_preview";
|
||||||
import { HUDMinerHighlight } from "./parts/miner_highlight";
|
import { HUDMinerHighlight } from "./parts/miner_highlight";
|
||||||
|
import { Entity } from "../entity";
|
||||||
import { HUDBetaOverlay } from "./parts/beta_overlay";
|
import { HUDBetaOverlay } from "./parts/beta_overlay";
|
||||||
import { HUDStandaloneAdvantages } from "./parts/standalone_advantages";
|
import { HUDStandaloneAdvantages } from "./parts/standalone_advantages";
|
||||||
import { HUDCatMemes } from "./parts/cat_memes";
|
import { HUDCatMemes } from "./parts/cat_memes";
|
||||||
|
|||||||
@ -1,203 +1,204 @@
|
|||||||
import { DrawParameters } from "../../../core/draw_parameters";
|
import { DrawParameters } from "../../../core/draw_parameters";
|
||||||
import { STOP_PROPAGATION } from "../../../core/signal";
|
import { STOP_PROPAGATION } from "../../../core/signal";
|
||||||
import { TrackedState } from "../../../core/tracked_state";
|
import { TrackedState } from "../../../core/tracked_state";
|
||||||
import { makeDiv } from "../../../core/utils";
|
import { makeDiv } from "../../../core/utils";
|
||||||
import { Vector } from "../../../core/vector";
|
import { Vector } from "../../../core/vector";
|
||||||
import { SOUNDS } from "../../../platform/sound";
|
import { T } from "../../../translations";
|
||||||
import { T } from "../../../translations";
|
import { enumMouseButton } from "../../camera";
|
||||||
import { Blueprint } from "../../blueprint";
|
import { KEYMAPPINGS } from "../../key_action_mapper";
|
||||||
import { enumMouseButton } from "../../camera";
|
import { BaseHUDPart } from "../base_hud_part";
|
||||||
import { KEYMAPPINGS } from "../../key_action_mapper";
|
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
||||||
import { BaseHUDPart } from "../base_hud_part";
|
import { Blueprint } from "../../blueprint";
|
||||||
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
import { SOUNDS } from "../../../platform/sound";
|
||||||
|
import { Entity } from "../../entity";
|
||||||
export class HUDBlueprintPlacer extends BaseHUDPart {
|
|
||||||
createElements(parent) {
|
export class HUDBlueprintPlacer extends BaseHUDPart {
|
||||||
const blueprintCostShape = this.root.shapeDefinitionMgr.getShapeFromShortKey(
|
createElements(parent) {
|
||||||
this.root.gameMode.getBlueprintShapeKey()
|
const blueprintCostShape = this.root.shapeDefinitionMgr.getShapeFromShortKey(
|
||||||
);
|
this.root.gameMode.getBlueprintShapeKey()
|
||||||
const blueprintCostShapeCanvas = blueprintCostShape.generateAsCanvas(80);
|
);
|
||||||
|
const blueprintCostShapeCanvas = blueprintCostShape.generateAsCanvas(80);
|
||||||
this.costDisplayParent = makeDiv(parent, "ingame_HUD_BlueprintPlacer", [], ``);
|
|
||||||
|
this.costDisplayParent = makeDiv(parent, "ingame_HUD_BlueprintPlacer", [], ``);
|
||||||
makeDiv(this.costDisplayParent, null, ["label"], T.ingame.blueprintPlacer.cost);
|
|
||||||
const costContainer = makeDiv(this.costDisplayParent, null, ["costContainer"], "");
|
makeDiv(this.costDisplayParent, null, ["label"], T.ingame.blueprintPlacer.cost);
|
||||||
this.costDisplayText = makeDiv(costContainer, null, ["costText"], "");
|
const costContainer = makeDiv(this.costDisplayParent, null, ["costContainer"], "");
|
||||||
costContainer.appendChild(blueprintCostShapeCanvas);
|
this.costDisplayText = makeDiv(costContainer, null, ["costText"], "");
|
||||||
}
|
costContainer.appendChild(blueprintCostShapeCanvas);
|
||||||
|
}
|
||||||
initialize() {
|
|
||||||
this.root.hud.signals.buildingsSelectedForCopy.add(this.createBlueprintFromBuildings, this);
|
initialize() {
|
||||||
|
this.root.hud.signals.buildingsSelectedForCopy.add(this.createBlueprintFromBuildings, this);
|
||||||
/** @type {TypedTrackedState<Blueprint?>} */
|
|
||||||
this.currentBlueprint = new TrackedState(this.onBlueprintChanged, this);
|
/** @type {TypedTrackedState<Blueprint?>} */
|
||||||
/** @type {Blueprint?} */
|
this.currentBlueprint = new TrackedState(this.onBlueprintChanged, this);
|
||||||
this.lastBlueprintUsed = null;
|
/** @type {Blueprint?} */
|
||||||
|
this.lastBlueprintUsed = null;
|
||||||
const keyActionMapper = this.root.keyMapper;
|
|
||||||
keyActionMapper.getBinding(KEYMAPPINGS.general.back).add(this.abortPlacement, this);
|
const keyActionMapper = this.root.keyMapper;
|
||||||
keyActionMapper.getBinding(KEYMAPPINGS.placement.pipette).add(this.abortPlacement, this);
|
keyActionMapper.getBinding(KEYMAPPINGS.general.back).add(this.abortPlacement, this);
|
||||||
keyActionMapper.getBinding(KEYMAPPINGS.placement.rotateWhilePlacing).add(this.rotateBlueprint, this);
|
keyActionMapper.getBinding(KEYMAPPINGS.placement.pipette).add(this.abortPlacement, this);
|
||||||
keyActionMapper.getBinding(KEYMAPPINGS.massSelect.pasteLastBlueprint).add(this.pasteBlueprint, this);
|
keyActionMapper.getBinding(KEYMAPPINGS.placement.rotateWhilePlacing).add(this.rotateBlueprint, this);
|
||||||
|
keyActionMapper.getBinding(KEYMAPPINGS.massSelect.pasteLastBlueprint).add(this.pasteBlueprint, this);
|
||||||
this.root.camera.downPreHandler.add(this.onMouseDown, this);
|
|
||||||
this.root.camera.movePreHandler.add(this.onMouseMove, this);
|
this.root.camera.downPreHandler.add(this.onMouseDown, this);
|
||||||
|
this.root.camera.movePreHandler.add(this.onMouseMove, this);
|
||||||
this.root.hud.signals.selectedPlacementBuildingChanged.add(this.abortPlacement, this);
|
|
||||||
this.root.signals.editModeChanged.add(this.onEditModeChanged, this);
|
this.root.hud.signals.selectedPlacementBuildingChanged.add(this.abortPlacement, this);
|
||||||
|
this.root.signals.editModeChanged.add(this.onEditModeChanged, this);
|
||||||
this.domAttach = new DynamicDomAttach(this.root, this.costDisplayParent);
|
|
||||||
this.trackedCanAfford = new TrackedState(this.onCanAffordChanged, this);
|
this.domAttach = new DynamicDomAttach(this.root, this.costDisplayParent);
|
||||||
}
|
this.trackedCanAfford = new TrackedState(this.onCanAffordChanged, this);
|
||||||
|
}
|
||||||
abortPlacement() {
|
|
||||||
if (this.currentBlueprint.get()) {
|
abortPlacement() {
|
||||||
this.currentBlueprint.set(null);
|
if (this.currentBlueprint.get()) {
|
||||||
|
this.currentBlueprint.set(null);
|
||||||
return STOP_PROPAGATION;
|
|
||||||
}
|
return STOP_PROPAGATION;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Called when the layer was changed
|
/**
|
||||||
* @param {Layer} layer
|
* Called when the layer was changed
|
||||||
*/
|
* @param {Layer} layer
|
||||||
onEditModeChanged(layer) {
|
*/
|
||||||
// Check if the layer of the blueprint differs and thus we have to deselect it
|
onEditModeChanged(layer) {
|
||||||
const blueprint = this.currentBlueprint.get();
|
// Check if the layer of the blueprint differs and thus we have to deselect it
|
||||||
if (blueprint) {
|
const blueprint = this.currentBlueprint.get();
|
||||||
if (blueprint.layer !== layer) {
|
if (blueprint) {
|
||||||
this.currentBlueprint.set(null);
|
if (blueprint.layer !== layer) {
|
||||||
}
|
this.currentBlueprint.set(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Called when the blueprint is now affordable or not
|
/**
|
||||||
* @param {boolean} canAfford
|
* Called when the blueprint is now affordable or not
|
||||||
*/
|
* @param {boolean} canAfford
|
||||||
onCanAffordChanged(canAfford) {
|
*/
|
||||||
this.costDisplayParent.classList.toggle("canAfford", canAfford);
|
onCanAffordChanged(canAfford) {
|
||||||
}
|
this.costDisplayParent.classList.toggle("canAfford", canAfford);
|
||||||
|
}
|
||||||
update() {
|
|
||||||
const currentBlueprint = this.currentBlueprint.get();
|
update() {
|
||||||
this.domAttach.update(currentBlueprint && currentBlueprint.getCost() > 0);
|
const currentBlueprint = this.currentBlueprint.get();
|
||||||
this.trackedCanAfford.set(currentBlueprint && currentBlueprint.canAfford(this.root));
|
this.domAttach.update(currentBlueprint && currentBlueprint.getCost() > 0);
|
||||||
}
|
this.trackedCanAfford.set(currentBlueprint && currentBlueprint.canAfford(this.root));
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Called when the blueprint was changed
|
/**
|
||||||
* @param {Blueprint} blueprint
|
* Called when the blueprint was changed
|
||||||
*/
|
* @param {Blueprint} blueprint
|
||||||
onBlueprintChanged(blueprint) {
|
*/
|
||||||
if (blueprint) {
|
onBlueprintChanged(blueprint) {
|
||||||
this.lastBlueprintUsed = blueprint;
|
if (blueprint) {
|
||||||
this.costDisplayText.innerText = "" + blueprint.getCost();
|
this.lastBlueprintUsed = blueprint;
|
||||||
}
|
this.costDisplayText.innerText = "" + blueprint.getCost();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* mouse down pre handler
|
/**
|
||||||
* @param {Vector} pos
|
* mouse down pre handler
|
||||||
* @param {enumMouseButton} button
|
* @param {Vector} pos
|
||||||
*/
|
* @param {enumMouseButton} button
|
||||||
onMouseDown(pos, button) {
|
*/
|
||||||
if (button === enumMouseButton.right) {
|
onMouseDown(pos, button) {
|
||||||
if (this.currentBlueprint.get()) {
|
if (button === enumMouseButton.right) {
|
||||||
this.abortPlacement();
|
if (this.currentBlueprint.get()) {
|
||||||
return STOP_PROPAGATION;
|
this.abortPlacement();
|
||||||
}
|
return STOP_PROPAGATION;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const blueprint = this.currentBlueprint.get();
|
|
||||||
if (!blueprint) {
|
const blueprint = this.currentBlueprint.get();
|
||||||
return;
|
if (!blueprint) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
if (!blueprint.canAfford(this.root)) {
|
|
||||||
this.root.soundProxy.playUiError();
|
if (!blueprint.canAfford(this.root)) {
|
||||||
return;
|
this.root.soundProxy.playUiError();
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
const worldPos = this.root.camera.screenToWorld(pos);
|
|
||||||
const tile = worldPos.toTileSpace();
|
const worldPos = this.root.camera.screenToWorld(pos);
|
||||||
if (blueprint.tryPlace(this.root, tile)) {
|
const tile = worldPos.toTileSpace();
|
||||||
const cost = blueprint.getCost();
|
if (blueprint.tryPlace(this.root, tile)) {
|
||||||
this.root.hubGoals.takeShapeByKey(this.root.gameMode.getBlueprintShapeKey(), cost);
|
const cost = blueprint.getCost();
|
||||||
this.root.soundProxy.playUi(SOUNDS.placeBuilding);
|
this.root.hubGoals.takeShapeByKey(this.root.gameMode.getBlueprintShapeKey(), cost);
|
||||||
}
|
this.root.soundProxy.playUi(SOUNDS.placeBuilding);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Mose move handler
|
/**
|
||||||
*/
|
* Mose move handler
|
||||||
onMouseMove() {
|
*/
|
||||||
// Prevent movement while blueprint is selected
|
onMouseMove() {
|
||||||
if (this.currentBlueprint.get()) {
|
// Prevent movement while blueprint is selected
|
||||||
return STOP_PROPAGATION;
|
if (this.currentBlueprint.get()) {
|
||||||
}
|
return STOP_PROPAGATION;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Called when an array of bulidings was selected
|
/**
|
||||||
* @param {Array<number>} uids
|
* Called when an array of bulidings was selected
|
||||||
*/
|
* @param {Array<Entity>} entities
|
||||||
createBlueprintFromBuildings(uids) {
|
*/
|
||||||
if (uids.length === 0) {
|
createBlueprintFromBuildings(entities) {
|
||||||
return;
|
if (entities.length === 0) {
|
||||||
}
|
return;
|
||||||
this.currentBlueprint.set(Blueprint.fromUids(this.root, uids));
|
}
|
||||||
}
|
this.currentBlueprint.set(Blueprint.fromEntities(entities));
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Attempts to rotate the current blueprint
|
/**
|
||||||
*/
|
* Attempts to rotate the current blueprint
|
||||||
rotateBlueprint() {
|
*/
|
||||||
if (this.currentBlueprint.get()) {
|
rotateBlueprint() {
|
||||||
if (this.root.keyMapper.getBinding(KEYMAPPINGS.placement.rotateInverseModifier).pressed) {
|
if (this.currentBlueprint.get()) {
|
||||||
this.currentBlueprint.get().rotateCcw();
|
if (this.root.keyMapper.getBinding(KEYMAPPINGS.placement.rotateInverseModifier).pressed) {
|
||||||
} else {
|
this.currentBlueprint.get().rotateCcw();
|
||||||
this.currentBlueprint.get().rotateCw();
|
} else {
|
||||||
}
|
this.currentBlueprint.get().rotateCw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Attempts to paste the last blueprint
|
/**
|
||||||
*/
|
* Attempts to paste the last blueprint
|
||||||
pasteBlueprint() {
|
*/
|
||||||
if (this.lastBlueprintUsed !== null) {
|
pasteBlueprint() {
|
||||||
if (this.lastBlueprintUsed.layer !== this.root.currentLayer) {
|
if (this.lastBlueprintUsed !== null) {
|
||||||
// Not compatible
|
if (this.lastBlueprintUsed.layer !== this.root.currentLayer) {
|
||||||
this.root.soundProxy.playUiError();
|
// Not compatible
|
||||||
return;
|
this.root.soundProxy.playUiError();
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
this.root.hud.signals.pasteBlueprintRequested.dispatch();
|
|
||||||
this.currentBlueprint.set(this.lastBlueprintUsed);
|
this.root.hud.signals.pasteBlueprintRequested.dispatch();
|
||||||
} else {
|
this.currentBlueprint.set(this.lastBlueprintUsed);
|
||||||
this.root.soundProxy.playUiError();
|
} else {
|
||||||
}
|
this.root.soundProxy.playUiError();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
*
|
/**
|
||||||
* @param {DrawParameters} parameters
|
*
|
||||||
*/
|
* @param {DrawParameters} parameters
|
||||||
draw(parameters) {
|
*/
|
||||||
const blueprint = this.currentBlueprint.get();
|
draw(parameters) {
|
||||||
if (!blueprint) {
|
const blueprint = this.currentBlueprint.get();
|
||||||
return;
|
if (!blueprint) {
|
||||||
}
|
return;
|
||||||
const mousePosition = this.root.app.mousePosition;
|
}
|
||||||
if (!mousePosition) {
|
const mousePosition = this.root.app.mousePosition;
|
||||||
// Not on screen
|
if (!mousePosition) {
|
||||||
return;
|
// Not on screen
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
|
||||||
const tile = worldPos.toTileSpace();
|
const worldPos = this.root.camera.screenToWorld(mousePosition);
|
||||||
blueprint.draw(parameters, tile);
|
const tile = worldPos.toTileSpace();
|
||||||
}
|
blueprint.draw(parameters, tile);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { DrawParameters } from "../../../core/draw_parameters";
|
|||||||
import { Entity } from "../../entity";
|
import { Entity } from "../../entity";
|
||||||
import { Loader } from "../../../core/loader";
|
import { Loader } from "../../../core/loader";
|
||||||
import { globalConfig } from "../../../core/config";
|
import { globalConfig } from "../../../core/config";
|
||||||
import { makeDiv, formatBigNumber, formatBigNumberFull } from "../../../core/utils";
|
import { makeDiv, formatBigNumber, formatBigNumberFull, dirInterval } from "../../../core/utils";
|
||||||
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
||||||
import { createLogger } from "../../../core/logging";
|
import { createLogger } from "../../../core/logging";
|
||||||
import { enumMouseButton } from "../../camera";
|
import { enumMouseButton } from "../../camera";
|
||||||
@ -23,7 +23,12 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
initialize() {
|
initialize() {
|
||||||
this.currentSelectionStartWorld = null;
|
this.currentSelectionStartWorld = null;
|
||||||
this.currentSelectionEnd = null;
|
this.currentSelectionEnd = null;
|
||||||
this.selectedUids = new Set();
|
|
||||||
|
/** @type {Set<Entity>} */
|
||||||
|
this.selectedEntities = new Set();
|
||||||
|
|
||||||
|
/** @type {number} */
|
||||||
|
this.selectedUids = 42;
|
||||||
|
|
||||||
this.root.signals.entityQueuedForDestroy.add(this.onEntityDestroyed, this);
|
this.root.signals.entityQueuedForDestroy.add(this.onEntityDestroyed, this);
|
||||||
this.root.hud.signals.pasteBlueprintRequested.add(this.clearSelection, this);
|
this.root.hud.signals.pasteBlueprintRequested.add(this.clearSelection, this);
|
||||||
@ -43,6 +48,20 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
this.root.signals.editModeChanged.add(this.clearSelection, this);
|
this.root.signals.editModeChanged.add(this.clearSelection, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.selectedEntities.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// getUidArray() {
|
||||||
|
// if (this.selectedEntities.size <= 0) return [];
|
||||||
|
// const uids = [];
|
||||||
|
// const arr = [...this.selectedEntities.values()];
|
||||||
|
// for (let i = arr.length - 1; i >= 0; --i) {
|
||||||
|
// uids.push(arr[i].uid);
|
||||||
|
// }
|
||||||
|
// return uids;
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the destroy callback and makes sure we clean our list
|
* Handles the destroy callback and makes sure we clean our list
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
@ -51,7 +70,7 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
if (this.root.bulkOperationRunning) {
|
if (this.root.bulkOperationRunning) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.selectedUids.delete(entity.uid);
|
this.selectedEntities.delete(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,8 +78,8 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
*/
|
*/
|
||||||
onBack() {
|
onBack() {
|
||||||
// Clear entities on escape
|
// Clear entities on escape
|
||||||
if (this.selectedUids.size > 0) {
|
if (this.selectedEntities.size > 0) {
|
||||||
this.selectedUids = new Set();
|
this.clear();
|
||||||
return STOP_PROPAGATION;
|
return STOP_PROPAGATION;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,19 +88,19 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
* Clears the entire selection
|
* Clears the entire selection
|
||||||
*/
|
*/
|
||||||
clearSelection() {
|
clearSelection() {
|
||||||
this.selectedUids = new Set();
|
this.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
confirmDelete() {
|
confirmDelete() {
|
||||||
if (
|
if (
|
||||||
!this.root.app.settings.getAllSettings().disableCutDeleteWarnings &&
|
!this.root.app.settings.getAllSettings().disableCutDeleteWarnings &&
|
||||||
this.selectedUids.size > 100
|
this.selectedEntities.size > 100
|
||||||
) {
|
) {
|
||||||
const { ok } = this.root.hud.parts.dialogs.showWarning(
|
const { ok } = this.root.hud.parts.dialogs.showWarning(
|
||||||
T.dialogs.massDeleteConfirm.title,
|
T.dialogs.massDeleteConfirm.title,
|
||||||
T.dialogs.massDeleteConfirm.desc.replace(
|
T.dialogs.massDeleteConfirm.desc.replace(
|
||||||
"<count>",
|
"<count>",
|
||||||
"" + formatBigNumberFull(this.selectedUids.size)
|
"" + formatBigNumberFull(this.selectedEntities.size)
|
||||||
),
|
),
|
||||||
["cancel:good:escape", "ok:bad:enter"]
|
["cancel:good:escape", "ok:bad:enter"]
|
||||||
);
|
);
|
||||||
@ -92,35 +111,26 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
doDelete() {
|
doDelete() {
|
||||||
const entityUids = Array.from(this.selectedUids);
|
|
||||||
|
|
||||||
// Build mapping from uid to entity
|
// Build mapping from uid to entity
|
||||||
/**
|
/**
|
||||||
* @type {Map<number, Entity>}
|
* @type {Map<number, Entity>}
|
||||||
*/
|
*/
|
||||||
const mapUidToEntity = this.root.entityMgr.getFrozenUidSearchMap();
|
//const mapUidToEntity = this.root.entityMgr.getFrozenUidSearchMap();
|
||||||
|
|
||||||
this.root.logic.performBulkOperation(() => {
|
this.root.logic.performBulkOperation(() => {
|
||||||
for (let i = 0; i < entityUids.length; ++i) {
|
const arr = [...this.selectedEntities.values()];
|
||||||
const uid = entityUids[i];
|
for (let i = arr.length - 1; i >= 0; --i) {
|
||||||
const entity = mapUidToEntity.get(uid);
|
if (!this.root.logic.tryDeleteBuilding(arr[i])) {
|
||||||
if (!entity) {
|
|
||||||
logger.error("Entity not found by uid:", uid);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.root.logic.tryDeleteBuilding(entity)) {
|
|
||||||
logger.error("Error in mass delete, could not remove building");
|
logger.error("Error in mass delete, could not remove building");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clear uids later
|
this.clear();
|
||||||
this.selectedUids = new Set();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
startCopy() {
|
startCopy() {
|
||||||
if (this.selectedUids.size > 0) {
|
if (this.selectedEntities.size > 0) {
|
||||||
if (!this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_blueprints)) {
|
if (!this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_blueprints)) {
|
||||||
this.root.hud.parts.dialogs.showInfo(
|
this.root.hud.parts.dialogs.showInfo(
|
||||||
T.dialogs.blueprintsNotUnlocked.title,
|
T.dialogs.blueprintsNotUnlocked.title,
|
||||||
@ -128,8 +138,10 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.root.hud.signals.buildingsSelectedForCopy.dispatch(Array.from(this.selectedUids));
|
const uids = [];
|
||||||
this.selectedUids = new Set();
|
|
||||||
|
this.root.hud.signals.buildingsSelectedForCopy.dispatch([...this.selectedEntities.values()]);
|
||||||
|
this.selectedEntities.clear();
|
||||||
this.root.soundProxy.playUiClick();
|
this.root.soundProxy.playUiClick();
|
||||||
} else {
|
} else {
|
||||||
this.root.soundProxy.playUiError();
|
this.root.soundProxy.playUiError();
|
||||||
@ -144,13 +156,13 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
);
|
);
|
||||||
} else if (
|
} else if (
|
||||||
!this.root.app.settings.getAllSettings().disableCutDeleteWarnings &&
|
!this.root.app.settings.getAllSettings().disableCutDeleteWarnings &&
|
||||||
this.selectedUids.size > 100
|
this.selectedEntities.size > 100
|
||||||
) {
|
) {
|
||||||
const { ok } = this.root.hud.parts.dialogs.showWarning(
|
const { ok } = this.root.hud.parts.dialogs.showWarning(
|
||||||
T.dialogs.massCutConfirm.title,
|
T.dialogs.massCutConfirm.title,
|
||||||
T.dialogs.massCutConfirm.desc.replace(
|
T.dialogs.massCutConfirm.desc.replace(
|
||||||
"<count>",
|
"<count>",
|
||||||
"" + formatBigNumberFull(this.selectedUids.size)
|
"" + formatBigNumberFull(this.selectedEntities.size)
|
||||||
),
|
),
|
||||||
["cancel:good:escape", "ok:bad:enter"]
|
["cancel:good:escape", "ok:bad:enter"]
|
||||||
);
|
);
|
||||||
@ -161,26 +173,26 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
doCut() {
|
doCut() {
|
||||||
if (this.selectedUids.size > 0) {
|
if (this.selectedEntities.size > 0) {
|
||||||
const entityUids = Array.from(this.selectedUids);
|
const cutAction = argArray => {
|
||||||
|
const arr = argArray || [...this.selectedEntities.values()];
|
||||||
const cutAction = () => {
|
|
||||||
// copy code relies on entities still existing, so must copy before deleting.
|
// copy code relies on entities still existing, so must copy before deleting.
|
||||||
this.root.hud.signals.buildingsSelectedForCopy.dispatch(entityUids);
|
|
||||||
|
|
||||||
for (let i = 0; i < entityUids.length; ++i) {
|
this.root.hud.signals.buildingsSelectedForCopy.dispatch(arr);
|
||||||
const uid = entityUids[i];
|
|
||||||
const entity = this.root.entityMgr.findByUid(uid);
|
for (let i = arr.length - 1; i >= 0; --i) {
|
||||||
|
const entity = arr[i];
|
||||||
if (!this.root.logic.tryDeleteBuilding(entity)) {
|
if (!this.root.logic.tryDeleteBuilding(entity)) {
|
||||||
logger.error("Error in mass cut, could not remove building");
|
logger.error("Error in mass cut, could not remove building");
|
||||||
this.selectedUids.delete(uid);
|
this.selectedEntities.delete(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const blueprint = Blueprint.fromUids(this.root, entityUids);
|
const arr = [...this.selectedEntities.values()];
|
||||||
|
const blueprint = Blueprint.fromEntities(arr);
|
||||||
if (blueprint.canAfford(this.root)) {
|
if (blueprint.canAfford(this.root)) {
|
||||||
cutAction();
|
cutAction(arr);
|
||||||
} else {
|
} else {
|
||||||
const { cancel, ok } = this.root.hud.parts.dialogs.showWarning(
|
const { cancel, ok } = this.root.hud.parts.dialogs.showWarning(
|
||||||
T.dialogs.massCutInsufficientConfirm.title,
|
T.dialogs.massCutInsufficientConfirm.title,
|
||||||
@ -212,7 +224,7 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
|
|
||||||
if (!this.root.keyMapper.getBinding(KEYMAPPINGS.massSelect.massSelectSelectMultiple).pressed) {
|
if (!this.root.keyMapper.getBinding(KEYMAPPINGS.massSelect.massSelectSelectMultiple).pressed) {
|
||||||
// Start new selection
|
// Start new selection
|
||||||
this.selectedUids = new Set();
|
this.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.currentSelectionStartWorld = this.root.camera.screenToWorld(pos.copy());
|
this.currentSelectionStartWorld = this.root.camera.screenToWorld(pos.copy());
|
||||||
@ -245,7 +257,7 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
for (let y = realTileStart.y; y <= realTileEnd.y; ++y) {
|
for (let y = realTileStart.y; y <= realTileEnd.y; ++y) {
|
||||||
const contents = this.root.map.getLayerContentXY(x, y, this.root.currentLayer);
|
const contents = this.root.map.getLayerContentXY(x, y, this.root.currentLayer);
|
||||||
if (contents && this.root.logic.canDeleteBuilding(contents)) {
|
if (contents && this.root.logic.canDeleteBuilding(contents)) {
|
||||||
this.selectedUids.add(contents.uid);
|
this.selectedEntities.add(contents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -319,18 +331,22 @@ export class HUDMassSelector extends BaseHUDPart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
parameters.context.fillStyle = THEME.map.selectionOverlay;
|
parameters.context.fillStyle = THEME.map.selectionOverlay;
|
||||||
this.selectedUids.forEach(uid => {
|
|
||||||
const entity = this.root.entityMgr.findByUid(uid);
|
if (this.selectedEntities.size > 0) {
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
const arr = [...this.selectedEntities.values()];
|
||||||
const bounds = staticComp.getTileSpaceBounds();
|
for (let i = arr.length - 1; i >= 0; --i) {
|
||||||
parameters.context.beginRoundedRect(
|
const entity = arr[i];
|
||||||
bounds.x * globalConfig.tileSize + boundsBorder,
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
bounds.y * globalConfig.tileSize + boundsBorder,
|
const bounds = staticComp.getTileSpaceBounds();
|
||||||
bounds.w * globalConfig.tileSize - 2 * boundsBorder,
|
parameters.context.beginRoundedRect(
|
||||||
bounds.h * globalConfig.tileSize - 2 * boundsBorder,
|
bounds.x * globalConfig.tileSize + boundsBorder,
|
||||||
2
|
bounds.y * globalConfig.tileSize + boundsBorder,
|
||||||
);
|
bounds.w * globalConfig.tileSize - 2 * boundsBorder,
|
||||||
parameters.context.fill();
|
bounds.h * globalConfig.tileSize - 2 * boundsBorder,
|
||||||
});
|
2
|
||||||
|
);
|
||||||
|
parameters.context.fill();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -422,8 +422,8 @@ export class BeltSystem extends GameSystemWithFilter {
|
|||||||
|
|
||||||
const result = [];
|
const result = [];
|
||||||
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
if (visitedUids.has(entity.uid)) {
|
if (visitedUids.has(entity.uid)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,9 +12,9 @@ export class BeltReaderSystem extends GameSystemWithFilter {
|
|||||||
const now = this.root.time.now();
|
const now = this.root.time.now();
|
||||||
const minimumTime = now - globalConfig.readerAnalyzeIntervalSeconds;
|
const minimumTime = now - globalConfig.readerAnalyzeIntervalSeconds;
|
||||||
const minimumTimeForThroughput = now - 1;
|
const minimumTimeForThroughput = now - 1;
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
|
||||||
const entity = this.allEntities[i];
|
|
||||||
|
|
||||||
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
|
const entity = this.allEntitiesArray[i];
|
||||||
const readerComp = entity.components.BeltReader;
|
const readerComp = entity.components.BeltReader;
|
||||||
const pinsComp = entity.components.WiredPins;
|
const pinsComp = entity.components.WiredPins;
|
||||||
|
|
||||||
|
|||||||
@ -22,8 +22,8 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
|
|||||||
|
|
||||||
update() {
|
update() {
|
||||||
// Set signals
|
// Set signals
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const pinsComp = entity.components.WiredPins;
|
const pinsComp = entity.components.WiredPins;
|
||||||
const signalComp = entity.components.ConstantSignal;
|
const signalComp = entity.components.ConstantSignal;
|
||||||
pinsComp.slots[0].value = signalComp.signal;
|
pinsComp.slots[0].value = signalComp.signal;
|
||||||
|
|||||||
@ -20,8 +20,8 @@ export class FilterSystem extends GameSystemWithFilter {
|
|||||||
|
|
||||||
const requiredProgress = 1 - progress;
|
const requiredProgress = 1 - progress;
|
||||||
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const filterComp = entity.components.Filter;
|
const filterComp = entity.components.Filter;
|
||||||
const ejectorComp = entity.components.ItemEjector;
|
const ejectorComp = entity.components.ItemEjector;
|
||||||
|
|
||||||
|
|||||||
@ -1,196 +1,185 @@
|
|||||||
import { globalConfig } from "../../core/config";
|
import { globalConfig } from "../../core/config";
|
||||||
import { smoothenDpi } from "../../core/dpi_manager";
|
import { smoothenDpi } from "../../core/dpi_manager";
|
||||||
import { DrawParameters } from "../../core/draw_parameters";
|
import { DrawParameters } from "../../core/draw_parameters";
|
||||||
import { drawSpriteClipped } from "../../core/draw_utils";
|
import { drawSpriteClipped } from "../../core/draw_utils";
|
||||||
import { Loader } from "../../core/loader";
|
import { Loader } from "../../core/loader";
|
||||||
import { Rectangle } from "../../core/rectangle";
|
import { Rectangle } from "../../core/rectangle";
|
||||||
import { ORIGINAL_SPRITE_SCALE } from "../../core/sprites";
|
import { ORIGINAL_SPRITE_SCALE } from "../../core/sprites";
|
||||||
import { formatBigNumber } from "../../core/utils";
|
import { formatBigNumber } from "../../core/utils";
|
||||||
import { T } from "../../translations";
|
import { T } from "../../translations";
|
||||||
import { HubComponent } from "../components/hub";
|
import { HubComponent } from "../components/hub";
|
||||||
import { Entity } from "../entity";
|
import { Entity } from "../entity";
|
||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
|
|
||||||
const HUB_SIZE_TILES = 4;
|
const HUB_SIZE_TILES = 4;
|
||||||
const HUB_SIZE_PIXELS = HUB_SIZE_TILES * globalConfig.tileSize;
|
const HUB_SIZE_PIXELS = HUB_SIZE_TILES * globalConfig.tileSize;
|
||||||
|
|
||||||
export class HubSystem extends GameSystemWithFilter {
|
export class HubSystem extends GameSystemWithFilter {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
super(root, [HubComponent]);
|
super(root, [HubComponent]);
|
||||||
|
|
||||||
this.hubSprite = Loader.getSprite("sprites/buildings/hub.png");
|
this.hubSprite = Loader.getSprite("sprites/buildings/hub.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {DrawParameters} parameters
|
* @param {DrawParameters} parameters
|
||||||
*/
|
*/
|
||||||
draw(parameters) {
|
draw(parameters) {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
this.drawEntity(parameters, this.allEntities[i]);
|
const entity = this.allEntitiesArray[i];
|
||||||
}
|
this.drawEntity(parameters, entity);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
update() {
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
update() {
|
||||||
// Set hub goal
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const pinsComp = entity.components.WiredPins;
|
const pinsComp = entity.components.WiredPins;
|
||||||
pinsComp.slots[0].value = this.root.shapeDefinitionMgr.getShapeItemFromDefinition(
|
pinsComp.slots[0].value = this.root.shapeDefinitionMgr.getShapeItemFromDefinition(
|
||||||
this.root.hubGoals.currentGoal.definition
|
this.root.hubGoals.currentGoal.definition
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {HTMLCanvasElement} canvas
|
* @param {HTMLCanvasElement} canvas
|
||||||
* @param {CanvasRenderingContext2D} context
|
* @param {CanvasRenderingContext2D} context
|
||||||
* @param {number} w
|
* @param {number} w
|
||||||
* @param {number} h
|
* @param {number} h
|
||||||
* @param {number} dpi
|
* @param {number} dpi
|
||||||
*/
|
*/
|
||||||
redrawHubBaseTexture(canvas, context, w, h, dpi) {
|
redrawHubBaseTexture(canvas, context, w, h, dpi) {
|
||||||
// This method is quite ugly, please ignore it!
|
// This method is quite ugly, please ignore it!
|
||||||
|
|
||||||
context.scale(dpi, dpi);
|
context.scale(dpi, dpi);
|
||||||
|
|
||||||
const parameters = new DrawParameters({
|
const parameters = new DrawParameters({
|
||||||
context,
|
context,
|
||||||
visibleRect: new Rectangle(0, 0, w, h),
|
visibleRect: new Rectangle(0, 0, w, h),
|
||||||
desiredAtlasScale: ORIGINAL_SPRITE_SCALE,
|
desiredAtlasScale: ORIGINAL_SPRITE_SCALE,
|
||||||
zoomLevel: dpi * 0.75,
|
zoomLevel: dpi * 0.75,
|
||||||
root: this.root,
|
root: this.root,
|
||||||
});
|
});
|
||||||
|
|
||||||
context.clearRect(0, 0, w, h);
|
context.clearRect(0, 0, w, h);
|
||||||
|
|
||||||
this.hubSprite.draw(context, 0, 0, w, h);
|
this.hubSprite.draw(context, 0, 0, w, h);
|
||||||
|
|
||||||
if (this.root.hubGoals.isEndOfDemoReached()) {
|
const definition = this.root.hubGoals.currentGoal.definition;
|
||||||
// End of demo
|
definition.drawCentered(45, 58, parameters, 36);
|
||||||
context.font = "bold 12px GameFont";
|
|
||||||
context.fillStyle = "#fd0752";
|
const goals = this.root.hubGoals.currentGoal;
|
||||||
context.textAlign = "center";
|
|
||||||
context.fillText(T.buildings.hub.endOfDemo.toUpperCase(), w / 2, h / 2 + 6);
|
const textOffsetX = 70;
|
||||||
context.textAlign = "left";
|
const textOffsetY = 61;
|
||||||
|
|
||||||
return;
|
if (goals.throughputOnly) {
|
||||||
}
|
// Throughput
|
||||||
|
const deliveredText = T.ingame.statistics.shapesDisplayUnits.second.replace(
|
||||||
const definition = this.root.hubGoals.currentGoal.definition;
|
"<shapes>",
|
||||||
definition.drawCentered(45, 58, parameters, 36);
|
formatBigNumber(goals.required)
|
||||||
|
);
|
||||||
const goals = this.root.hubGoals.currentGoal;
|
|
||||||
|
context.font = "bold 12px GameFont";
|
||||||
const textOffsetX = 70;
|
context.fillStyle = "#64666e";
|
||||||
const textOffsetY = 61;
|
context.textAlign = "left";
|
||||||
|
context.fillText(deliveredText, textOffsetX, textOffsetY);
|
||||||
if (goals.throughputOnly) {
|
} else {
|
||||||
// Throughput
|
// Deliver count
|
||||||
const deliveredText = T.ingame.statistics.shapesDisplayUnits.second.replace(
|
const delivered = this.root.hubGoals.getCurrentGoalDelivered();
|
||||||
"<shapes>",
|
const deliveredText = "" + formatBigNumber(delivered);
|
||||||
formatBigNumber(goals.required)
|
|
||||||
);
|
if (delivered > 9999) {
|
||||||
|
context.font = "bold 16px GameFont";
|
||||||
context.font = "bold 12px GameFont";
|
} else if (delivered > 999) {
|
||||||
context.fillStyle = "#64666e";
|
context.font = "bold 20px GameFont";
|
||||||
context.textAlign = "left";
|
} else {
|
||||||
context.fillText(deliveredText, textOffsetX, textOffsetY);
|
context.font = "bold 25px GameFont";
|
||||||
} else {
|
}
|
||||||
// Deliver count
|
context.fillStyle = "#64666e";
|
||||||
const delivered = this.root.hubGoals.getCurrentGoalDelivered();
|
context.textAlign = "left";
|
||||||
const deliveredText = "" + formatBigNumber(delivered);
|
context.fillText(deliveredText, textOffsetX, textOffsetY);
|
||||||
|
|
||||||
if (delivered > 9999) {
|
// Required
|
||||||
context.font = "bold 16px GameFont";
|
context.font = "13px GameFont";
|
||||||
} else if (delivered > 999) {
|
context.fillStyle = "#a4a6b0";
|
||||||
context.font = "bold 20px GameFont";
|
context.fillText("/ " + formatBigNumber(goals.required), textOffsetX, textOffsetY + 13);
|
||||||
} else {
|
|
||||||
context.font = "bold 25px GameFont";
|
// Reward
|
||||||
}
|
const rewardText = T.storyRewards[goals.reward].title.toUpperCase();
|
||||||
context.fillStyle = "#64666e";
|
if (rewardText.length > 12) {
|
||||||
context.textAlign = "left";
|
context.font = "bold 8px GameFont";
|
||||||
context.fillText(deliveredText, textOffsetX, textOffsetY);
|
} else {
|
||||||
|
context.font = "bold 10px GameFont";
|
||||||
// Required
|
}
|
||||||
context.font = "13px GameFont";
|
context.fillStyle = "#fd0752";
|
||||||
context.fillStyle = "#a4a6b0";
|
context.textAlign = "center";
|
||||||
context.fillText("/ " + formatBigNumber(goals.required), textOffsetX, textOffsetY + 13);
|
|
||||||
}
|
context.fillText(rewardText, HUB_SIZE_PIXELS / 2, 105);
|
||||||
|
|
||||||
// Reward
|
// Level "8"
|
||||||
const rewardText = T.storyRewards[goals.reward].title.toUpperCase();
|
context.font = "bold 10px GameFont";
|
||||||
if (rewardText.length > 12) {
|
context.fillStyle = "#fff";
|
||||||
context.font = "bold 8px GameFont";
|
context.fillText("" + this.root.hubGoals.level, 27, 32);
|
||||||
} else {
|
|
||||||
context.font = "bold 10px GameFont";
|
// "LVL"
|
||||||
}
|
context.textAlign = "center";
|
||||||
context.fillStyle = "#fd0752";
|
context.fillStyle = "#fff";
|
||||||
context.textAlign = "center";
|
context.font = "bold 6px GameFont";
|
||||||
|
context.fillText(T.buildings.hub.levelShortcut, 27, 22);
|
||||||
context.fillText(rewardText, HUB_SIZE_PIXELS / 2, 105);
|
|
||||||
|
// "Deliver"
|
||||||
// Level "8"
|
context.fillStyle = "#64666e";
|
||||||
context.font = "bold 10px GameFont";
|
context.font = "bold 10px GameFont";
|
||||||
context.fillStyle = "#fff";
|
context.fillText(T.buildings.hub.deliver.toUpperCase(), HUB_SIZE_PIXELS / 2, 30);
|
||||||
context.fillText("" + this.root.hubGoals.level, 27, 32);
|
|
||||||
|
// "To unlock"
|
||||||
// "LVL"
|
const unlockText = T.buildings.hub.toUnlock.toUpperCase();
|
||||||
context.textAlign = "center";
|
if (unlockText.length > 15) {
|
||||||
context.fillStyle = "#fff";
|
context.font = "bold 8px GameFont";
|
||||||
context.font = "bold 6px GameFont";
|
} else {
|
||||||
context.fillText(T.buildings.hub.levelShortcut, 27, 22);
|
context.font = "bold 10px GameFont";
|
||||||
|
}
|
||||||
// "Deliver"
|
context.fillText(T.buildings.hub.toUnlock.toUpperCase(), HUB_SIZE_PIXELS / 2, 92);
|
||||||
context.fillStyle = "#64666e";
|
|
||||||
context.font = "bold 10px GameFont";
|
context.textAlign = "left";
|
||||||
context.fillText(T.buildings.hub.deliver.toUpperCase(), HUB_SIZE_PIXELS / 2, 30);
|
}
|
||||||
|
}
|
||||||
// "To unlock"
|
|
||||||
const unlockText = T.buildings.hub.toUnlock.toUpperCase();
|
/**
|
||||||
if (unlockText.length > 15) {
|
* @param {DrawParameters} parameters
|
||||||
context.font = "bold 8px GameFont";
|
* @param {Entity} entity
|
||||||
} else {
|
*/
|
||||||
context.font = "bold 10px GameFont";
|
drawEntity(parameters, entity) {
|
||||||
}
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
context.fillText(T.buildings.hub.toUnlock.toUpperCase(), HUB_SIZE_PIXELS / 2, 92);
|
if (!staticComp.shouldBeDrawn(parameters)) {
|
||||||
|
return;
|
||||||
context.textAlign = "left";
|
}
|
||||||
}
|
|
||||||
|
// Deliver count
|
||||||
/**
|
const delivered = this.root.hubGoals.getCurrentGoalDelivered();
|
||||||
* @param {DrawParameters} parameters
|
const deliveredText = "" + formatBigNumber(delivered);
|
||||||
* @param {Entity} entity
|
|
||||||
*/
|
const dpi = smoothenDpi(globalConfig.shapesSharpness * parameters.zoomLevel);
|
||||||
drawEntity(parameters, entity) {
|
const canvas = parameters.root.buffers.getForKey({
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
key: "hub",
|
||||||
if (!staticComp.shouldBeDrawn(parameters)) {
|
subKey: dpi + "/" + this.root.hubGoals.level + "/" + deliveredText,
|
||||||
return;
|
w: globalConfig.tileSize * 4,
|
||||||
}
|
h: globalConfig.tileSize * 4,
|
||||||
|
dpi,
|
||||||
// Deliver count
|
redrawMethod: this.redrawHubBaseTexture.bind(this),
|
||||||
const delivered = this.root.hubGoals.getCurrentGoalDelivered();
|
});
|
||||||
const deliveredText = "" + formatBigNumber(delivered);
|
|
||||||
|
const extrude = 8;
|
||||||
const dpi = smoothenDpi(globalConfig.shapesSharpness * parameters.zoomLevel);
|
drawSpriteClipped({
|
||||||
const canvas = parameters.root.buffers.getForKey({
|
parameters,
|
||||||
key: "hub",
|
sprite: canvas,
|
||||||
subKey: dpi + "/" + this.root.hubGoals.level + "/" + deliveredText,
|
x: staticComp.origin.x * globalConfig.tileSize - extrude,
|
||||||
w: globalConfig.tileSize * 4,
|
y: staticComp.origin.y * globalConfig.tileSize - extrude,
|
||||||
h: globalConfig.tileSize * 4,
|
w: HUB_SIZE_PIXELS + 2 * extrude,
|
||||||
dpi,
|
h: HUB_SIZE_PIXELS + 2 * extrude,
|
||||||
redrawMethod: this.redrawHubBaseTexture.bind(this),
|
originalW: HUB_SIZE_PIXELS * dpi,
|
||||||
});
|
originalH: HUB_SIZE_PIXELS * dpi,
|
||||||
|
});
|
||||||
const extrude = 8;
|
}
|
||||||
drawSpriteClipped({
|
}
|
||||||
parameters,
|
|
||||||
sprite: canvas,
|
|
||||||
x: staticComp.origin.x * globalConfig.tileSize - extrude,
|
|
||||||
y: staticComp.origin.y * globalConfig.tileSize - extrude,
|
|
||||||
w: HUB_SIZE_PIXELS + 2 * extrude,
|
|
||||||
h: HUB_SIZE_PIXELS + 2 * extrude,
|
|
||||||
originalW: HUB_SIZE_PIXELS * dpi,
|
|
||||||
originalH: HUB_SIZE_PIXELS * dpi,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -39,8 +39,8 @@ export class ItemAcceptorSystem extends GameSystemWithFilter {
|
|||||||
// Reset accumulated ticks
|
// Reset accumulated ticks
|
||||||
this.accumulatedTicksWhileInMapOverview = 0;
|
this.accumulatedTicksWhileInMapOverview = 0;
|
||||||
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const aceptorComp = entity.components.ItemAcceptor;
|
const aceptorComp = entity.components.ItemAcceptor;
|
||||||
const animations = aceptorComp.itemConsumptionAnimations;
|
const animations = aceptorComp.itemConsumptionAnimations;
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { DrawParameters } from "../../core/draw_parameters";
|
|||||||
import { createLogger } from "../../core/logging";
|
import { createLogger } from "../../core/logging";
|
||||||
import { Rectangle } from "../../core/rectangle";
|
import { Rectangle } from "../../core/rectangle";
|
||||||
import { StaleAreaDetector } from "../../core/stale_area_detector";
|
import { StaleAreaDetector } from "../../core/stale_area_detector";
|
||||||
|
import { dirInterval } from "../../core/utils";
|
||||||
import { enumDirection, enumDirectionToVector } from "../../core/vector";
|
import { enumDirection, enumDirectionToVector } from "../../core/vector";
|
||||||
import { BaseItem } from "../base_item";
|
import { BaseItem } from "../base_item";
|
||||||
import { BeltComponent } from "../components/belt";
|
import { BeltComponent } from "../components/belt";
|
||||||
@ -60,8 +61,8 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
|||||||
*/
|
*/
|
||||||
recomputeCacheFull() {
|
recomputeCacheFull() {
|
||||||
logger.log("Full cache recompute in post load hook");
|
logger.log("Full cache recompute in post load hook");
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
this.recomputeSingleEntityCache(entity);
|
this.recomputeSingleEntityCache(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,8 +147,8 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Go over all cache entries
|
// Go over all cache entries
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const sourceEntity = this.allEntities[i];
|
const sourceEntity = this.allEntitiesArray[i];
|
||||||
const sourceEjectorComp = sourceEntity.components.ItemEjector;
|
const sourceEjectorComp = sourceEntity.components.ItemEjector;
|
||||||
|
|
||||||
const slots = sourceEjectorComp.slots;
|
const slots = sourceEjectorComp.slots;
|
||||||
|
|||||||
@ -68,9 +68,8 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
|
|
||||||
const processorComp = entity.components.ItemProcessor;
|
const processorComp = entity.components.ItemProcessor;
|
||||||
const ejectorComp = entity.components.ItemEjector;
|
const ejectorComp = entity.components.ItemEjector;
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,8 @@ export class ItemProducerSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const pinsComp = entity.components.WiredPins;
|
const pinsComp = entity.components.WiredPins;
|
||||||
const pin = pinsComp.slots[0];
|
const pin = pinsComp.slots[0];
|
||||||
const network = pin.linkedNetwork;
|
const network = pin.linkedNetwork;
|
||||||
|
|||||||
@ -14,9 +14,8 @@ export class LeverSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
|
|
||||||
const leverComp = entity.components.Lever;
|
const leverComp = entity.components.Lever;
|
||||||
const pinsComp = entity.components.WiredPins;
|
const pinsComp = entity.components.WiredPins;
|
||||||
|
|
||||||
|
|||||||
@ -30,8 +30,8 @@ export class LogicGateSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const logicComp = entity.components.LogicGate;
|
const logicComp = entity.components.LogicGate;
|
||||||
const slotComp = entity.components.WiredPins;
|
const slotComp = entity.components.WiredPins;
|
||||||
|
|
||||||
|
|||||||
@ -36,8 +36,8 @@ export class MinerSystem extends GameSystemWithFilter {
|
|||||||
miningSpeed *= 100;
|
miningSpeed *= 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const minerComp = entity.components.Miner;
|
const minerComp = entity.components.Miner;
|
||||||
|
|
||||||
// Reset everything on recompute
|
// Reset everything on recompute
|
||||||
|
|||||||
@ -1,101 +1,101 @@
|
|||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
import { StorageComponent } from "../components/storage";
|
import { StorageComponent } from "../components/storage";
|
||||||
import { DrawParameters } from "../../core/draw_parameters";
|
import { DrawParameters } from "../../core/draw_parameters";
|
||||||
import { formatBigNumber, lerp } from "../../core/utils";
|
import { formatBigNumber, lerp } from "../../core/utils";
|
||||||
import { Loader } from "../../core/loader";
|
import { Loader } from "../../core/loader";
|
||||||
import { BOOL_TRUE_SINGLETON, BOOL_FALSE_SINGLETON } from "../items/boolean_item";
|
import { BOOL_TRUE_SINGLETON, BOOL_FALSE_SINGLETON } from "../items/boolean_item";
|
||||||
import { MapChunkView } from "../map_chunk_view";
|
import { MapChunkView } from "../map_chunk_view";
|
||||||
|
|
||||||
export class StorageSystem extends GameSystemWithFilter {
|
export class StorageSystem extends GameSystemWithFilter {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
super(root, [StorageComponent]);
|
super(root, [StorageComponent]);
|
||||||
|
|
||||||
this.storageOverlaySprite = Loader.getSprite("sprites/misc/storage_overlay.png");
|
this.storageOverlaySprite = Loader.getSprite("sprites/misc/storage_overlay.png");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores which uids were already drawn to avoid drawing entities twice
|
* Stores which uids were already drawn to avoid drawing entities twice
|
||||||
* @type {Set<number>}
|
* @type {Set<number>}
|
||||||
*/
|
*/
|
||||||
this.drawnUids = new Set();
|
this.drawnUids = new Set();
|
||||||
|
|
||||||
this.root.signals.gameFrameStarted.add(this.clearDrawnUids, this);
|
this.root.signals.gameFrameStarted.add(this.clearDrawnUids, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
clearDrawnUids() {
|
clearDrawnUids() {
|
||||||
this.drawnUids.clear();
|
this.drawnUids.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntitiesArray[i];
|
||||||
const storageComp = entity.components.Storage;
|
const storageComp = entity.components.Storage;
|
||||||
const pinsComp = entity.components.WiredPins;
|
const pinsComp = entity.components.WiredPins;
|
||||||
|
|
||||||
// Eject from storage
|
// Eject from storage
|
||||||
if (storageComp.storedItem && storageComp.storedCount > 0) {
|
if (storageComp.storedItem && storageComp.storedCount > 0) {
|
||||||
const ejectorComp = entity.components.ItemEjector;
|
const ejectorComp = entity.components.ItemEjector;
|
||||||
|
|
||||||
const nextSlot = ejectorComp.getFirstFreeSlot();
|
const nextSlot = ejectorComp.getFirstFreeSlot();
|
||||||
if (nextSlot !== null) {
|
if (nextSlot !== null) {
|
||||||
if (ejectorComp.tryEject(nextSlot, storageComp.storedItem)) {
|
if (ejectorComp.tryEject(nextSlot, storageComp.storedItem)) {
|
||||||
storageComp.storedCount--;
|
storageComp.storedCount--;
|
||||||
|
|
||||||
if (storageComp.storedCount === 0) {
|
if (storageComp.storedCount === 0) {
|
||||||
storageComp.storedItem = null;
|
storageComp.storedItem = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let targetAlpha = storageComp.storedCount > 0 ? 1 : 0;
|
let targetAlpha = storageComp.storedCount > 0 ? 1 : 0;
|
||||||
storageComp.overlayOpacity = lerp(storageComp.overlayOpacity, targetAlpha, 0.05);
|
storageComp.overlayOpacity = lerp(storageComp.overlayOpacity, targetAlpha, 0.05);
|
||||||
|
|
||||||
pinsComp.slots[0].value = storageComp.storedItem;
|
pinsComp.slots[0].value = storageComp.storedItem;
|
||||||
pinsComp.slots[1].value = storageComp.getIsFull() ? BOOL_TRUE_SINGLETON : BOOL_FALSE_SINGLETON;
|
pinsComp.slots[1].value = storageComp.getIsFull() ? BOOL_TRUE_SINGLETON : BOOL_FALSE_SINGLETON;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {DrawParameters} parameters
|
* @param {DrawParameters} parameters
|
||||||
* @param {MapChunkView} chunk
|
* @param {MapChunkView} chunk
|
||||||
*/
|
*/
|
||||||
drawChunk(parameters, chunk) {
|
drawChunk(parameters, chunk) {
|
||||||
const contents = chunk.containedEntitiesByLayer.regular;
|
const contents = chunk.containedEntitiesByLayer.regular;
|
||||||
for (let i = 0; i < contents.length; ++i) {
|
for (let i = 0; i < contents.length; ++i) {
|
||||||
const entity = contents[i];
|
const entity = contents[i];
|
||||||
const storageComp = entity.components.Storage;
|
const storageComp = entity.components.Storage;
|
||||||
if (!storageComp) {
|
if (!storageComp) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const storedItem = storageComp.storedItem;
|
const storedItem = storageComp.storedItem;
|
||||||
if (!storedItem) {
|
if (!storedItem) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.drawnUids.has(entity.uid)) {
|
if (this.drawnUids.has(entity.uid)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.drawnUids.add(entity.uid);
|
this.drawnUids.add(entity.uid);
|
||||||
|
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
|
||||||
const context = parameters.context;
|
const context = parameters.context;
|
||||||
context.globalAlpha = storageComp.overlayOpacity;
|
context.globalAlpha = storageComp.overlayOpacity;
|
||||||
const center = staticComp.getTileSpaceBounds().getCenter().toWorldSpace();
|
const center = staticComp.getTileSpaceBounds().getCenter().toWorldSpace();
|
||||||
storedItem.drawItemCenteredClipped(center.x, center.y, parameters, 30);
|
storedItem.drawItemCenteredClipped(center.x, center.y, parameters, 30);
|
||||||
|
|
||||||
this.storageOverlaySprite.drawCached(parameters, center.x - 15, center.y + 15, 30, 15);
|
this.storageOverlaySprite.drawCached(parameters, center.x - 15, center.y + 15, 30, 15);
|
||||||
|
|
||||||
if (parameters.visibleRect.containsCircle(center.x, center.y + 25, 20)) {
|
if (parameters.visibleRect.containsCircle(center.x, center.y + 25, 20)) {
|
||||||
context.font = "bold 10px GameFont";
|
context.font = "bold 10px GameFont";
|
||||||
context.textAlign = "center";
|
context.textAlign = "center";
|
||||||
context.fillStyle = "#64666e";
|
context.fillStyle = "#64666e";
|
||||||
context.fillText(formatBigNumber(storageComp.storedCount), center.x, center.y + 25.5);
|
context.fillText(formatBigNumber(storageComp.storedCount), center.x, center.y + 25.5);
|
||||||
context.textAlign = "left";
|
context.textAlign = "left";
|
||||||
}
|
}
|
||||||
context.globalAlpha = 1;
|
context.globalAlpha = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,349 +1,348 @@
|
|||||||
import { globalConfig } from "../../core/config";
|
import { globalConfig } from "../../core/config";
|
||||||
import { Loader } from "../../core/loader";
|
import { Loader } from "../../core/loader";
|
||||||
import { createLogger } from "../../core/logging";
|
import { createLogger } from "../../core/logging";
|
||||||
import { Rectangle } from "../../core/rectangle";
|
import { Rectangle } from "../../core/rectangle";
|
||||||
import { StaleAreaDetector } from "../../core/stale_area_detector";
|
import { StaleAreaDetector } from "../../core/stale_area_detector";
|
||||||
import { fastArrayDelete } from "../../core/utils";
|
import { fastArrayDelete } from "../../core/utils";
|
||||||
import {
|
import {
|
||||||
enumAngleToDirection,
|
enumAngleToDirection,
|
||||||
enumDirection,
|
enumDirection,
|
||||||
enumDirectionToAngle,
|
enumDirectionToAngle,
|
||||||
enumDirectionToVector,
|
enumDirectionToVector,
|
||||||
enumInvertedDirections,
|
enumInvertedDirections,
|
||||||
} from "../../core/vector";
|
} from "../../core/vector";
|
||||||
import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt";
|
import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt";
|
||||||
import { Entity } from "../entity";
|
import { Entity } from "../entity";
|
||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
|
|
||||||
const logger = createLogger("tunnels");
|
const logger = createLogger("tunnels");
|
||||||
|
|
||||||
export class UndergroundBeltSystem extends GameSystemWithFilter {
|
export class UndergroundBeltSystem extends GameSystemWithFilter {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
super(root, [UndergroundBeltComponent]);
|
super(root, [UndergroundBeltComponent]);
|
||||||
|
|
||||||
this.beltSprites = {
|
this.beltSprites = {
|
||||||
[enumUndergroundBeltMode.sender]: Loader.getSprite(
|
[enumUndergroundBeltMode.sender]: Loader.getSprite(
|
||||||
"sprites/buildings/underground_belt_entry.png"
|
"sprites/buildings/underground_belt_entry.png"
|
||||||
),
|
),
|
||||||
[enumUndergroundBeltMode.receiver]: Loader.getSprite(
|
[enumUndergroundBeltMode.receiver]: Loader.getSprite(
|
||||||
"sprites/buildings/underground_belt_exit.png"
|
"sprites/buildings/underground_belt_exit.png"
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.staleAreaWatcher = new StaleAreaDetector({
|
this.staleAreaWatcher = new StaleAreaDetector({
|
||||||
root: this.root,
|
root: this.root,
|
||||||
name: "underground-belt",
|
name: "underground-belt",
|
||||||
recomputeMethod: this.recomputeArea.bind(this),
|
recomputeMethod: this.recomputeArea.bind(this),
|
||||||
});
|
});
|
||||||
|
|
||||||
this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this);
|
this.root.signals.entityManuallyPlaced.add(this.onEntityManuallyPlaced, this);
|
||||||
|
|
||||||
// NOTICE: Once we remove a tunnel, we need to update the whole area to
|
// NOTICE: Once we remove a tunnel, we need to update the whole area to
|
||||||
// clear outdated handles
|
// clear outdated handles
|
||||||
this.staleAreaWatcher.recomputeOnComponentsChanged(
|
this.staleAreaWatcher.recomputeOnComponentsChanged(
|
||||||
[UndergroundBeltComponent],
|
[UndergroundBeltComponent],
|
||||||
globalConfig.undergroundBeltMaxTilesByTier[globalConfig.undergroundBeltMaxTilesByTier.length - 1]
|
globalConfig.undergroundBeltMaxTilesByTier[globalConfig.undergroundBeltMaxTilesByTier.length - 1]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback when an entity got placed, used to remove belts between underground belts
|
* Callback when an entity got placed, used to remove belts between underground belts
|
||||||
* @param {Entity} entity
|
* @param {Entity} entity
|
||||||
*/
|
*/
|
||||||
onEntityManuallyPlaced(entity) {
|
onEntityManuallyPlaced(entity) {
|
||||||
if (!this.root.app.settings.getAllSettings().enableTunnelSmartplace) {
|
if (!this.root.app.settings.getAllSettings().enableTunnelSmartplace) {
|
||||||
// Smart-place disabled
|
// Smart-place disabled
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
if (undergroundComp && undergroundComp.mode === enumUndergroundBeltMode.receiver) {
|
if (undergroundComp && undergroundComp.mode === enumUndergroundBeltMode.receiver) {
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
const tile = staticComp.origin;
|
const tile = staticComp.origin;
|
||||||
|
|
||||||
const direction = enumAngleToDirection[staticComp.rotation];
|
const direction = enumAngleToDirection[staticComp.rotation];
|
||||||
const inverseDirection = enumInvertedDirections[direction];
|
const inverseDirection = enumInvertedDirections[direction];
|
||||||
const offset = enumDirectionToVector[inverseDirection];
|
const offset = enumDirectionToVector[inverseDirection];
|
||||||
|
|
||||||
let currentPos = tile.copy();
|
let currentPos = tile.copy();
|
||||||
|
|
||||||
const tier = undergroundComp.tier;
|
const tier = undergroundComp.tier;
|
||||||
const range = globalConfig.undergroundBeltMaxTilesByTier[tier];
|
const range = globalConfig.undergroundBeltMaxTilesByTier[tier];
|
||||||
|
|
||||||
// FIND ENTRANCE
|
// FIND ENTRANCE
|
||||||
// Search for the entrance which is farthest apart (this is why we can't reuse logic here)
|
// Search for the entrance which is farthest apart (this is why we can't reuse logic here)
|
||||||
let matchingEntrance = null;
|
let matchingEntrance = null;
|
||||||
for (let i = 0; i < range; ++i) {
|
for (let i = 0; i < range; ++i) {
|
||||||
currentPos.addInplace(offset);
|
currentPos.addInplace(offset);
|
||||||
const contents = this.root.map.getTileContent(currentPos, entity.layer);
|
const contents = this.root.map.getTileContent(currentPos, entity.layer);
|
||||||
if (!contents) {
|
if (!contents) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentsUndergroundComp = contents.components.UndergroundBelt;
|
const contentsUndergroundComp = contents.components.UndergroundBelt;
|
||||||
const contentsStaticComp = contents.components.StaticMapEntity;
|
const contentsStaticComp = contents.components.StaticMapEntity;
|
||||||
if (
|
if (
|
||||||
contentsUndergroundComp &&
|
contentsUndergroundComp &&
|
||||||
contentsUndergroundComp.tier === undergroundComp.tier &&
|
contentsUndergroundComp.tier === undergroundComp.tier &&
|
||||||
contentsUndergroundComp.mode === enumUndergroundBeltMode.sender &&
|
contentsUndergroundComp.mode === enumUndergroundBeltMode.sender &&
|
||||||
enumAngleToDirection[contentsStaticComp.rotation] === direction
|
enumAngleToDirection[contentsStaticComp.rotation] === direction
|
||||||
) {
|
) {
|
||||||
matchingEntrance = {
|
matchingEntrance = {
|
||||||
entity: contents,
|
entity: contents,
|
||||||
range: i,
|
range: i,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!matchingEntrance) {
|
if (!matchingEntrance) {
|
||||||
// Nothing found
|
// Nothing found
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DETECT OBSOLETE BELTS BETWEEN
|
// DETECT OBSOLETE BELTS BETWEEN
|
||||||
// Remove any belts between entrance and exit which have the same direction,
|
// Remove any belts between entrance and exit which have the same direction,
|
||||||
// but only if they *all* have the right direction
|
// but only if they *all* have the right direction
|
||||||
currentPos = tile.copy();
|
currentPos = tile.copy();
|
||||||
let allBeltsMatch = true;
|
let allBeltsMatch = true;
|
||||||
for (let i = 0; i < matchingEntrance.range; ++i) {
|
for (let i = 0; i < matchingEntrance.range; ++i) {
|
||||||
currentPos.addInplace(offset);
|
currentPos.addInplace(offset);
|
||||||
|
|
||||||
const contents = this.root.map.getTileContent(currentPos, entity.layer);
|
const contents = this.root.map.getTileContent(currentPos, entity.layer);
|
||||||
if (!contents) {
|
if (!contents) {
|
||||||
allBeltsMatch = false;
|
allBeltsMatch = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentsStaticComp = contents.components.StaticMapEntity;
|
const contentsStaticComp = contents.components.StaticMapEntity;
|
||||||
const contentsBeltComp = contents.components.Belt;
|
const contentsBeltComp = contents.components.Belt;
|
||||||
if (!contentsBeltComp) {
|
if (!contentsBeltComp) {
|
||||||
allBeltsMatch = false;
|
allBeltsMatch = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// It's a belt
|
// It's a belt
|
||||||
if (
|
if (
|
||||||
contentsBeltComp.direction !== enumDirection.top ||
|
contentsBeltComp.direction !== enumDirection.top ||
|
||||||
enumAngleToDirection[contentsStaticComp.rotation] !== direction
|
enumAngleToDirection[contentsStaticComp.rotation] !== direction
|
||||||
) {
|
) {
|
||||||
allBeltsMatch = false;
|
allBeltsMatch = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentPos = tile.copy();
|
currentPos = tile.copy();
|
||||||
if (allBeltsMatch) {
|
if (allBeltsMatch) {
|
||||||
// All belts between this are obsolete, so drop them
|
// All belts between this are obsolete, so drop them
|
||||||
for (let i = 0; i < matchingEntrance.range; ++i) {
|
for (let i = 0; i < matchingEntrance.range; ++i) {
|
||||||
currentPos.addInplace(offset);
|
currentPos.addInplace(offset);
|
||||||
const contents = this.root.map.getTileContent(currentPos, entity.layer);
|
const contents = this.root.map.getTileContent(currentPos, entity.layer);
|
||||||
assert(contents, "Invalid smart underground belt logic");
|
assert(contents, "Invalid smart underground belt logic");
|
||||||
this.root.logic.tryDeleteBuilding(contents);
|
this.root.logic.tryDeleteBuilding(contents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// REMOVE OBSOLETE TUNNELS
|
// REMOVE OBSOLETE TUNNELS
|
||||||
// Remove any double tunnels, by checking the tile plus the tile above
|
// Remove any double tunnels, by checking the tile plus the tile above
|
||||||
currentPos = tile.copy().add(offset);
|
currentPos = tile.copy().add(offset);
|
||||||
for (let i = 0; i < matchingEntrance.range - 1; ++i) {
|
for (let i = 0; i < matchingEntrance.range - 1; ++i) {
|
||||||
const posBefore = currentPos.copy();
|
const posBefore = currentPos.copy();
|
||||||
currentPos.addInplace(offset);
|
currentPos.addInplace(offset);
|
||||||
|
|
||||||
const entityBefore = this.root.map.getTileContent(posBefore, entity.layer);
|
const entityBefore = this.root.map.getTileContent(posBefore, entity.layer);
|
||||||
const entityAfter = this.root.map.getTileContent(currentPos, entity.layer);
|
const entityAfter = this.root.map.getTileContent(currentPos, entity.layer);
|
||||||
|
|
||||||
if (!entityBefore || !entityAfter) {
|
if (!entityBefore || !entityAfter) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const undergroundBefore = entityBefore.components.UndergroundBelt;
|
const undergroundBefore = entityBefore.components.UndergroundBelt;
|
||||||
const undergroundAfter = entityAfter.components.UndergroundBelt;
|
const undergroundAfter = entityAfter.components.UndergroundBelt;
|
||||||
|
|
||||||
if (!undergroundBefore || !undergroundAfter) {
|
if (!undergroundBefore || !undergroundAfter) {
|
||||||
// Not an underground belt
|
// Not an underground belt
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
// Both same tier
|
// Both same tier
|
||||||
undergroundBefore.tier !== undergroundAfter.tier ||
|
undergroundBefore.tier !== undergroundAfter.tier ||
|
||||||
// And same tier as our original entity
|
// And same tier as our original entity
|
||||||
undergroundBefore.tier !== undergroundComp.tier
|
undergroundBefore.tier !== undergroundComp.tier
|
||||||
) {
|
) {
|
||||||
// Mismatching tier
|
// Mismatching tier
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
undergroundBefore.mode !== enumUndergroundBeltMode.sender ||
|
undergroundBefore.mode !== enumUndergroundBeltMode.sender ||
|
||||||
undergroundAfter.mode !== enumUndergroundBeltMode.receiver
|
undergroundAfter.mode !== enumUndergroundBeltMode.receiver
|
||||||
) {
|
) {
|
||||||
// Not the right mode
|
// Not the right mode
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check rotations
|
// Check rotations
|
||||||
const staticBefore = entityBefore.components.StaticMapEntity;
|
const staticBefore = entityBefore.components.StaticMapEntity;
|
||||||
const staticAfter = entityAfter.components.StaticMapEntity;
|
const staticAfter = entityAfter.components.StaticMapEntity;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
enumAngleToDirection[staticBefore.rotation] !== direction ||
|
enumAngleToDirection[staticBefore.rotation] !== direction ||
|
||||||
enumAngleToDirection[staticAfter.rotation] !== direction
|
enumAngleToDirection[staticAfter.rotation] !== direction
|
||||||
) {
|
) {
|
||||||
// Wrong rotation
|
// Wrong rotation
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// All good, can remove
|
// All good, can remove
|
||||||
this.root.logic.tryDeleteBuilding(entityBefore);
|
this.root.logic.tryDeleteBuilding(entityBefore);
|
||||||
this.root.logic.tryDeleteBuilding(entityAfter);
|
this.root.logic.tryDeleteBuilding(entityAfter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recomputes the cache in the given area, invalidating all entries there
|
* Recomputes the cache in the given area, invalidating all entries there
|
||||||
* @param {Rectangle} area
|
* @param {Rectangle} area
|
||||||
*/
|
*/
|
||||||
recomputeArea(area) {
|
recomputeArea(area) {
|
||||||
for (let x = area.x; x < area.right(); ++x) {
|
for (let x = area.x; x < area.right(); ++x) {
|
||||||
for (let y = area.y; y < area.bottom(); ++y) {
|
for (let y = area.y; y < area.bottom(); ++y) {
|
||||||
const entities = this.root.map.getLayersContentsMultipleXY(x, y);
|
const entities = this.root.map.getLayersContentsMultipleXY(x, y);
|
||||||
for (let i = 0; i < entities.length; ++i) {
|
for (let i = 0; i < entities.length; ++i) {
|
||||||
const entity = entities[i];
|
const entity = entities[i];
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
if (!undergroundComp) {
|
if (!undergroundComp) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
undergroundComp.cachedLinkedEntity = null;
|
undergroundComp.cachedLinkedEntity = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
this.staleAreaWatcher.update();
|
this.staleAreaWatcher.update();
|
||||||
|
for (let i = this.allEntitiesArray.length - 1; i >= 0; --i) {
|
||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
const entity = this.allEntitiesArray[i];
|
||||||
const entity = this.allEntities[i];
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
|
||||||
if (undergroundComp.mode === enumUndergroundBeltMode.sender) {
|
this.handleSender(entity);
|
||||||
this.handleSender(entity);
|
} else {
|
||||||
} else {
|
this.handleReceiver(entity);
|
||||||
this.handleReceiver(entity);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Finds the receiver for a given sender
|
||||||
* Finds the receiver for a given sender
|
* @param {Entity} entity
|
||||||
* @param {Entity} entity
|
* @returns {import("../components/underground_belt").LinkedUndergroundBelt}
|
||||||
* @returns {import("../components/underground_belt").LinkedUndergroundBelt}
|
*/
|
||||||
*/
|
findRecieverForSender(entity) {
|
||||||
findRecieverForSender(entity) {
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
const staticComp = entity.components.StaticMapEntity;
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
|
||||||
const searchDirection = staticComp.localDirectionToWorld(enumDirection.top);
|
const searchVector = enumDirectionToVector[searchDirection];
|
||||||
const searchVector = enumDirectionToVector[searchDirection];
|
const targetRotation = enumDirectionToAngle[searchDirection];
|
||||||
const targetRotation = enumDirectionToAngle[searchDirection];
|
let currentTile = staticComp.origin;
|
||||||
let currentTile = staticComp.origin;
|
|
||||||
|
// Search in the direction of the tunnel
|
||||||
// Search in the direction of the tunnel
|
for (
|
||||||
for (
|
let searchOffset = 0;
|
||||||
let searchOffset = 0;
|
searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
|
||||||
searchOffset < globalConfig.undergroundBeltMaxTilesByTier[undergroundComp.tier];
|
++searchOffset
|
||||||
++searchOffset
|
) {
|
||||||
) {
|
currentTile = currentTile.add(searchVector);
|
||||||
currentTile = currentTile.add(searchVector);
|
|
||||||
|
const potentialReceiver = this.root.map.getTileContent(currentTile, "regular");
|
||||||
const potentialReceiver = this.root.map.getTileContent(currentTile, "regular");
|
if (!potentialReceiver) {
|
||||||
if (!potentialReceiver) {
|
// Empty tile
|
||||||
// Empty tile
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
const receiverUndergroundComp = potentialReceiver.components.UndergroundBelt;
|
||||||
const receiverUndergroundComp = potentialReceiver.components.UndergroundBelt;
|
if (!receiverUndergroundComp || receiverUndergroundComp.tier !== undergroundComp.tier) {
|
||||||
if (!receiverUndergroundComp || receiverUndergroundComp.tier !== undergroundComp.tier) {
|
// Not a tunnel, or not on the same tier
|
||||||
// Not a tunnel, or not on the same tier
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
const receiverStaticComp = potentialReceiver.components.StaticMapEntity;
|
||||||
const receiverStaticComp = potentialReceiver.components.StaticMapEntity;
|
if (receiverStaticComp.rotation !== targetRotation) {
|
||||||
if (receiverStaticComp.rotation !== targetRotation) {
|
// Wrong rotation
|
||||||
// Wrong rotation
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
if (receiverUndergroundComp.mode !== enumUndergroundBeltMode.receiver) {
|
||||||
if (receiverUndergroundComp.mode !== enumUndergroundBeltMode.receiver) {
|
// Not a receiver, but a sender -> Abort to make sure we don't deliver double
|
||||||
// Not a receiver, but a sender -> Abort to make sure we don't deliver double
|
break;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
|
return { entity: potentialReceiver, distance: searchOffset };
|
||||||
return { entity: potentialReceiver, distance: searchOffset };
|
}
|
||||||
}
|
|
||||||
|
// None found
|
||||||
// None found
|
return { entity: null, distance: 0 };
|
||||||
return { entity: null, distance: 0 };
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param {Entity} entity
|
||||||
* @param {Entity} entity
|
*/
|
||||||
*/
|
handleSender(entity) {
|
||||||
handleSender(entity) {
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
|
||||||
|
// Find the current receiver
|
||||||
// Find the current receiver
|
let cacheEntry = undergroundComp.cachedLinkedEntity;
|
||||||
let cacheEntry = undergroundComp.cachedLinkedEntity;
|
if (!cacheEntry) {
|
||||||
if (!cacheEntry) {
|
// Need to recompute cache
|
||||||
// Need to recompute cache
|
cacheEntry = undergroundComp.cachedLinkedEntity = this.findRecieverForSender(entity);
|
||||||
cacheEntry = undergroundComp.cachedLinkedEntity = this.findRecieverForSender(entity);
|
}
|
||||||
}
|
|
||||||
|
if (!cacheEntry.entity) {
|
||||||
if (!cacheEntry.entity) {
|
// If there is no connection to a receiver, ignore this one
|
||||||
// If there is no connection to a receiver, ignore this one
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
// Check if we have any items to eject
|
||||||
// Check if we have any items to eject
|
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
||||||
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
if (nextItemAndDuration) {
|
||||||
if (nextItemAndDuration) {
|
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending");
|
||||||
assert(undergroundComp.pendingItems.length === 1, "more than 1 pending");
|
|
||||||
|
// Check if the receiver can accept it
|
||||||
// Check if the receiver can accept it
|
if (
|
||||||
if (
|
cacheEntry.entity.components.UndergroundBelt.tryAcceptTunneledItem(
|
||||||
cacheEntry.entity.components.UndergroundBelt.tryAcceptTunneledItem(
|
nextItemAndDuration[0],
|
||||||
nextItemAndDuration[0],
|
cacheEntry.distance,
|
||||||
cacheEntry.distance,
|
this.root.hubGoals.getUndergroundBeltBaseSpeed(),
|
||||||
this.root.hubGoals.getUndergroundBeltBaseSpeed(),
|
this.root.time.now()
|
||||||
this.root.time.now()
|
)
|
||||||
)
|
) {
|
||||||
) {
|
// Drop this item
|
||||||
// Drop this item
|
fastArrayDelete(undergroundComp.pendingItems, 0);
|
||||||
fastArrayDelete(undergroundComp.pendingItems, 0);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param {Entity} entity
|
||||||
* @param {Entity} entity
|
*/
|
||||||
*/
|
handleReceiver(entity) {
|
||||||
handleReceiver(entity) {
|
const undergroundComp = entity.components.UndergroundBelt;
|
||||||
const undergroundComp = entity.components.UndergroundBelt;
|
|
||||||
|
// Try to eject items, we only check the first one because it is sorted by remaining time
|
||||||
// Try to eject items, we only check the first one because it is sorted by remaining time
|
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
||||||
const nextItemAndDuration = undergroundComp.pendingItems[0];
|
if (nextItemAndDuration) {
|
||||||
if (nextItemAndDuration) {
|
if (this.root.time.now() > nextItemAndDuration[1]) {
|
||||||
if (this.root.time.now() > nextItemAndDuration[1]) {
|
const ejectorComp = entity.components.ItemEjector;
|
||||||
const ejectorComp = entity.components.ItemEjector;
|
|
||||||
|
const nextSlotIndex = ejectorComp.getFirstFreeSlot();
|
||||||
const nextSlotIndex = ejectorComp.getFirstFreeSlot();
|
if (nextSlotIndex !== null) {
|
||||||
if (nextSlotIndex !== null) {
|
if (ejectorComp.tryEject(nextSlotIndex, nextItemAndDuration[0])) {
|
||||||
if (ejectorComp.tryEject(nextSlotIndex, nextItemAndDuration[0])) {
|
undergroundComp.pendingItems.shift();
|
||||||
undergroundComp.pendingItems.shift();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck
|
||||||
import { ReadWriteProxy } from "../core/read_write_proxy";
|
import { ReadWriteProxy } from "../core/read_write_proxy";
|
||||||
import { ExplainedResult } from "../core/explained_result";
|
import { ExplainedResult } from "../core/explained_result";
|
||||||
import { SavegameSerializer } from "./savegame_serializer";
|
import { SavegameSerializer } from "./savegame_serializer";
|
||||||
@ -168,7 +169,10 @@ export class Savegame extends ReadWriteProxy {
|
|||||||
* Returns if this game has a serialized game dump
|
* Returns if this game has a serialized game dump
|
||||||
*/
|
*/
|
||||||
hasGameDump() {
|
hasGameDump() {
|
||||||
return !!this.currentData.dump && this.currentData.dump.entities.length > 0;
|
return (
|
||||||
|
!!this.currentData.dump &&
|
||||||
|
(this.currentData.dump.entities.length > 0 || this.currentData.dump.entities.size > 0)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -67,9 +67,10 @@ export class SavegameSerializer {
|
|||||||
const seenUids = new Set();
|
const seenUids = new Set();
|
||||||
|
|
||||||
// Check for duplicate UIDS
|
// Check for duplicate UIDS
|
||||||
for (let i = 0; i < savegame.entities.length; ++i) {
|
const entities = [...savegame.entities.values()];
|
||||||
|
for (let i = 0; i < entities.length; ++i) {
|
||||||
/** @type {Entity} */
|
/** @type {Entity} */
|
||||||
const entity = savegame.entities[i];
|
const entity = entities[i];
|
||||||
|
|
||||||
const uid = entity.uid;
|
const uid = entity.uid;
|
||||||
if (!Number.isInteger(uid)) {
|
if (!Number.isInteger(uid)) {
|
||||||
@ -133,7 +134,8 @@ export class SavegameSerializer {
|
|||||||
errorReason = errorReason || root.hubGoals.deserialize(savegame.hubGoals, root);
|
errorReason = errorReason || root.hubGoals.deserialize(savegame.hubGoals, root);
|
||||||
errorReason = errorReason || root.hud.parts.pinnedShapes.deserialize(savegame.pinnedShapes);
|
errorReason = errorReason || root.hud.parts.pinnedShapes.deserialize(savegame.pinnedShapes);
|
||||||
errorReason = errorReason || root.hud.parts.waypoints.deserialize(savegame.waypoints);
|
errorReason = errorReason || root.hud.parts.waypoints.deserialize(savegame.waypoints);
|
||||||
errorReason = errorReason || this.internal.deserializeEntityArray(root, savegame.entities);
|
errorReason =
|
||||||
|
errorReason || this.internal.deserializeEntityArray(root, [...savegame.entities.values()]);
|
||||||
errorReason = errorReason || root.systemMgr.systems.belt.deserializePaths(savegame.beltPaths);
|
errorReason = errorReason || root.systemMgr.systems.belt.deserializePaths(savegame.beltPaths);
|
||||||
|
|
||||||
// Check for errors
|
// Check for errors
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
* hubGoals: any,
|
* hubGoals: any,
|
||||||
* pinnedShapes: any,
|
* pinnedShapes: any,
|
||||||
* waypoints: any,
|
* waypoints: any,
|
||||||
* entities: Array<Entity>,
|
* entities: Array<Entity>|Set<Entity>,
|
||||||
* beltPaths: Array<any>
|
* beltPaths: Array<any>
|
||||||
* }} SerializedGame
|
* }} SerializedGame
|
||||||
*
|
*
|
||||||
|
|||||||
@ -40,7 +40,7 @@ export class SavegameInterface_V1001 extends SavegameInterface_V1000 {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const entities = dump.entities;
|
const entities = Array.isArray(dump.entities) ? dump.entities : [...dump.entities.values()];
|
||||||
for (let i = 0; i < entities.length; ++i) {
|
for (let i = 0; i < entities.length; ++i) {
|
||||||
const entity = entities[i];
|
const entity = entities[i];
|
||||||
|
|
||||||
|
|||||||
@ -24,7 +24,7 @@ export class SavegameInterface_V1002 extends SavegameInterface_V1001 {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const entities = dump.entities;
|
const entities = Array.isArray(dump.entities) ? dump.entities : [...dump.entities.values()];
|
||||||
for (let i = 0; i < entities.length; ++i) {
|
for (let i = 0; i < entities.length; ++i) {
|
||||||
const entity = entities[i];
|
const entity = entities[i];
|
||||||
const beltComp = entity.components.Belt;
|
const beltComp = entity.components.Belt;
|
||||||
|
|||||||
@ -26,7 +26,7 @@ export class SavegameInterface_V1005 extends SavegameInterface_V1004 {
|
|||||||
// just reset belt paths for now
|
// just reset belt paths for now
|
||||||
dump.beltPaths = [];
|
dump.beltPaths = [];
|
||||||
|
|
||||||
const entities = dump.entities;
|
const entities = Array.isArray(dump.entities) ? dump.entities : [...dump.entities.values()];
|
||||||
|
|
||||||
// clear ejector slots
|
// clear ejector slots
|
||||||
for (let i = 0; i < entities.length; ++i) {
|
for (let i = 0; i < entities.length; ++i) {
|
||||||
|
|||||||
@ -173,7 +173,8 @@ export class SavegameInterface_V1006 extends SavegameInterface_V1005 {
|
|||||||
dump.hubGoals.level = levelMapping[level] || level;
|
dump.hubGoals.level = levelMapping[level] || level;
|
||||||
|
|
||||||
// Update entities
|
// Update entities
|
||||||
const entities = dump.entities;
|
const entities = Array.isArray(dump.entities) ? dump.entities : [...dump.entities.values()];
|
||||||
|
|
||||||
for (let i = 0; i < entities.length; ++i) {
|
for (let i = 0; i < entities.length; ++i) {
|
||||||
const entity = entities[i];
|
const entity = entities[i];
|
||||||
const components = entity.components;
|
const components = entity.components;
|
||||||
@ -269,8 +270,17 @@ export class SavegameInterface_V1006 extends SavegameInterface_V1005 {
|
|||||||
newStaticComp.originalRotation = staticComp.originalRotation;
|
newStaticComp.originalRotation = staticComp.originalRotation;
|
||||||
newStaticComp.rotation = staticComp.rotation;
|
newStaticComp.rotation = staticComp.rotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* in one of our files:
|
||||||
|
* we dont seem to actually have a blueprintspritekey
|
||||||
|
* but we do have this attribute called code
|
||||||
|
*/
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
newStaticComp.code = spriteMapping[staticComp.blueprintSpriteKey];
|
if (staticComp.blueprintSpriteKey) {
|
||||||
|
// @ts-ignore
|
||||||
|
newStaticComp.code = spriteMapping[staticComp.blueprintSpriteKey];
|
||||||
|
} else newStaticComp.code = staticComp.code;
|
||||||
|
|
||||||
// Hub special case
|
// Hub special case
|
||||||
if (entity.components.Hub) {
|
if (entity.components.Hub) {
|
||||||
@ -293,9 +303,11 @@ export class SavegameInterface_V1006 extends SavegameInterface_V1005 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!newStaticComp.code) {
|
if (!newStaticComp.code) {
|
||||||
|
console.dir(entity);
|
||||||
|
console.dir(staticComp);
|
||||||
throw new Error(
|
throw new Error(
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
"1006 Migration: Could not reconstruct code for " + staticComp.blueprintSpriteKey
|
"1006 Migration: Could not reconstruct code for " + code
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,12 +11,15 @@ const logger = createLogger("serializer_internal");
|
|||||||
export class SerializerInternal {
|
export class SerializerInternal {
|
||||||
/**
|
/**
|
||||||
* Serializes an array of entities
|
* Serializes an array of entities
|
||||||
* @param {Array<Entity>} array
|
* @param {Array<Entity>|Set<Entity>} array
|
||||||
*/
|
*/
|
||||||
serializeEntityArray(array) {
|
serializeEntityArray(array) {
|
||||||
const serialized = [];
|
const serialized = [];
|
||||||
for (let i = 0; i < array.length; ++i) {
|
|
||||||
const entity = array[i];
|
const arr = Array.isArray(array) ? array : [...array.values()];
|
||||||
|
|
||||||
|
for (let i = 0; i < arr.length; ++i) {
|
||||||
|
const entity = arr[i];
|
||||||
if (!entity.queuedForDestroy && !entity.destroyed) {
|
if (!entity.queuedForDestroy && !entity.destroyed) {
|
||||||
serialized.push(entity.serialize());
|
serialized.push(entity.serialize());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user