@@ -1743,6 +1743,30 @@ var StateSync = (function (exports) {
1743
1743
return StringDataBuffer ;
1744
1744
} ) ( ) ;
1745
1745
1746
+ var Net = /** @class */ ( function ( ) {
1747
+ function Net ( ) { }
1748
+ Net . send = function ( obj ) {
1749
+ var _this = this ;
1750
+ var promise = new Promise ( function ( resolve ) {
1751
+ setTimeout (
1752
+ resolve ,
1753
+ _this . delay + Math . random ( ) * _this . jitter ,
1754
+ obj
1755
+ ) ;
1756
+ } ) ;
1757
+ return {
1758
+ recv : function ( func , context ) {
1759
+ promise . then ( function ( res ) {
1760
+ func . call ( context , res ) ;
1761
+ } ) ;
1762
+ } ,
1763
+ } ;
1764
+ } ;
1765
+ Net . delay = 0 ;
1766
+ Net . jitter = 0 ;
1767
+ return Net ;
1768
+ } ) ( ) ;
1769
+
1746
1770
var Vector = /** @class */ ( function ( ) {
1747
1771
function Vector ( ) {
1748
1772
this . x = 0 ;
@@ -1757,7 +1781,7 @@ var StateSync = (function (exports) {
1757
1781
function Transform ( ) {
1758
1782
this . pos = new Vector ( ) ;
1759
1783
}
1760
- Transform . prototype . move = function ( x , y ) {
1784
+ Transform . prototype . serverMove = function ( x , y ) {
1761
1785
this . pos . x += x ;
1762
1786
this . pos . y += y ;
1763
1787
} ;
@@ -1769,7 +1793,7 @@ var StateSync = (function (exports) {
1769
1793
__param ( 1 , RpcVar ( DataType . int ) ) ,
1770
1794
] ,
1771
1795
Transform . prototype ,
1772
- "move " ,
1796
+ "serverMove " ,
1773
1797
null
1774
1798
) ;
1775
1799
Transform = __decorate ( [ NetComp ( "trans" ) ] , Transform ) ;
@@ -1793,36 +1817,20 @@ var StateSync = (function (exports) {
1793
1817
return View ;
1794
1818
} ) ( ) ;
1795
1819
1796
- var Net = /** @class */ ( function ( ) {
1797
- function Net ( ) { }
1798
- Net . send = function ( obj ) {
1799
- var _this = this ;
1800
- var promise = new Promise ( function ( resolve ) {
1801
- setTimeout (
1802
- resolve ,
1803
- _this . delay + Math . random ( ) * _this . jitter ,
1804
- obj
1805
- ) ;
1806
- } ) ;
1807
- return {
1808
- recv : function ( func , context ) {
1809
- promise . then ( function ( res ) {
1810
- func . call ( context , res ) ;
1811
- } ) ;
1812
- } ,
1813
- } ;
1814
- } ;
1815
- Net . delay = 0 ;
1816
- Net . jitter = 0 ;
1817
- return Net ;
1820
+ var Time = /** @class */ ( function ( ) {
1821
+ function Time ( ) { }
1822
+ Time . deltaTime = 0 ;
1823
+ Time . fixedDeltaTime = 1 / 30 ;
1824
+ return Time ;
1818
1825
} ) ( ) ;
1819
-
1820
1826
var Base = /** @class */ ( function ( ) {
1821
1827
function Base ( name , canvas , rpcType ) {
1822
1828
this . canvas = canvas ;
1823
1829
this . bg = "#947A6D" ;
1824
1830
this . yelloBall = 0xf7d94c ;
1825
1831
this . whiteBall = 0xf8c3cd ;
1832
+ this . _preTimestamp = 0 ;
1833
+ this . _fixedTimeAccumulator = 0 ;
1826
1834
this . domain = Domain . Create ( name , StringDataBuffer , rpcType ) ;
1827
1835
this . ctx = canvas . getContext ( "2d" ) ;
1828
1836
this . canvas . width = 950 ;
@@ -1833,8 +1841,29 @@ var StateSync = (function (exports) {
1833
1841
this . initScene ( ) ;
1834
1842
this . render ( 0 ) ;
1835
1843
}
1844
+ Base . prototype . update = function ( ) { } ;
1845
+ Base . prototype . fixedUpdate = function ( ) { } ;
1846
+ Base . prototype . lateUpdate = function ( ) { } ;
1836
1847
Base . prototype . loop = function ( time ) {
1848
+ this . update ( ) ;
1849
+ if ( this . _preTimestamp === 0 ) {
1850
+ Time . deltaTime = 1 / 60 ;
1851
+ } else {
1852
+ Time . deltaTime = time - this . _preTimestamp ;
1853
+ }
1854
+ this . _preTimestamp = time ;
1855
+ this . _fixedTimeAccumulator += time ;
1856
+ var count = 0 ;
1857
+ while (
1858
+ this . _fixedTimeAccumulator >= Time . fixedDeltaTime &&
1859
+ count < 3
1860
+ ) {
1861
+ count ++ ;
1862
+ this . _fixedTimeAccumulator -= Time . fixedDeltaTime ;
1863
+ this . fixedUpdate ( ) ;
1864
+ }
1837
1865
this . render ( time ) ;
1866
+ this . lateUpdate ( ) ;
1838
1867
} ;
1839
1868
Base . prototype . render = function ( time ) {
1840
1869
requestAnimationFrame ( this . myLoop ) ;
@@ -1878,6 +1907,8 @@ var StateSync = (function (exports) {
1878
1907
this . domain . reg ( ent1 ) ;
1879
1908
this . domain . reg ( ent2 ) ;
1880
1909
} ;
1910
+ Base . prototype . onKeyDown = function ( ev ) { } ;
1911
+ Base . prototype . onKeyUp = function ( ev ) { } ;
1881
1912
return Base ;
1882
1913
} ) ( ) ;
1883
1914
var Server = /** @class */ ( function ( _super ) {
@@ -1890,21 +1921,31 @@ var StateSync = (function (exports) {
1890
1921
}
1891
1922
Server . prototype . loop = function ( dt ) {
1892
1923
_super . prototype . loop . call ( this , dt ) ;
1893
- var t1 = this . c1 . $comps . trans ;
1924
+ this . c1 . $comps . trans ;
1894
1925
this . c2 . $comps . trans ;
1895
- t1 . move ( 1 , 0 ) ;
1926
+ } ;
1927
+ Server . prototype . fixedUpdate = function ( ) {
1928
+ var c1 = Net . client1 ;
1929
+ var c2 = Net . client2 ;
1930
+ var data = this . domain . asData ( ) ;
1931
+ Net . send ( data ) . recv ( c1 . domain . setData , c1 . domain ) ;
1932
+ Net . send ( data ) . recv ( c2 . domain . setData , c2 . domain ) ;
1896
1933
} ;
1897
1934
return Server ;
1898
1935
} ) ( Base ) ;
1899
1936
var Client = /** @class */ ( function ( _super ) {
1900
1937
__extends ( Client , _super ) ;
1901
- function Client ( index , canvas ) {
1938
+ function Client ( index , canvas , controlMap ) {
1902
1939
var _this =
1903
1940
_super . call ( this , "client" + index , canvas , RpcType . CLIENT ) ||
1904
1941
this ;
1905
1942
_this . index = index ;
1906
1943
_this . canvas = canvas ;
1944
+ _this . controlMap = controlMap ;
1945
+ _this . _input = { isLeft : false , isRight : false } ;
1907
1946
_this . mine . $comps . view . changeColor ( _this . color ) ;
1947
+ window . addEventListener ( "keydown" , _this . onKeyDown . bind ( _this ) ) ;
1948
+ window . addEventListener ( "keyup" , _this . onKeyUp . bind ( _this ) ) ;
1908
1949
return _this ;
1909
1950
}
1910
1951
Object . defineProperty ( Client . prototype , "mine" , {
@@ -1921,13 +1962,40 @@ var StateSync = (function (exports) {
1921
1962
enumerable : false ,
1922
1963
configurable : true ,
1923
1964
} ) ;
1965
+ Client . prototype . onKeyDown = function ( ev ) {
1966
+ var map = this . controlMap ;
1967
+ if ( ev . key === map . left ) {
1968
+ this . _input . isLeft = true ;
1969
+ } else if ( ev . key === map . right ) {
1970
+ this . _input . isRight = true ;
1971
+ }
1972
+ } ;
1973
+ Client . prototype . onKeyUp = function ( ev ) {
1974
+ var map = this . controlMap ;
1975
+ if ( ev . key === map . left ) {
1976
+ this . _input . isLeft = false ;
1977
+ } else if ( ev . key === map . right ) {
1978
+ this . _input . isRight = false ;
1979
+ }
1980
+ } ;
1981
+ Client . prototype . fixedUpdate = function ( ) {
1982
+ var input = this . _input ;
1983
+ var trans = this . mine . get ( Transform ) ;
1984
+ trans . serverMove (
1985
+ ( input . isLeft ? - 1 : 0 ) + ( input . isRight ? 1 : 0 ) ,
1986
+ 0
1987
+ ) ;
1988
+ var data = this . domain . asData ( ) ;
1989
+ Net . send ( data ) . recv ( Net . server . domain . setData , Net . server . domain ) ;
1990
+ } ;
1924
1991
return Client ;
1925
1992
} ) ( Base ) ;
1926
1993
1927
1994
exports . Base = Base ;
1928
1995
exports . Client = Client ;
1929
1996
exports . Net = Net ;
1930
1997
exports . Server = Server ;
1998
+ exports . Time = Time ;
1931
1999
exports . Transform = Transform ;
1932
2000
exports . Vector = Vector ;
1933
2001
exports . View = View ;
0 commit comments