clean client code
This commit is contained in:
parent
f3619ba891
commit
21a30ad212
18 changed files with 294 additions and 268 deletions
|
@ -1,36 +1,10 @@
|
|||
function Asteroid(data) {
|
||||
this.id = data.id;
|
||||
this.x = data.delta[0];
|
||||
this.y = data.delta[1];
|
||||
this.r = data.delta[2];
|
||||
this.bodyType = 'asteroid';
|
||||
this.frame = data.frame;
|
||||
this.updated = false;
|
||||
class Asteroid extends Body {
|
||||
constructor(data) {
|
||||
super(data)
|
||||
this.bodyType = 'asteroid';
|
||||
}
|
||||
|
||||
var b2Vec2 = Box2D.Common.Math.b2Vec2;
|
||||
var s = SCALE;
|
||||
tick() {
|
||||
|
||||
this.getPos = function() {
|
||||
var pos = this.b2body.GetPosition();
|
||||
var angle = this.b2body.GetAngle();
|
||||
return {
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
r: angle
|
||||
};
|
||||
};
|
||||
|
||||
this.update = function(data) {
|
||||
this.x = data[0];
|
||||
this.y = data[1];
|
||||
this.xvel = data[2]
|
||||
this.yvel = data[3];
|
||||
this.r = data[4];
|
||||
this.rvel = data[5];
|
||||
this.updated = 10;
|
||||
};
|
||||
|
||||
this.tick = function() {
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
57
public/js/starbugs/world/body.js
Normal file
57
public/js/starbugs/world/body.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
//@10
|
||||
|
||||
class Body {
|
||||
constructor(data) {
|
||||
this.x = data.delta[0];
|
||||
this.y = data.delta[1];
|
||||
this.xvel = data.delta[2];
|
||||
this.yvel = data.delta[3];
|
||||
this.r = data.delta[4];
|
||||
this.rvel = data.delta[5];
|
||||
|
||||
this.id = data.id
|
||||
this.frame = data.frame;
|
||||
this.b2body = false;
|
||||
this.updated = 0;
|
||||
|
||||
this.com = {
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
}
|
||||
|
||||
getPos() {
|
||||
var pos = this.b2body.GetPosition();
|
||||
var angle = this.b2body.GetAngle();
|
||||
return {
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
r: angle
|
||||
};
|
||||
}
|
||||
|
||||
applyForce(x, y) {
|
||||
var b = this.b2body;
|
||||
b.ApplyForce(new b2Vec2(x, y), b.GetWorldCenter());
|
||||
}
|
||||
|
||||
applyTorque(f) {
|
||||
this.b2body.ApplyTorque(f);
|
||||
}
|
||||
|
||||
update(data) {
|
||||
this.x = data[0];
|
||||
this.y = data[1];
|
||||
this.xvel = data[2]
|
||||
this.yvel = data[3];
|
||||
this.r = data[4];
|
||||
this.rvel = data[5];
|
||||
this.updated = 10;
|
||||
|
||||
this.updateType(data);
|
||||
}
|
||||
|
||||
updateType() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,27 +1,22 @@
|
|||
function Physics() {
|
||||
var b2Vec2 = Box2D.Common.Math.b2Vec2;
|
||||
var b2World = Box2D.Dynamics.b2World;
|
||||
var b2Body = Box2D.Dynamics.b2Body;
|
||||
var b2BodyDef = Box2D.Dynamics.b2BodyDef;
|
||||
var b2Fixture = Box2D.Dynamics.b2Fixture;
|
||||
var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
|
||||
var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
|
||||
class Physics {
|
||||
constructor() {
|
||||
|
||||
this.world = new b2World(new b2Vec2(0, 0));
|
||||
this.toRemove = [];
|
||||
this.world = new b2World(new b2Vec2(0, 0));
|
||||
this.toRemove = [];
|
||||
|
||||
var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
|
||||
var debugDraw = new b2DebugDraw();
|
||||
debugDraw.SetSprite(document.getElementById("starbugs_canvas").getContext("2d"));
|
||||
debugDraw.SetDrawScale(SCALE);
|
||||
debugDraw.SetFillAlpha(0.3);
|
||||
debugDraw.SetLineThickness(1.0);
|
||||
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
|
||||
this.world.SetDebugDraw(debugDraw);
|
||||
var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
|
||||
var debugDraw = new b2DebugDraw();
|
||||
debugDraw.SetSprite(document.getElementById("starbugs_canvas").getContext("2d"));
|
||||
debugDraw.SetDrawScale(SCALE);
|
||||
debugDraw.SetFillAlpha(0.3);
|
||||
debugDraw.SetLineThickness(1.0);
|
||||
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
|
||||
this.world.SetDebugDraw(debugDraw);
|
||||
}
|
||||
|
||||
this.createBody = function(body) {
|
||||
var s = SCALE;
|
||||
var bodyDef = new b2BodyDef();
|
||||
createBody(body) {
|
||||
let s = SCALE;
|
||||
let bodyDef = new b2BodyDef();
|
||||
bodyDef.userData = body;
|
||||
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
|
||||
bodyDef.fixedRotation = false;
|
||||
|
@ -54,7 +49,7 @@ function Physics() {
|
|||
|
||||
body.com = b2body.GetLocalCenter();
|
||||
if (body.bodyType == 'ship') {
|
||||
console.log(body.getPos());
|
||||
//console.log(body.getPos());
|
||||
//console.log(b2body.GetLocalCenter());
|
||||
//console.log(body);
|
||||
//console.log(b2body.GetMass());
|
||||
|
@ -62,11 +57,11 @@ function Physics() {
|
|||
}
|
||||
}
|
||||
|
||||
this.removeBody = function(body) {
|
||||
removeBody(body) {
|
||||
this.toRemove.push(body.b2body);
|
||||
}
|
||||
|
||||
this.step = function() {
|
||||
step() {
|
||||
this.world.Step(1, 5, 1 / 60);
|
||||
this.world.ClearForces();
|
||||
//this.world.DrawDebugData();
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
function Player(own, name, ship) {
|
||||
this.name = name;
|
||||
this.ship = ship;
|
||||
this.own = own;
|
||||
class Player {
|
||||
constructor(name, team, ship) {
|
||||
this.name = name;
|
||||
this.team = team;
|
||||
this.ship = ship;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,75 +1,38 @@
|
|||
function Ship(data) {
|
||||
this.id = data.id;
|
||||
this.x = data.delta[0];
|
||||
this.y = data.delta[1];
|
||||
this.r = data.delta[2];
|
||||
this.team = data.team;
|
||||
this.name = data.name;
|
||||
this.hull = '01';
|
||||
this.move = [];
|
||||
this.thrust = {};
|
||||
this.power = data.power;
|
||||
this.mounts = data.mounts;
|
||||
this.turrets = data.turrets;
|
||||
this.frame = data.frame;
|
||||
this.size = {
|
||||
'small': 8,
|
||||
'medium': 16,
|
||||
'large': 24
|
||||
}[data.size];
|
||||
this.lastMove = [];
|
||||
this.b2body = false;
|
||||
this.bodyType = 'ship';
|
||||
this.com = {
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
class Ship extends Body {
|
||||
constructor(data) {
|
||||
super(data);
|
||||
this.player = new Player(data.name, data.team, this);
|
||||
this.team = data.team;
|
||||
this.name = data.name;
|
||||
this.hull = '01';
|
||||
this.move = [];
|
||||
this.thrust = {};
|
||||
this.power = data.power;
|
||||
this.mounts = data.mounts;
|
||||
this.turrets = data.turrets;
|
||||
this.size = {
|
||||
'small': 8,
|
||||
'medium': 16,
|
||||
'large': 24
|
||||
}[data.size];
|
||||
this.lastMove = [];
|
||||
this.bodyType = 'ship';
|
||||
}
|
||||
|
||||
var b2Vec2 = Box2D.Common.Math.b2Vec2;
|
||||
var s = SCALE;
|
||||
|
||||
this.getPos = function() {
|
||||
var pos = this.b2body.GetPosition();
|
||||
var angle = this.b2body.GetAngle();
|
||||
return {
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
r: angle
|
||||
}
|
||||
};
|
||||
|
||||
this.update = function (data) {
|
||||
this.x = data[0];
|
||||
this.y = data[1];
|
||||
this.xvel = data[2]
|
||||
this.yvel = data[3];
|
||||
this.r = data[4];
|
||||
this.rvel = data[5];
|
||||
this.updated = 10;
|
||||
this.thrust = {
|
||||
forward: data[6],
|
||||
left: data[7],
|
||||
right: data[8]
|
||||
}
|
||||
};
|
||||
|
||||
this.updateMove = function() {
|
||||
updateMove() {
|
||||
if (JSON.stringify(this.move) != JSON.stringify(this.lastMove) || true) {
|
||||
game.net.update(this.move);
|
||||
this.lastMove = Array.apply(0, this.move); // Bloody Javascript.
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
this.applyForce = function(x, y) {
|
||||
var b = this.b2body;
|
||||
b.ApplyForce(new b2Vec2(x, y), b.GetWorldCenter());
|
||||
};
|
||||
updateType(data) {
|
||||
this.thrust = {
|
||||
forward: data[6]
|
||||
}
|
||||
}
|
||||
|
||||
this.applyTorque = function(f) {
|
||||
this.b2body.ApplyTorque(f);
|
||||
};
|
||||
|
||||
this.tick = function() {
|
||||
tick() {
|
||||
if (this.move[0]) {
|
||||
var power = this.power.forward;
|
||||
var x = Math.cos(this.getPos().r) * power;
|
||||
|
@ -84,5 +47,5 @@ function Ship(data) {
|
|||
if (this.move[2]) {
|
||||
this.applyTorque(this.power.rotation);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,27 @@
|
|||
var SCALE = 32;
|
||||
|
||||
function World() {
|
||||
this.bodies = {};
|
||||
this.playerShip = false;
|
||||
this.playerShipId = false;
|
||||
this.physics = new Physics();
|
||||
class World {
|
||||
constructor() {
|
||||
this.bodies = {};
|
||||
this.playerShip = false;
|
||||
this.playerShipId = false;
|
||||
this.physics = new Physics();
|
||||
|
||||
var fr = false;
|
||||
this.bounds = {
|
||||
left: -50,
|
||||
right: 50,
|
||||
top: -50,
|
||||
bottom: 50
|
||||
}
|
||||
}
|
||||
|
||||
this.getCenter = function() {
|
||||
getCenter() {
|
||||
if (!this.playerShip) return { x: 0, y: 0 };
|
||||
|
||||
var x = this.playerShip.getPos().x * SCALE;
|
||||
var y = this.playerShip.getPos().y * SCALE;
|
||||
var comx = this.playerShip.com.x * SCALE;
|
||||
var comy = this.playerShip.com.y * SCALE;
|
||||
comx = 0;
|
||||
comy = 0;
|
||||
var r = this.playerShip.getPos().r;
|
||||
var d = Math.sqrt(comx * comx + comy * comy);
|
||||
var a = Math.atan2(comy, comx);
|
||||
|
@ -27,33 +32,33 @@ function World() {
|
|||
return { x: x, y: y };
|
||||
};
|
||||
|
||||
this.add = function(data) {
|
||||
add(data) {
|
||||
var body;
|
||||
if (data.type == 'asteroid') body = new Asteroid(data);
|
||||
if (data.type == 'ship') body = new Ship(data);
|
||||
if (data.type == 'structure') body = new Structure(data);
|
||||
|
||||
if(data.type == 'ship') console.log(body);
|
||||
//if(data.type == 'ship') console.log(body);
|
||||
|
||||
this.bodies[body.id] = body;
|
||||
if(data.type == 'ship') console.log(this.bodies);
|
||||
//if(data.type == 'ship') console.log(this.bodies);
|
||||
this.physics.createBody(body);
|
||||
};
|
||||
|
||||
this.remove = function(id) {
|
||||
console.log(id);
|
||||
remove(id) {
|
||||
//console.log(id);
|
||||
this.physics.removeBody(this.bodies[id]);
|
||||
delete this.bodies[id];
|
||||
};
|
||||
|
||||
this.clear = function() {
|
||||
clear() {
|
||||
for (var i in this.bodies) {
|
||||
this.remove(i);
|
||||
}
|
||||
this.playerShip = false;
|
||||
};
|
||||
|
||||
this.update = function(data) {
|
||||
update(data) {
|
||||
this.playerShip = this.bodies[this.playerShipId];
|
||||
|
||||
for (var id in data) {
|
||||
|
@ -69,7 +74,7 @@ function World() {
|
|||
}
|
||||
};
|
||||
|
||||
this.tick = function() {
|
||||
tick() {
|
||||
this.physics.step();
|
||||
|
||||
for (var i in this.bodies) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue