1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 00:54:14 +02:00

code organization and some es6

This commit is contained in:
Robin Appelman 2016-12-18 15:10:55 +01:00
commit 653bd862bd
34 changed files with 471 additions and 417 deletions

20
src/Data/Entity.js Normal file
View file

@ -0,0 +1,20 @@
export class Entity {
constructor(serverClass, sentTable, entityIndex, serialNumber) {
this.serverClass = serverClass;
this.sendTable = sentTable;
this.entityIndex = entityIndex;
this.serialNumber = serialNumber;
this.props = [];
this.inPVS = false;
}
getPropByDefinition(definition) {
for (let i = 0; i < this.props; i++) {
if (this.props[i].definition === definition) {
return this.props[i];
}
}
return null;
}
}

15
src/Data/SendProp.js Normal file
View file

@ -0,0 +1,15 @@
import clone from 'clone';
export class SentProp {
constructor(definition) {
this.definition = definition;
this.value = null;
}
clone() {
const prop = new SentProp(this.definition);
prop.value = clone(this.value);
return prop;
}
}

View file

@ -0,0 +1,90 @@
export class SendPropDefinition {
constructor(type, name, flags) {
this.type = type;
this.name = name;
this.flags = flags;
this.excludeDTName = null;
this.lowValue = 0;
this.highValue = 0;
this.bitCount = 0;
this.table = null;
this.numElements = null;
this.arrayProperty = null;
}
hasFlag(flag) {
return (this.flags & flag) != 0;
}
isExcludeProp() {
return this.hasFlag(SendPropDefinition.flags.SPROP_EXCLUDE);
}
inspect() {
return {
name : this.name,
type : SendPropDefinition.formatType(this.type),
flags: SendPropDefinition.formatFlags(this.flags)
}
}
static formatFlags(flags) {
let names = [];
for (const name in SendPropDefinition.flags) {
if (SendPropDefinition.flags.hasOwnProperty(name)) {
if (flags & SendPropDefinition.flags[name]) {
names.push(name);
}
}
}
return names;
}
static formatType(type) {
for (let name in SendPropDefinition.types) {
if (SendPropDefinition.types.hasOwnProperty(name)) {
if (SendPropDefinition.types[name] === type) {
return name;
}
}
}
return 'unknown';
}
}
SendPropDefinition.types = {
DPT_Int : 0,
DPT_Float : 1,
DPT_Vector : 2,
DPT_VectorXY : 3,// Only encodes the XY of a vector, ignores Z
DPT_String : 4,
DPT_Array : 5,
DPT_DataTable : 6,
DPT_NUMSendPropTypes: 7
};
SendPropDefinition.flags = {
SPROP_UNSIGNED : (1 << 0),// Unsigned integer data.
SPROP_COORD : (1 << 1),// If this is set, the float/vector is treated like a world coordinate.
// Note that the bit count is ignored in this case.
SPROP_NOSCALE : (1 << 2),// For floating point, don't scale into range, just take value as is.
SPROP_ROUNDDOWN : (1 << 3),// For floating point, limit high value to range minus one bit unit
SPROP_ROUNDUP : (1 << 4),// For floating point, limit low value to range minus one bit unit
SPROP_NORMAL : (1 << 5),// If this is set, the vector is treated like a normal (only valid for vectors)
SPROP_EXCLUDE : (1 << 6),// This is an exclude prop (not excludED, but it points at another prop to be excluded).
SPROP_XYZE : (1 << 7),// Use XYZ/Exponent encoding for vectors.
SPROP_INSIDEARRAY : (1 << 8),// This tells us that the property is inside an array, so it shouldn't be put into the
// flattened property list. Its array will point at it when it needs to.
SPROP_PROXY_ALWAYS_YES : (1 << 9),// Set for datatable props using one of the default datatable proxies like
// SendProxy_DataTableToDataTable that always send the data to all clients.
SPROP_CHANGES_OFTEN : (1 << 10),// this is an often changed field, moved to head of sendtable so it gets a small index
SPROP_IS_A_VECTOR_ELEM : (1 << 11),// Set automatically if SPROP_VECTORELEM is used.
SPROP_COLLAPSIBLE : (1 << 12),// Set automatically if it's a datatable with an offset of 0 that doesn't change the pointer
// (ie: for all automatically-chained base classes).
// In this case, it can get rid of this SendPropDataTable altogether and spare the
// trouble of walking the hierarchy more than necessary.
SPROP_COORD_MP : (1 << 13),// Like SPROP_COORD, but special handling for multiplayer games
SPROP_COORD_MP_LOWPRECISION: (1 << 14),// Like SPROP_COORD, but special handling for multiplayer games where the fractional component only gets a 3 bits instead of 5
SPROP_COORD_MP_INTEGRAL : (1 << 15),// SPROP_COORD_MP, but coordinates are rounded to integral boundaries
SPROP_VARINT : (1 << 5) //reuse normal
};

70
src/Data/SendTable.js Normal file
View file

@ -0,0 +1,70 @@
import {SendPropDefinition} from './SendPropDefinition';
export class SendTable {
constructor(name) {
this.name = name;
this.props = [];
this._flattenedProps = [];
}
addProp(prop) {
this.props.push(prop);
}
flatten() {
let excludes = [];
let props = [];
this.getAllProps(excludes, props);
// sort often changed props before the others
let start = 0;
for (let i = 0; i < props.length; i++) {
if (props[i].hasFlag(SendPropDefinition.flags.SPROP_CHANGES_OFTEN)) {
if (i != start) {
const temp = props[i];
props[i] = props[start];
props[start] = temp;
}
start++;
}
}
this._flattenedProps = props;
}
getAllProps(excludes, props) {
let localProps = [];
this.getAllPropsIteratorProps(excludes, localProps, props);
for (let i = 0; i < localProps.length; i++) {
props.push(localProps[i]);
}
}
getAllPropsIteratorProps(excludes, props, childProps) {
for (let i = 0; i < this.props.length; i++) {
const prop = this.props[i];
if (prop.type === SendPropDefinition.types.DPT_DataTable) {
if (prop.hasFlag(SendPropDefinition.flags.SPROP_EXCLUDE)) {
excludes.push(prop.table);
} else if (excludes.indexOf(this) === -1) {
if (prop.hasFlag(SendPropDefinition.flags.SPROP_COLLAPSIBLE)) {
prop.table.getAllPropsIteratorProps(excludes, props, childProps);
} else {
prop.table.getAllProps(excludes, childProps);
}
}
} else if (!prop.hasFlag(SendPropDefinition.flags.SPROP_EXCLUDE)) {
props.push(prop);
}
}
}
get flattenedProps(){
if (this._flattenedProps.length === 0) {
this.flatten();
}
return this._flattenedProps;
}
}

7
src/Data/Vector.js Normal file
View file

@ -0,0 +1,7 @@
export class Vector {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}