updated wolfie2d

This commit is contained in:
OfficialCHenry 2022-04-13 23:09:40 -04:00
parent d19a52e184
commit 1f0764087a
4 changed files with 153 additions and 323 deletions

View File

@ -247,7 +247,7 @@ export default class Input {
* @returns True if the mouse was just pressed, false otherwise * @returns True if the mouse was just pressed, false otherwise
*/ */
static isMouseJustPressed(mouseButton?: number): boolean { static isMouseJustPressed(mouseButton?: number): boolean {
if (mouseButton) { if (mouseButton !== undefined) {
return Input.mouseJustPressed && !Input.mouseDisabled && mouseButton == this.mouseButtonPressed; return Input.mouseJustPressed && !Input.mouseDisabled && mouseButton == this.mouseButtonPressed;
} }
return Input.mouseJustPressed && !Input.mouseDisabled; return Input.mouseJustPressed && !Input.mouseDisabled;
@ -260,7 +260,7 @@ export default class Input {
* @returns True if the mouse is currently pressed, false otherwise * @returns True if the mouse is currently pressed, false otherwise
*/ */
static isMousePressed(mouseButton?: number): boolean { static isMousePressed(mouseButton?: number): boolean {
if (mouseButton) { if (mouseButton !== undefined) {
return Input.mousePressed && !Input.mouseDisabled && mouseButton == this.mouseButtonPressed; return Input.mousePressed && !Input.mouseDisabled && mouseButton == this.mouseButtonPressed;
} }
return Input.mousePressed && !Input.mouseDisabled; return Input.mousePressed && !Input.mouseDisabled;
@ -299,14 +299,12 @@ export default class Input {
return Input.mousePosition.clone().scale(1 / this.viewport.getZoomLevel()).add(Input.viewport.getOrigin()); return Input.mousePosition.clone().scale(1 / this.viewport.getZoomLevel()).add(Input.viewport.getOrigin());
} }
// -- MODIFIED BY HENRY -> added a scaling
/** /**
* Gets the position of the last mouse press * Gets the position of the last mouse press
* @returns The mouse position stored as a Vec2 * @returns The mouse position stored as a Vec2
*/ */
static getMousePressPosition(): Vec2 { static getMousePressPosition(): Vec2 {
return Input.mousePressPosition.scaled(1 / this.viewport.getZoomLevel()); return Input.getMousePosition();
} }
/** /**

View File

@ -84,6 +84,7 @@ export default abstract class UIElement extends CanvasNode {
let clickPos = Input.getMousePressPosition(); let clickPos = Input.getMousePressPosition();
if(this.contains(clickPos.x, clickPos.y) && this.visible && !this.layer.isHidden()){ if(this.contains(clickPos.x, clickPos.y) && this.visible && !this.layer.isHidden()){
this.isClicked = true; this.isClicked = true;
if(this.onClick !== null){ if(this.onClick !== null){
this.onClick(); this.onClick();
} }

View File

@ -33,48 +33,9 @@ export default class UIElementRenderer {
* @param label The label to render * @param label The label to render
*/ */
renderLabel(label: Label): void { renderLabel(label: Label): void {
// // If the size is unassigned (by the user or automatically) assign it
// label.handleInitialSizing(this.ctx);
// // Grab the global alpha so we can adjust it for this render
// let previousAlpha = this.ctx.globalAlpha;
// // Get the font and text position in label
// this.ctx.font = label.getFontString();
// let offset = label.calculateTextOffset(this.ctx);
// // Stroke and fill a rounded rect and give it text
// this.ctx.globalAlpha = label.backgroundColor.a;
// this.ctx.fillStyle = label.calculateBackgroundColor().toStringRGBA();
// this.ctx.fillRoundedRect(-label.size.x/2, -label.size.y/2,
// label.size.x, label.size.y, label.borderRadius);
// this.ctx.strokeStyle = label.calculateBorderColor().toStringRGBA();
// this.ctx.globalAlpha = label.borderColor.a;
// this.ctx.lineWidth = label.borderWidth;
// this.ctx.strokeRoundedRect(-label.size.x/2, -label.size.y/2,
// label.size.x, label.size.y, label.borderRadius);
// this.ctx.fillStyle = label.calculateTextColor();
// this.ctx.globalAlpha = label.textColor.a;
// this.ctx.fillText(label.text, offset.x - label.size.x/2, offset.y - label.size.y/2);
// this.ctx.globalAlpha = previousAlpha;
// If the size is unassigned (by the user or automatically) assign it // If the size is unassigned (by the user or automatically) assign it
let lines = label.text.split('\n');
let tempText = label.text;
if (lines.length > 1) {
let max = 0;
let maxLengthIndex = 0;
for (let i = 0; i < lines.length; i++) {
if (lines[i].length > max) {
max = lines[i].length;
maxLengthIndex = i;
}
}
label.text = lines[maxLengthIndex];
}
label.handleInitialSizing(this.ctx); label.handleInitialSizing(this.ctx);
// Grab the global alpha so we can adjust it for this render // Grab the global alpha so we can adjust it for this render
let previousAlpha = this.ctx.globalAlpha; let previousAlpha = this.ctx.globalAlpha;
@ -92,22 +53,13 @@ export default class UIElementRenderer {
this.ctx.globalAlpha = label.borderColor.a; this.ctx.globalAlpha = label.borderColor.a;
this.ctx.lineWidth = label.borderWidth; this.ctx.lineWidth = label.borderWidth;
this.ctx.strokeRoundedRect(-label.size.x/2, -label.size.y/2, this.ctx.strokeRoundedRect(-label.size.x/2, -label.size.y/2,
label.size.x, label.size.y + ((lines.length - 1) * label.fontSize), label.borderRadius); label.size.x, label.size.y, label.borderRadius);
this.ctx.fillStyle = label.calculateTextColor(); this.ctx.fillStyle = label.calculateTextColor();
this.ctx.globalAlpha = label.textColor.a; this.ctx.globalAlpha = label.textColor.a;
if (lines.length === 1) { this.ctx.fillText(label.text, offset.x - label.size.x/2, offset.y - label.size.y/2);
this.ctx.fillText(label.text, offset.x - label.size.x / 2, (offset.y - label.size.y / 2));
} else {
for (let i = 0; i < lines.length; i++) {
let additionalY = i * (label.size.y / 2 + (label.fontSize === 40 ? 20 : 10));
label.text = lines[i];
offset = label.calculateTextOffset(this.ctx);
this.ctx.fillText(lines[i], (offset.x - label.size.x / 2), (offset.y - label.size.y / 2 + additionalY));
}
}
this.ctx.globalAlpha = previousAlpha; this.ctx.globalAlpha = previousAlpha;
label.text = tempText;
} }
/** /**

View File

@ -81,10 +81,6 @@ export default class ResourceManager {
private loadonly_gl_ShaderProgramsToLoad: number; private loadonly_gl_ShaderProgramsToLoad: number;
private loadonly_gl_ShaderLoadingQueue: Queue<KeyPath_Shader>; private loadonly_gl_ShaderLoadingQueue: Queue<KeyPath_Shader>;
private loadonly_tilemapObjectToLoad: number;
private loadonly_tilemapObjectLoaded: number;
private loadonly_tilemapObjectLoadingQueue: Queue<KeyMapPair>;
private gl_ShaderPrograms: Map<WebGLProgramType>; private gl_ShaderPrograms: Map<WebGLProgramType>;
private gl_Textures: Map<number>; private gl_Textures: Map<number>;
@ -141,10 +137,6 @@ export default class ResourceManager {
this.resourcesToUnload = new Array(); this.resourcesToUnload = new Array();
this.resourcesToKeep = new Array(); this.resourcesToKeep = new Array();
this.loadonly_tilemapObjectToLoad = 0;
this.loadonly_tilemapObjectToLoad = 0;
this.loadonly_tilemapObjectLoadingQueue = new Queue();
}; };
/* ######################################## SINGLETON ########################################*/ /* ######################################## SINGLETON ########################################*/
@ -198,9 +190,9 @@ export default class ResourceManager {
*/ */
public getImage(key: string): HTMLImageElement { public getImage(key: string): HTMLImageElement {
let image = this.images.get(key); let image = this.images.get(key);
// if (image === undefined) { if(image === undefined){
// throw `There is no image associated with key "${key}"` throw `There is no image associated with key "${key}"`
// } }
return image; return image;
} }
@ -319,9 +311,6 @@ export default class ResourceManager {
this.loading = true; this.loading = true;
// Load everything in the queues. Tilemaps have to come before images because they will add new images to the queue // Load everything in the queues. Tilemaps have to come before images because they will add new images to the queue
this.loadTilemapObjectFromQueue(() => {
console.log("Loaded TilemapObjects");
this.loadTilemapsFromQueue(() => { this.loadTilemapsFromQueue(() => {
console.log("Loaded Tilemaps"); console.log("Loaded Tilemaps");
this.loadSpritesheetsFromQueue(() => { this.loadSpritesheetsFromQueue(() => {
@ -346,7 +335,6 @@ export default class ResourceManager {
}); });
}); });
}); });
});
} }
private finishLoading(callback: Function): void { private finishLoading(callback: Function): void {
@ -986,110 +974,6 @@ export default class ResourceManager {
/ this.loadonly_typesToLoad; / this.loadonly_typesToLoad;
} }
// Customized funtions below
// These funtions are NOT well tested!!!
// Only used for shattered sword specific purpose!!!
// Use them carefully!!!
public tilemapFromObject(key: string, tilemap: TiledTilemapData): void {
this.loadonly_tilemapObjectLoadingQueue.enqueue({ key: key, map: tilemap });
}
private loadTilemapObjectFromQueue(onFinishLoading: Function) {
this.loadonly_tilemapObjectToLoad = this.loadonly_tilemapObjectLoadingQueue.getSize();
this.loadonly_tilemapObjectLoaded = 0;
// If no items to load, we're finished
if (this.loadonly_tilemapObjectToLoad === 0) {
onFinishLoading();
return;
}
while (this.loadonly_tilemapObjectLoadingQueue.hasItems()) {
let map = this.loadonly_tilemapObjectLoadingQueue.dequeue();
this.loadTilemapFromObject(map.key, map.map, onFinishLoading);
}
}
private loadTilemapFromObject(key: string, tiledMap: TiledTilemapData, callbackIfLast: Function): void {
// We can parse the object later - it's much faster than loading
this.tilemaps.add(key, tiledMap);
let resource = new ResourceReference(key, ResourceType.TILEMAP);
// Grab the tileset images we need to load and add them to the imageloading queue
for (let tileset of tiledMap.tilesets) {
if (tileset.image) {
let key = tileset.image;
let path = key;
this.loadonly_imageLoadingQueue.enqueue({ key: key, path: path, isDependency: true });
// Add this image as a dependency to the tilemap
resource.addDependency(new ResourceReference(key, ResourceType.IMAGE));
}
}
// Add the resource reference to the list of resource to unload
this.resourcesToUnload.push(resource);
this.finishLoadingTilemapObject(callbackIfLast);
}
private finishLoadingTilemapObject(callback: Function): void {
this.loadonly_tilemapObjectLoaded += 1;
if (this.loadonly_tilemapObjectLoaded === this.loadonly_tilemapObjectToLoad) {
// We're done loading tilemaps
callback();
}
}
public singleImage(key: string, path: string, callbackIfLast: Function): void {
var image = new Image();
image.onload = () => {
// Add to loaded images
this.images.add(key, image);
this.resourcesToUnload.push(new ResourceReference(key, ResourceType.IMAGE));
// If WebGL is active, create a texture
if (this.gl_WebGLActive) {
this.createWebGLTexture(key, image);
}
// Finish image load
this.finishLoadingSingleObject(callbackIfLast);
}
image.src = path;
}
public singleAudio(key: string, path: string, callbackIfLast: Function): void {
let audioCtx = AudioManager.getInstance().getAudioContext();
let request = new XMLHttpRequest();
request.open('GET', path, true);
request.responseType = 'arraybuffer';
request.onload = () => {
audioCtx.decodeAudioData(request.response, (buffer) => {
// Add to list of audio buffers
this.audioBuffers.add(key, buffer);
this.resourcesToUnload.push(new ResourceReference(key, ResourceType.AUDIO));
// Finish loading sound
this.finishLoadingSingleObject(callbackIfLast);
}, (error) => {
throw "Error loading sound";
});
}
request.send();
}
private finishLoadingSingleObject(callback: Function): void {
callback();
}
update(deltaT: number): void { update(deltaT: number): void {
if(this.loading){ if(this.loading){
if(this.onLoadProgress){ if(this.onLoadProgress){
@ -1144,11 +1028,6 @@ class KeyPathPair {
isDependency?: boolean = false; isDependency?: boolean = false;
} }
class KeyMapPair {
key: string;
map: TiledTilemapData;
}
class KeyPath_Shader { class KeyPath_Shader {
key: string; key: string;
vpath: string; vpath: string;