@@ -1234,13 +1234,16 @@ var StateSync = (function (exports) {
1234
1234
} ;
1235
1235
return ADirty ;
1236
1236
} ) ( ) ;
1237
- /** @class */ ( function ( _super ) {
1237
+ var Int = /** @class */ ( function ( _super ) {
1238
1238
__extends ( Int , _super ) ;
1239
- function Int ( ) {
1240
- var _this =
1241
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1239
+ function Int ( value ) {
1240
+ if ( value === void 0 ) {
1241
+ value = 0 ;
1242
+ }
1243
+ var _this = _super . call ( this ) || this ;
1242
1244
_this . dirty = false ;
1243
1245
_this . _value = 0 ;
1246
+ _this . _value = value ;
1244
1247
return _this ;
1245
1248
}
1246
1249
Object . defineProperty ( Int . prototype , "value" , {
@@ -1276,11 +1279,14 @@ var StateSync = (function (exports) {
1276
1279
} ) ( ADirty ) ;
1277
1280
/** @class */ ( function ( _super ) {
1278
1281
__extends ( Float , _super ) ;
1279
- function Float ( ) {
1280
- var _this =
1281
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1282
+ function Float ( value ) {
1283
+ if ( value === void 0 ) {
1284
+ value = 0 ;
1285
+ }
1286
+ var _this = _super . call ( this ) || this ;
1282
1287
_this . dirty = false ;
1283
1288
_this . _value = 0 ;
1289
+ _this . _value = value ;
1284
1290
return _this ;
1285
1291
}
1286
1292
Object . defineProperty ( Float . prototype , "value" , {
@@ -1316,11 +1322,14 @@ var StateSync = (function (exports) {
1316
1322
} ) ( ADirty ) ;
1317
1323
/** @class */ ( function ( _super ) {
1318
1324
__extends ( Long , _super ) ;
1319
- function Long ( ) {
1320
- var _this =
1321
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1325
+ function Long ( value ) {
1326
+ if ( value === void 0 ) {
1327
+ value = 0 ;
1328
+ }
1329
+ var _this = _super . call ( this ) || this ;
1322
1330
_this . dirty = false ;
1323
1331
_this . _value = 0 ;
1332
+ _this . _value = value ;
1324
1333
return _this ;
1325
1334
}
1326
1335
Object . defineProperty ( Long . prototype , "value" , {
@@ -1356,11 +1365,14 @@ var StateSync = (function (exports) {
1356
1365
} ) ( ADirty ) ;
1357
1366
/** @class */ ( function ( _super ) {
1358
1367
__extends ( Uint , _super ) ;
1359
- function Uint ( ) {
1360
- var _this =
1361
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1368
+ function Uint ( value ) {
1369
+ if ( value === void 0 ) {
1370
+ value = 0 ;
1371
+ }
1372
+ var _this = _super . call ( this ) || this ;
1362
1373
_this . dirty = false ;
1363
1374
_this . _value = 0 ;
1375
+ _this . _value = value ;
1364
1376
return _this ;
1365
1377
}
1366
1378
Object . defineProperty ( Uint . prototype , "value" , {
@@ -1396,11 +1408,14 @@ var StateSync = (function (exports) {
1396
1408
} ) ( ADirty ) ;
1397
1409
/** @class */ ( function ( _super ) {
1398
1410
__extends ( Double , _super ) ;
1399
- function Double ( ) {
1400
- var _this =
1401
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1411
+ function Double ( value ) {
1412
+ if ( value === void 0 ) {
1413
+ value = 0 ;
1414
+ }
1415
+ var _this = _super . call ( this ) || this ;
1402
1416
_this . dirty = false ;
1403
1417
_this . _value = 0 ;
1418
+ _this . _value = value ;
1404
1419
return _this ;
1405
1420
}
1406
1421
Object . defineProperty ( Double . prototype , "value" , {
@@ -1436,11 +1451,14 @@ var StateSync = (function (exports) {
1436
1451
} ) ( ADirty ) ;
1437
1452
/** @class */ ( function ( _super ) {
1438
1453
__extends ( Ulong , _super ) ;
1439
- function Ulong ( ) {
1440
- var _this =
1441
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1454
+ function Ulong ( value ) {
1455
+ if ( value === void 0 ) {
1456
+ value = 0 ;
1457
+ }
1458
+ var _this = _super . call ( this ) || this ;
1442
1459
_this . dirty = false ;
1443
1460
_this . _value = 0 ;
1461
+ _this . _value = value ;
1444
1462
return _this ;
1445
1463
}
1446
1464
Object . defineProperty ( Ulong . prototype , "value" , {
@@ -1476,11 +1494,14 @@ var StateSync = (function (exports) {
1476
1494
} ) ( ADirty ) ;
1477
1495
/** @class */ ( function ( _super ) {
1478
1496
__extends ( Short , _super ) ;
1479
- function Short ( ) {
1480
- var _this =
1481
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1497
+ function Short ( value ) {
1498
+ if ( value === void 0 ) {
1499
+ value = 0 ;
1500
+ }
1501
+ var _this = _super . call ( this ) || this ;
1482
1502
_this . dirty = false ;
1483
1503
_this . _value = 0 ;
1504
+ _this . _value = value ;
1484
1505
return _this ;
1485
1506
}
1486
1507
Object . defineProperty ( Short . prototype , "value" , {
@@ -1516,11 +1537,14 @@ var StateSync = (function (exports) {
1516
1537
} ) ( ADirty ) ;
1517
1538
/** @class */ ( function ( _super ) {
1518
1539
__extends ( Ushort , _super ) ;
1519
- function Ushort ( ) {
1520
- var _this =
1521
- ( _super !== null && _super . apply ( this , arguments ) ) || this ;
1540
+ function Ushort ( value ) {
1541
+ if ( value === void 0 ) {
1542
+ value = 0 ;
1543
+ }
1544
+ var _this = _super . call ( this ) || this ;
1522
1545
_this . dirty = false ;
1523
1546
_this . _value = 0 ;
1547
+ _this . _value = value ;
1524
1548
return _this ;
1525
1549
}
1526
1550
Object . defineProperty ( Ushort . prototype , "value" , {
@@ -1816,11 +1840,29 @@ var StateSync = (function (exports) {
1816
1840
View = __decorate ( [ NetComp ( "view" ) ] , View ) ;
1817
1841
return View ;
1818
1842
} ) ( ) ;
1843
+ var ServerTime = /** @class */ ( function ( ) {
1844
+ function ServerTime ( ) {
1845
+ this . timestamp = 0 ;
1846
+ this . deltaTime = new Int ( ) ;
1847
+ }
1848
+ __decorate (
1849
+ [ NetVar ( DataType . int ) ] ,
1850
+ ServerTime . prototype ,
1851
+ "timestamp" ,
1852
+ void 0
1853
+ ) ;
1854
+ __decorate ( [ NetVar ( Int ) ] , ServerTime . prototype , "deltaTime" , void 0 ) ;
1855
+ ServerTime = __decorate ( [ NetComp ( "time" ) ] , ServerTime ) ;
1856
+ return ServerTime ;
1857
+ } ) ( ) ;
1819
1858
1820
1859
var Time = /** @class */ ( function ( ) {
1821
- function Time ( ) { }
1822
- Time . deltaTime = 0 ;
1823
- Time . fixedDeltaTime = 1 / 30 ;
1860
+ function Time ( ) {
1861
+ this . deltaTime = 0 ;
1862
+ this . fixedDeltaTime = ( 1 / 10 ) * 1000 ;
1863
+ this . fixedTimestamp = 0 ;
1864
+ this . timestamp = 0 ;
1865
+ }
1824
1866
return Time ;
1825
1867
} ) ( ) ;
1826
1868
var Base = /** @class */ ( function ( ) {
@@ -1829,6 +1871,8 @@ var StateSync = (function (exports) {
1829
1871
this . bg = "#947A6D" ;
1830
1872
this . yelloBall = 0xf7d94c ;
1831
1873
this . whiteBall = 0xf8c3cd ;
1874
+ this . time = new Time ( ) ;
1875
+ this . doInterpolating = false ;
1832
1876
this . _preTimestamp = 0 ;
1833
1877
this . _fixedTimeAccumulator = 0 ;
1834
1878
this . domain = Domain . Create ( name , StringDataBuffer , rpcType ) ;
@@ -1839,58 +1883,62 @@ var StateSync = (function (exports) {
1839
1883
this . ctx . fillRect ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
1840
1884
this . myLoop = this . loop . bind ( this ) ;
1841
1885
this . initScene ( ) ;
1842
- this . render ( 0 ) ;
1886
+ this . render ( ) ;
1843
1887
}
1844
1888
Base . prototype . update = function ( ) { } ;
1845
1889
Base . prototype . fixedUpdate = function ( ) { } ;
1846
1890
Base . prototype . lateUpdate = function ( ) { } ;
1847
- Base . prototype . loop = function ( time ) {
1848
- this . update ( ) ;
1891
+ Base . prototype . loop = function ( timestamp ) {
1892
+ var Time = this . time ;
1849
1893
if ( this . _preTimestamp === 0 ) {
1850
- Time . deltaTime = 1 / 60 ;
1894
+ Time . deltaTime = ( 1 / 60 ) * 1000 ;
1851
1895
} else {
1852
- Time . deltaTime = time - this . _preTimestamp ;
1896
+ Time . deltaTime = timestamp - this . _preTimestamp ;
1853
1897
}
1854
- this . _preTimestamp = time ;
1855
- this . _fixedTimeAccumulator += time ;
1898
+ Time . timestamp += Time . deltaTime ;
1899
+ this . update ( ) ;
1900
+ this . _preTimestamp = timestamp ;
1901
+ this . _fixedTimeAccumulator += Time . deltaTime ;
1856
1902
var count = 0 ;
1857
1903
while (
1858
1904
this . _fixedTimeAccumulator >= Time . fixedDeltaTime &&
1859
1905
count < 3
1860
1906
) {
1861
1907
count ++ ;
1862
1908
this . _fixedTimeAccumulator -= Time . fixedDeltaTime ;
1909
+ Time . fixedTimestamp += Time . fixedDeltaTime ;
1863
1910
this . fixedUpdate ( ) ;
1864
1911
}
1865
- this . render ( time ) ;
1912
+ this . render ( ) ;
1866
1913
this . lateUpdate ( ) ;
1867
1914
} ;
1868
- Base . prototype . render = function ( time ) {
1915
+ Base . prototype . render = function ( ) {
1869
1916
requestAnimationFrame ( this . myLoop ) ;
1870
- this . canvas . width = this . canvas . width ;
1917
+ this . time ;
1871
1918
this . domain ;
1919
+ this . canvas . width = this . canvas . width ;
1872
1920
var ctx = this . ctx ;
1873
1921
ctx . fillStyle = this . bg ;
1874
1922
ctx . fillRect ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
1875
1923
var c1 = this . c1 ;
1876
1924
if ( c1 ) {
1877
1925
var p1 = c1 . $comps . trans ;
1878
1926
var v1 = c1 . $comps . view ;
1879
- ctx . fillStyle = "#" + v1 . color . toString ( 16 ) ;
1880
- ctx . beginPath ( ) ;
1881
- ctx . arc ( p1 . pos . x , p1 . pos . y , 26 , 0 , 2 * Math . PI ) ;
1882
- ctx . fill ( ) ;
1927
+ this . _drawBall ( ctx , p1 . pos , "#" + v1 . color . toString ( 16 ) ) ;
1883
1928
}
1884
1929
var c2 = this . c2 ;
1885
1930
if ( c2 ) {
1886
1931
var p2 = c2 . $comps . trans ;
1887
1932
var v2 = c2 . $comps . view ;
1888
- ctx . fillStyle = "#" + v2 . color . toString ( 16 ) ;
1889
- ctx . beginPath ( ) ;
1890
- ctx . arc ( p2 . pos . x , p2 . pos . y , 26 , 0 , 2 * Math . PI ) ;
1891
- ctx . fill ( ) ;
1933
+ this . _drawBall ( ctx , p2 . pos , "#" + v2 . color . toString ( 16 ) ) ;
1892
1934
}
1893
1935
} ;
1936
+ Base . prototype . _drawBall = function ( ctx , pos , color ) {
1937
+ ctx . fillStyle = color ;
1938
+ ctx . beginPath ( ) ;
1939
+ ctx . arc ( pos . x , pos . y , 26 , 0 , 2 * Math . PI ) ;
1940
+ ctx . fill ( ) ;
1941
+ } ;
1894
1942
Base . prototype . initScene = function ( ) {
1895
1943
var ent1 = new Entity ( ) ;
1896
1944
var trans1 = ent1 . add ( Transform ) ;
@@ -1902,8 +1950,12 @@ var StateSync = (function (exports) {
1902
1950
ent2 . add ( View ) ;
1903
1951
trans2 . pos . y = 35 ;
1904
1952
trans2 . pos . x = 30 ;
1953
+ var server = new Entity ( ) ;
1954
+ server . add ( ServerTime ) ;
1905
1955
this . c1 = ent1 ;
1906
1956
this . c2 = ent2 ;
1957
+ this . server = server ;
1958
+ this . domain . reg ( server ) ;
1907
1959
this . domain . reg ( ent1 ) ;
1908
1960
this . domain . reg ( ent2 ) ;
1909
1961
} ;
@@ -1925,6 +1977,9 @@ var StateSync = (function (exports) {
1925
1977
this . c2 . $comps . trans ;
1926
1978
} ;
1927
1979
Server . prototype . fixedUpdate = function ( ) {
1980
+ var Time = this . time ;
1981
+ var serverTime = this . server . get ( ServerTime ) ;
1982
+ serverTime . timestamp = Time . fixedTimestamp ;
1928
1983
var c1 = Net . client1 ;
1929
1984
var c2 = Net . client2 ;
1930
1985
var data = this . domain . asData ( ) ;
@@ -1981,12 +2036,14 @@ var StateSync = (function (exports) {
1981
2036
Client . prototype . fixedUpdate = function ( ) {
1982
2037
var input = this . _input ;
1983
2038
var trans = this . mine . get ( Transform ) ;
1984
- trans . serverMove (
1985
- ( input . isLeft ? - 1 : 0 ) + ( input . isRight ? 1 : 0 ) ,
1986
- 0
1987
- ) ;
2039
+ var dirX = ( input . isLeft ? - 1 : 0 ) + ( input . isRight ? 1 : 0 ) ;
2040
+ trans . serverMove ( dirX * this . time . fixedDeltaTime * 0.1 , 0 ) ;
1988
2041
var data = this . domain . asData ( ) ;
1989
2042
Net . send ( data ) . recv ( Net . server . domain . setData , Net . server . domain ) ;
2043
+ var serverTime = this . server . get ( ServerTime ) ;
2044
+ this . time . timestamp =
2045
+ this . time . timestamp * 0.5 + serverTime . timestamp * 0.5 ;
2046
+ console . log ( serverTime . timestamp , this . time . timestamp ) ;
1990
2047
} ;
1991
2048
return Client ;
1992
2049
} ) ( Base ) ;
@@ -1995,6 +2052,7 @@ var StateSync = (function (exports) {
1995
2052
exports . Client = Client ;
1996
2053
exports . Net = Net ;
1997
2054
exports . Server = Server ;
2055
+ exports . ServerTime = ServerTime ;
1998
2056
exports . Time = Time ;
1999
2057
exports . Transform = Transform ;
2000
2058
exports . Vector = Vector ;
0 commit comments