JSFiddle - React, Tailwind, and code Playground
by a_bertrand
HTML
Time: <span id="result">-- RUNNING --</span>
JavaScript
// A JS Performance test
var Actor = (function () {
function Actor() {
this.X = 0;
this.Y = 0;
this.GoalX = 0;
this.GoalY = 0;
this.Direction = null;
this.Speed = 2;
this.CollisionSize = 16;
this.path = null;
this.lastPath = 1000;
this.reached = false;
this.waitTimer = 0;
}
Actor.prototype.Render = function (ctx) {
// Non visible? Skip
if (this.X - 16 - game.viewX < -16 || this.Y - 16 - game.viewY < -16 || this.X - 16 - game.viewX > game.width + 16 || this.Y - 16 - game.viewY > game.height + 16)
return;
ctx.drawImage(game.tiles, 0, 6 * 32, 32, 32, Math.round(this.X - 16 - game.viewX), Math.round(this.Y - 16 - game.viewY), 32, 32);
};
Actor.prototype.Handle = function () {
if (this.reached) {
return;
}
var a = this.GoalX - this.X;
var b = this.GoalY - this.Y;
var d = Math.sqrt(a * a + b * b);
if (!this.path && d > 16) {
if (game.nbPathAvailable > 0) {
game.nbPathAvailable--;
this.lastPath = 0;
var step = this.Speed * 8;
if (d < 128)
var step = this.Speed;
var gx = this.GoalX;
var gy = this.GoalY;
for (var i = 0; i < 60000 && this.Collide(gx, gy); i++) {
gx = this.GoalX + Math.cos(i / 20) * i / 100;
gy = this.GoalY + Math.sin(i / 20) * i / 100;
}
if (!this.Collide(gx, gy)) {
this.path = PathSolver.Solve(this.X, this.Y, gx, gy, 30000, step, this);
}
}
else {
this.lastPath++;
return;
}
}
if (this.path && this.path.length == 0) {
this.waitTimer = 1000;
this.reached = true;
}
else if (this.path &&...