mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Performance optimizations and refacturing.
This commit is contained in:
35
js/module.js
35
js/module.js
@@ -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);
|
||||
}
|
||||
);
|
||||
);
|
||||
Reference in New Issue
Block a user