Performance optimizations and refacturing.

This commit is contained in:
Mal
2020-02-12 23:30:04 +01:00
parent b06186043f
commit 72589544bd
6 changed files with 138 additions and 26 deletions

View File

@@ -7,6 +7,8 @@ import FileLoader from "./FileLoader.js";
import Camera from "./Camera.js";
import Gisela from "./Gisela.js";
import Setting from "./Setting.js";
import InterfaceEvent from "./events/InterfaceEvent.js";
import FrameRateMeasurer from "./FrameRateMeasurer.js";
class ImageLoader
{
@@ -27,8 +29,10 @@ class ImageLoader
return this.numberImagesLoaded / this.images.length;
}
addImage(image)
addImage(imagePath)
{
let image = new Image();
image.src = imagePath;
image.addEventListener(
'load', () => {
this.update();
@@ -125,7 +129,7 @@ function MainLoop(timestamp)
camera.lockCameraIntoBorders();
/* Drawing */
if (timestamp - lastRendered >= FRAME_DURATION) {
if (timestamp - lastRendered >= frameDuration) {
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
architecture.draw(context, camera);
@@ -151,10 +155,10 @@ function MainLoop(timestamp)
window.requestAnimationFrame(MainLoop);
}
const FPS = 120;
const FRAME_DURATION = 1000 / FPS;
const GAME_SPEED = 1;
const GRAVITY = 2;
let fps;
let frameDuration;
let levelJson = new FileLoader('levels/level01.json');
@@ -174,21 +178,12 @@ let KeyJump = new Key('Space');
let loader = new ImageLoader();
let imgAnimation = new Image();
imgAnimation.src = Setting.GRAPHICS_LOCATION + 'mr-croc-walk-right.png';
loader.addImage(imgAnimation);
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-right.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-left.png');
loader.addImage(Setting.TILESET_LOCATION + 'landscape01.jpg');
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-right.png');
let imgAnimationB = new Image();
imgAnimationB.src = Setting.GRAPHICS_LOCATION + 'mr-croc-walk-left.png';
loader.addImage(imgAnimationB);
let imgArch = new Image();
imgArch.src = Setting.TILESET_LOCATION + 'landscape01.jpg';
loader.addImage(imgArch);
let imgGisela = new Image();
imgGisela.src = Setting.GRAPHICS_LOCATION + 'gisela-right.png';
loader.addImage(imgGisela);
new FrameRateMeasurer();
window.addEventListener(
'imagesloaded',
@@ -217,6 +212,8 @@ window.addEventListener(
gisela = new Gisela();
architecture.setMovableToTargetPosition(gisela);
fps = 120; //event.frameRate;
frameDuration = 1000 / fps;
window.requestAnimationFrame(MainLoop);
}
);
);