Skip to content

Commit

Permalink
rename to PhysicalObject3D and support common bending
Browse files Browse the repository at this point in the history
  • Loading branch information
namel committed Jul 21, 2018
1 parent aeaea85 commit e018a7b
Show file tree
Hide file tree
Showing 29 changed files with 401 additions and 430 deletions.
6 changes: 3 additions & 3 deletions es5/GameWorld.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ var GameWorld = function () {
}

/**
* Remove an object from the game world
* @param {Object} object object to remove
* Add an object to the game world
* @param {Object} object object to add
*/

}, {
Expand All @@ -125,7 +125,7 @@ var GameWorld = function () {
}

/**
* Add an object to the game world
* Remove an object from the game world
* @param {number} id id of the object to remove
*/

Expand Down
20 changes: 10 additions & 10 deletions es5/network/NetworkTransmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _Serializer = require('./../serialize/Serializer');
var _BaseTypes = require('../serialize/BaseTypes');

var _Serializer2 = _interopRequireDefault(_Serializer);
var _BaseTypes2 = _interopRequireDefault(_BaseTypes);

var _NetworkedEventFactory = require('./NetworkedEventFactory');

Expand Down Expand Up @@ -38,29 +38,29 @@ var NetworkTransmitter = function () {

this.registerNetworkedEventFactory('objectUpdate', {
netScheme: {
stepCount: { type: _Serializer2.default.TYPES.INT32 },
objectInstance: { type: _Serializer2.default.TYPES.CLASSINSTANCE }
stepCount: { type: _BaseTypes2.default.TYPES.INT32 },
objectInstance: { type: _BaseTypes2.default.TYPES.CLASSINSTANCE }
}
});

this.registerNetworkedEventFactory('objectCreate', {
netScheme: {
stepCount: { type: _Serializer2.default.TYPES.INT32 },
objectInstance: { type: _Serializer2.default.TYPES.CLASSINSTANCE }
stepCount: { type: _BaseTypes2.default.TYPES.INT32 },
objectInstance: { type: _BaseTypes2.default.TYPES.CLASSINSTANCE }
}
});

this.registerNetworkedEventFactory('objectDestroy', {
netScheme: {
stepCount: { type: _Serializer2.default.TYPES.INT32 },
objectInstance: { type: _Serializer2.default.TYPES.CLASSINSTANCE }
stepCount: { type: _BaseTypes2.default.TYPES.INT32 },
objectInstance: { type: _BaseTypes2.default.TYPES.CLASSINSTANCE }
}
});

this.registerNetworkedEventFactory('syncHeader', {
netScheme: {
stepCount: { type: _Serializer2.default.TYPES.INT32 },
fullUpdate: { type: _Serializer2.default.TYPES.UINT8 }
stepCount: { type: _BaseTypes2.default.TYPES.INT32 },
fullUpdate: { type: _BaseTypes2.default.TYPES.UINT8 }
}
});

Expand Down
10 changes: 5 additions & 5 deletions es5/network/NetworkedEventCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Object.defineProperty(exports, "__esModule", {

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _Serializer = require('./../serialize/Serializer');
var _BaseTypes = require('../serialize/BaseTypes');

var _Serializer2 = _interopRequireDefault(_Serializer);
var _BaseTypes2 = _interopRequireDefault(_BaseTypes);

var _Serializable2 = require('./../serialize/Serializable');
var _Serializable2 = require('../serialize/Serializable');

var _Serializable3 = _interopRequireDefault(_Serializable2);

Expand All @@ -33,8 +33,8 @@ var NetworkedEventCollection = function (_Serializable) {
get: function get() {
return {
events: {
type: _Serializer2.default.TYPES.LIST,
itemType: _Serializer2.default.TYPES.CLASSINSTANCE
type: _BaseTypes2.default.TYPES.LIST,
itemType: _BaseTypes2.default.TYPES.CLASSINSTANCE
}
};
}
Expand Down
112 changes: 68 additions & 44 deletions es5/serialize/DynamicObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var _Serializer = require('./Serializer');

var _Serializer2 = _interopRequireDefault(_Serializer);

var _BaseTypes = require('./BaseTypes');

var _BaseTypes2 = _interopRequireDefault(_BaseTypes);

var _MathUtils = require('../lib/MathUtils');

var _MathUtils2 = _interopRequireDefault(_MathUtils);
Expand Down Expand Up @@ -68,18 +72,18 @@ var DynamicObject = function (_GameObject) {
* @example
* static get netScheme() {
* return Object.assign({
* mojo: { type: Serializer.TYPES.UINT8 },
* mojo: { type: BaseTypes.TYPES.UINT8 },
* }, super.netScheme);
* }
*/
get: function get() {
return Object.assign({
playerId: { type: _Serializer2.default.TYPES.INT16 },
position: { type: _Serializer2.default.TYPES.CLASSINSTANCE },
width: { type: _Serializer2.default.TYPES.INT16 },
height: { type: _Serializer2.default.TYPES.INT16 },
velocity: { type: _Serializer2.default.TYPES.CLASSINSTANCE },
angle: { type: _Serializer2.default.TYPES.FLOAT32 }
playerId: { type: _BaseTypes2.default.TYPES.INT16 },
position: { type: _BaseTypes2.default.TYPES.CLASSINSTANCE },
width: { type: _BaseTypes2.default.TYPES.INT16 },
height: { type: _BaseTypes2.default.TYPES.INT16 },
velocity: { type: _BaseTypes2.default.TYPES.CLASSINSTANCE },
angle: { type: _BaseTypes2.default.TYPES.FLOAT32 }
}, _get(DynamicObject.__proto__ || Object.getPrototypeOf(DynamicObject), 'netScheme', this));
}

Expand Down Expand Up @@ -107,6 +111,7 @@ var DynamicObject = function (_GameObject) {
var _this = _possibleConstructorReturn(this, (DynamicObject.__proto__ || Object.getPrototypeOf(DynamicObject)).call(this, gameEngine, options));

_this.playerId = 0;
_this.bendingIncrements = 0;

_this.position = new _TwoVector2.default(0, 0);
_this.velocity = new _TwoVector2.default(0, 0);
Expand Down Expand Up @@ -183,8 +188,6 @@ var DynamicObject = function (_GameObject) {
*/
_this.acceleration = 0.1;

_this.bending = new _TwoVector2.default(0, 0);
_this.bendingAngle = 0;
_this.deceleration = 0.99;
return _this;
}
Expand All @@ -211,12 +214,22 @@ var DynamicObject = function (_GameObject) {
}

/**
* Formatted textual description of the game object's current bending properties.
* @return {String} description - a string description
* Each object class can define its own bending overrides.
* return an object which can include attributes: position, velocity,
* and angle. In each case, you can specify a min value, max
* value, and a percent value.
*
* @return {Object} bending - an object with bending paramters
*/

}, {
key: 'bendingToString',


/**
* Formatted textual description of the game object's current bending properties.
* @return {String} description - a string description
*/
value: function bendingToString() {
if (this.bendingIncrements) return 'bend=' + this.bending + ' angle=' + this.bendingAngle + ' num_increments=' + this.bendingIncrements;
return 'no bending';
Expand All @@ -234,58 +247,59 @@ var DynamicObject = function (_GameObject) {
_get(DynamicObject.prototype.__proto__ || Object.getPrototypeOf(DynamicObject.prototype), 'syncTo', this).call(this, other);
this.position.copy(other.position);
this.velocity.copy(other.velocity);
this.bending.copy(other.bending);
this.bendingAngle = other.bendingAngle;
this.rotationSpeed = other.rotationSpeed;
this.acceleration = other.acceleration;
this.deceleration = other.deceleration;
}
}, {
key: 'bendToCurrent',
value: function bendToCurrent(original, bending, worldSettings, isLocal, bendingIncrements) {

// TODO: the bending parameters should now be an object,
// with a single getter bendingMultiples which has local
// and remote values for position, velocity, and angle
this.bendingIncrements = bendingIncrements;
value: function bendToCurrent(original, percent, worldSettings, isLocal, increments) {

var bending = { increments: increments, percent: percent };
// if the object has defined a bending multiples for this object, use them
if (typeof this.bendingMultiple === 'number') bending = this.bendingMultiple;

// velocity bending factor
var velocityBending = bending;
if (typeof this.bendingVelocityMultiple === 'number') velocityBending = this.bendingVelocityMultiple;

// angle bending factor
var angleBending = bending;
if (typeof this.bendingAngleMultiple === 'number') angleBending = this.bendingAngleMultiple;
if (isLocal && typeof this.bendingAngleLocalMultiple === 'number') angleBending = this.bendingAngleLocalMultiple;

// bend to position, velocity, and angle gradually
// TODO: consider using lerp() method of TwoVector instead.
// you will need implement lerpWrapped() first.
if (worldSettings.worldWrap) {
this.bending.x = _MathUtils2.default.interpolateDeltaWithWrapping(original.position.x, this.position.x, bending, 0, worldSettings.width) / bendingIncrements;
this.bending.y = _MathUtils2.default.interpolateDeltaWithWrapping(original.position.y, this.position.y, bending, 0, worldSettings.height) / bendingIncrements;
} else {
this.bending.x = _MathUtils2.default.interpolateDelta(original.position.x, this.position.x, bending) / bendingIncrements;
this.bending.y = _MathUtils2.default.interpolateDelta(original.position.y, this.position.y, bending) / bendingIncrements;
var positionBending = Object.assign({}, bending, this.bending.position);
var velocityBending = Object.assign({}, bending, this.bending.velocity);
var angleBending = Object.assign({}, bending, this.bending.angle);

if (isLocal) {
Object.assign(positionBending, this.bending.positionLocal);
Object.assign(velocityBending, this.bending.velocityLocal);
Object.assign(angleBending, this.bending.angleLocal);
}
this.bendingAngle = _MathUtils2.default.interpolateDeltaWithWrapping(original.angle, this.angle, angleBending, 0, 360) / bendingIncrements;
this.velocity.x = _MathUtils2.default.interpolate(original.velocity.x, this.velocity.x, velocityBending);
this.velocity.y = _MathUtils2.default.interpolate(original.velocity.y, this.velocity.y, velocityBending);

// get the incremental delta position & velocity
this.incrementScale = percent / increments;
this.bendingPositionDelta = original.position.getBendingDelta(this.position, positionBending);
this.bendingVelocityDelta = original.velocity.getBendingDelta(this.velocity, velocityBending);
this.bendingAngleDelta = _MathUtils2.default.interpolateDeltaWithWrapping(original.angle, this.angle, angleBending.percent, 0, 2 * Math.PI) / increments;

this.bendingTarget = new this.constructor();
this.bendingTarget.syncTo(this);

// revert to original
this.position.copy(original.position);
this.velocity.copy(original.velocity);
this.angle = original.angle;

// keep parameters
this.bendingIncrements = increments;
this.bendingOptions = bending;
}
}, {
key: 'applyIncrementalBending',
value: function applyIncrementalBending() {
value: function applyIncrementalBending(stepDesc) {
if (this.bendingIncrements === 0) return;

this.position.add(this.bending);
this.angle += this.bendingAngle;
var timeFactor = 1;
if (stepDesc && stepDesc.dt) timeFactor = stepDesc.dt / (1000 / 60);

var posDelta = this.bendingPositionDelta.clone().multiplyScalar(timeFactor);
var velDelta = this.bendingVelocityDelta.clone().multiplyScalar(timeFactor);
this.position.add(posDelta);
this.velocity.add(velDelta);
this.angle += this.bendingAngleDelta * timeFactor;

this.bendingIncrements--;
}
}, {
Expand Down Expand Up @@ -357,6 +371,16 @@ var DynamicObject = function (_GameObject) {
get: function get() {
return this.position.y;
}
}, {
key: 'bending',
get: function get() {
return {
// example:
// position: { percent: 0.8, min: 0.0, max: 4.0 },
// velocity: { percent: 0.4, min: 0.0 },
// angleLocal: { percent: 0.0 }
};
}
}, {
key: 'maxSpeed',
get: function get() {
Expand Down
58 changes: 6 additions & 52 deletions es5/serialize/GameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var _Serializable2 = require('./Serializable');

var _Serializable3 = _interopRequireDefault(_Serializable2);

var _Serializer = require('./Serializer');
var _BaseTypes = require('./BaseTypes');

var _Serializer2 = _interopRequireDefault(_Serializer);
var _BaseTypes2 = _interopRequireDefault(_BaseTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

Expand All @@ -38,7 +38,7 @@ var GameObject = function (_Serializable) {
key: 'netScheme',
get: function get() {
return {
id: { type: _Serializer2.default.TYPES.INT32 }
id: { type: _BaseTypes2.default.TYPES.INT32 }
};
}

Expand Down Expand Up @@ -170,28 +170,13 @@ var GameObject = function (_Serializable) {
key: 'bendToCurrent',
value: function bendToCurrent(original, bending, worldSettings, isLocal, bendingIncrements) {}

/**
* The bending multiple is a getter, which returns the
* amount of bending.
* Bending is defined as the amount of correction that will be applied
* on the client side to a given object's position, incrementally, until the next
* server broadcast is expected to arrive.
* When this value is 0.0, the client ignores the server object's position.
* When this value is null, the bending is taken from the synchronization
* defaults. Set this to zero for objects whose position
* jumps suddenly - because the game intended a jump, not a gradual bend.
* @memberof GameObject
* @member {Number} bendingMultiple
*/

}, {
key: 'syncTo',


/**
* synchronize this object to the state of an other object
* @param {GameObject} other the other object to synchronize to
*/

}, {
key: 'syncTo',
value: function syncTo(other) {
_get(GameObject.prototype.__proto__ || Object.getPrototypeOf(GameObject.prototype), 'syncTo', this).call(this, other);
}
Expand Down Expand Up @@ -268,37 +253,6 @@ var GameObject = function (_Serializable) {
angleLocal: { percent: 1.0 }
};
}
}, {
key: 'bendingMultiple',
get: function get() {
return null;
}

/**
* The velocity bending multiple is a getter, which returns the
* amount of velocity bending.
* @memberof GameObject
* @member {Number} bendingVelocityMultiple
*/

}, {
key: 'bendingVelocityMultiple',
get: function get() {
return null;
}

/**
* The angle bending multiple is a getter, which returns the
* amount of angle bending.
* @memberof GameObject
* @member {Number} bendingAngleMultiple
*/

}, {
key: 'bendingAngleMultiple',
get: function get() {
return null;
}
}]);

return GameObject;
Expand Down
Loading

0 comments on commit e018a7b

Please sign in to comment.