|
export const LayerType = { |
|
GAME: 'game', |
|
GUI: 'gui', |
|
DEBUG: 'debug', |
|
TERMINAL: 'terminal' |
|
}; |
|
|
|
export class Layer { |
|
constructor(type, parent = null) { |
|
this.type = type; |
|
this.parent = null; |
|
this.child = null; |
|
this.entities = []; |
|
this.visible = true; |
|
this.inputEnabled = true; |
|
this.zIndex = 0; |
|
|
|
if (parent) { |
|
this.setParent(parent); |
|
} |
|
} |
|
|
|
setParent(parent) { |
|
if (parent.child) { |
|
console.warn('Parent already has a child, removing existing child'); |
|
parent.child.parent = null; |
|
} |
|
this.parent = parent; |
|
parent.child = this; |
|
this.zIndex = parent.zIndex + 1; |
|
} |
|
|
|
removeParent() { |
|
if (this.parent) { |
|
this.parent.child = null; |
|
this.parent = null; |
|
} |
|
} |
|
|
|
getRoot() { |
|
return this.parent ? this.parent.getRoot() : this; |
|
} |
|
|
|
addEntity(entity) { |
|
if (this.type === 'root') { |
|
console.warn('Cannot add entities to root layer'); |
|
return; |
|
} |
|
this.entities.push(entity); |
|
} |
|
|
|
draw(ctx, camera) { |
|
if (!this.visible) return; |
|
|
|
|
|
this.entities.forEach(entity => { |
|
if (typeof entity.draw === 'function') { |
|
entity.draw(ctx, camera); |
|
} |
|
}); |
|
|
|
|
|
if (this.child) { |
|
this.child.draw(ctx, camera); |
|
} |
|
} |
|
|
|
enableInput() { |
|
this.inputEnabled = true; |
|
|
|
|
|
} |
|
|
|
disableInput() { |
|
this.inputEnabled = false; |
|
|
|
|
|
} |
|
|
|
clear() { |
|
this.entities = []; |
|
} |
|
|
|
toggle() { |
|
this.visible = !this.visible; |
|
} |
|
|
|
|
|
handleKeyDown(event) { } |
|
handleMouseMove(x, y) { } |
|
handleClick(x, y) { } |
|
handleWheel(event) { } |
|
} |
|
|
|
export class GameLayer extends Layer { |
|
constructor(parent = null) { |
|
super(LayerType.GAME, parent); |
|
} |
|
|
|
handleKeyDown(event) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleGameInput) entity.handleGameInput(event); |
|
}); |
|
} |
|
} |
|
|
|
export class GUILayer extends Layer { |
|
constructor(parent = null) { |
|
super(LayerType.GUI, parent); |
|
} |
|
|
|
handleClick(x, y) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleClick) entity.handleClick(x, y); |
|
}); |
|
} |
|
|
|
handleMouseMove(x, y) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleMouseMove) entity.handleMouseMove(x, y); |
|
}); |
|
} |
|
} |
|
|
|
export class DebugLayer extends Layer { |
|
constructor(parent = null) { |
|
super(LayerType.DEBUG, parent); |
|
} |
|
|
|
handleMouseMove(x, y) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleMouseMove) entity.handleMouseMove(x, y); |
|
}); |
|
} |
|
|
|
handleClick(x, y) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleClick) entity.handleClick(x, y); |
|
}); |
|
} |
|
|
|
handleWheel(event) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleWheel) entity.handleWheel(event); |
|
}); |
|
} |
|
} |
|
|
|
export class TerminalLayer extends Layer { |
|
constructor(parent = null) { |
|
super(LayerType.TERMINAL, parent); |
|
} |
|
|
|
handleKeyDown(event) { |
|
if (!this.inputEnabled) return; |
|
this.entities.forEach(entity => { |
|
if (entity.handleKeyInput) entity.handleKeyInput(event.key); |
|
}); |
|
} |
|
} |
|
|