From fa7fe8f77585765496f6ff55aa90e57195a0de20 Mon Sep 17 00:00:00 2001 From: rasmusmerzin Date: Wed, 14 Aug 2019 14:46:58 +0300 Subject: [PATCH] 1.0 --- images/{vines.png => background.png} | Bin images/booster.png | Bin 0 -> 474 bytes images/bull.png | Bin 1220 -> 0 bytes images/bull.xcf | Bin 5805 -> 0 bytes images/char-green.png | Bin 0 -> 867 bytes images/char-pink.png | Bin 0 -> 872 bytes images/{char.png => char-white.png} | Bin images/char-yellow.png | Bin 0 -> 869 bytes images/gravel.png | Bin 596 -> 0 bytes images/platform.png | Bin 0 -> 456 bytes images/rock.png | Bin 518 -> 0 bytes images/slate.png | Bin 523 -> 0 bytes index.html | 9 +- js-platformer.sublime-workspace | 263 ++++++++++++++++++++ script.js | 355 ++++++++++++++++++++------- 15 files changed, 535 insertions(+), 92 deletions(-) rename images/{vines.png => background.png} (100%) create mode 100644 images/booster.png delete mode 100644 images/bull.png delete mode 100644 images/bull.xcf create mode 100644 images/char-green.png create mode 100644 images/char-pink.png rename images/{char.png => char-white.png} (100%) create mode 100644 images/char-yellow.png delete mode 100644 images/gravel.png create mode 100644 images/platform.png delete mode 100644 images/rock.png delete mode 100644 images/slate.png create mode 100644 js-platformer.sublime-workspace diff --git a/images/vines.png b/images/background.png similarity index 100% rename from images/vines.png rename to images/background.png diff --git a/images/booster.png b/images/booster.png new file mode 100644 index 0000000000000000000000000000000000000000..06a53120f822b0574f8d65da3bb045c44b48ea42 GIT binary patch literal 474 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzwj^(N7a$D;Kb?2i11Zh|kH}&M z25w;xW@MN(M*=9wUgGKN%Kn&xkIh17ZDQ~hppa~4NJNQqer|4RUI~!Pz~Eeznpl#W zqEMb$lA+-4=^K!um&(q-z-Z^`;usQfI5|P$Wy8dn-t2d^XPlq4Ykc%qIxe$JQJe`V zFo!R7(_+uREp0B(KJYY}ez#lKW@Ne0?yKYE`%I~I?WfxL`UN72-#@O>m@NLvSe)tA zcZZqr3tlm|rE#p;`MpqdiYCX`PVHHL1h*6&U~~7_b9}lQTRext)8a^(m47wtn4PEm zSX=m{Pd(sg&p}BhkJR0aSC)EOJenie?%yxO(ODVrjP=Q`Bjv$UX=@IGOFFUlvBxxPwe^Zg}>hOo)VB?UCh7`WHe2qX`A9DP!M~% L`njxgN@xNAyb8O& literal 0 HcmV?d00001 diff --git a/images/bull.png b/images/bull.png deleted file mode 100644 index 83c48fc0696eaf2cdaa5e4e89f496a7361f42c66..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1220 zcma)6drX^E6u<4h4Zb14QL{P1qiWXzZh$T*Lds(;k3uL=9xY4m>e=9otlE@WU#N|gK6QB zDQ>A206@+(YCue}bXy*lzv=PD@tI;*{d9hsXVcOnXLCS5_kKRaI^}qF&0xR+_3nJ7 zuOG5vtq)6mm84sK3BZ;Qdvq$Y%El(m3Ny7k8VleVXf>82C_m`dywD4Uha@yKak@=TS(K`})-q1_MJ)2N-VDAInu;Q3uGZzoInld$EA8v@>iEJ?fXA%+ zU|RP;P7}Xl$!FZAgRl|*d>R5DlK^f<4L3YAOMKU-EZ)WNp(L!r8#B24tO=rrfk}-R zLMtmaB<$?LtFr5t=Xfw60Gx-+ZeIekloah;X;1mHbD#!%3Ww>^Y1CnyhpPSja($|M zJKG$FWNzR;q}5z-saLv&-+8I)Fj9D4hp4}jRN-T>-1@z`2j1^=gn`>0IrCCY!K?H> z@eosR0S-Z8S;m%u)88^U8bVe1^<+uAR_unP=X)_%dm4}9MLBlcN#wYEn_=a13Ad=NmojYE{khiysx$8 z5Q*=M6YWjI^L0 zc2-5r>22m5MJ+<68Ao8s9C`c=N!BzvXghL_itZA{&7M+Lm*l0FZ zVfd+Iv89sieKgoS<3*c6vVLktLBI$0a6_3w#s}kya6p~N8OF{u30vBRx)QNq!$@PEa^zE$AZ Uyggy;Ofecl3#3zpmy#;~21#%a)Bpeg diff --git a/images/bull.xcf b/images/bull.xcf deleted file mode 100644 index 11976eea3d07d1627c6f9383c9d9f525eb0273f0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5805 zcmeI0OK%)S5XXBSJ3BjGzhY{ha`4w1{#{n)#QILp`h=h>%0FJZXtmH?qjYtm2 zmN;=haN)v@1D{|nd^Et1=J&6eS+8;81D9~vk;+}uUEN((JzM|3yVu`-(SEtH+1~7L zbsf(U@<}G5 zRyTVayIZ?^?Nw_!d%dUY?Ry{GwdV?Djj2!n`_z~FFV;8uJH7UU`{iD&_p0i=Kh#jK%zdA0S`rOCtm-@4u7|(|?cb0oL$z9|U8c#4e$5~lKawH&XhGKS) zSv1H4i#jCE#L^M?adH+hD`J#PrPi(81H20SBqDtevk&p|0V#c-cV+SpttD`+P-grp z@vIQvGPznJPZH&4kqs;_x2tq4nZ-O7nMJ=$8x!%08^e{lVo_IE1iqsA3b#mhoi=x? z=Ek|Jhc!|*`U*y0!RRX()dFfk?+vwJyr@Bws*UH+V1;Y|2^odg`5B6G)mFv2D&AK5 zu3K$ns+0vpFkz+cdReM2yr`rK32fVHLA5PIf|R-!xyAqq8U&CS3RFeC?#C$Pm-`~- zM}`_H+ZW~Cb`ESpjX~=BYifUF0VMM1-2HGdClpV|+-LZw-(r?C(+ zO#vMf=ys?R#scg`$UZo8on&c};gxp5UT3aT)UgQYWmSQ_jNWAfowUU#pf3sB2)IJ^>1N$m?IZ2m3A9+(&rPz$07~oQGs5bg=-t zCV)>YcgkOeW6B+no(m-9XkgY_;xzY8JV>!lA?BaZKV=p7m|ce{)MOFUB;SxFZ30F4 zEvQq~TFwT{DJANPIqBzc6>};WRGG+t%<99{haz2z400K>k~mlBnn;HK3;5oo&(al> z(UTmS)nKH_NZki{j7CsFWn!nB7IwvWtYjvbk|YpA(bWOh{8x8%5%e9E;R37Fv zf1J|+v9c}^E~!SJe4H!Swx@#1gIodp4gawjkvpcQU)s#IBYmL!YJo<|DrH;O0 zE7zl1JgUWSuNDgf#VF>f_}41fn*o_cCj@zuz)BO{V6W)}jzT;G$Z6pB5|I#roCUho z&b@|E2s7ppTe3yuEF}=Em_-6@5Ja<~IEk31Z8isJC#;u39z-leAR0y}#32jrC|nB5 zv4UL0ybH60Jr$)8n-D#O1c+|bS+pQyc6VYNVVWL+nU*sOwS;|xmoNW!`4V*-~C@B*ZL0Op0D+b#tx_6AaBSLQywCgoqqsU<~_dv diff --git a/images/char-green.png b/images/char-green.png new file mode 100644 index 0000000000000000000000000000000000000000..b42c82eb4b78c72ba1b87d37bd1c640ca445bf22 GIT binary patch literal 867 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSY)RhkEUnfx)>bHL)Z$ zMWH;iBtya7(>EYRFO{8vf$6xXi(^Q|t+#i5gB}|QI3%_|GTHpxM$eC_c*>mgcPu$h zqN2w{qqF`--j8m(@oq`f`e?3c(^(Jfi8*xs$Hz}U?_Zz)FLsrTef1yqN+Y&s&nmz0 zh}kncFgY+OtYAOkw=#O|uXU;J+2QX#Za#jVp(B#r7Z_MtDo;lz8 zQ_*>2?>{^ZHq0)mA1=-+`~NFUrr*u!uFZ!7f(I0cE9{w*3e)7BpVp5=DvSk^ySyyb;2V5>_B|wtfdCK%9dUG7xX-l z;Y)7Aimm@{ZQT~Sr`OJ$nc={eDXXo&aY{VWOSNr?JaBSjJOkLP3*PBG*w?o7+L{%Q zi+BGFe4Ng3#qhwr`1?ig-)fi6|NG*jF~hAj%r?7TZ~yYK`u_g!@19(Il+Lil)FIF9 zncRG9CinjNj2Esktd;zG@6fT2#s{8wFmQt5=7vqN?^gW&F}G$u127yW_wBj#-14Ly zBhZVLYhM4_rgOSzZ`}W9DZido7F8DfjlVuO2dHqyoLB#acT_KtKlA16=WDk#pRC9X zT3`SB_pe{iZ4b@hX_)*O7;&4MwTW!*JV131VyM8%_x!)O9+8z!Q=XuRA0SG%3 z{%vP4ox^CbnQ>Ea(VFG?#teRG4fo2PCo!=d@UV5r;+6kNG8-+i2({MsgVU^qSzB?D+Yo#&tBsT2T zF8sdU{d$t}T*eE<`~j=mR{!N`n8mi@>yw_4!!y1cGemu1=q+1s_DPb#muJE2G;l)3 bMtnIg{_p?SO~x{^t3cA8u6{1-oD!MUnfx)>bHL)Z$ zMWH;iBtya7(>EYRFO{8vf$5B=i(^Q|t+#h=`)(TuI3!wnOf^ft9cAgPx8Y=9%=4UT zwtQjcMuCGumVbozhu;!&l|F3ld}`4&)&qO)ERwF-Q)~0DdXfG8eAB-9d;h2ZOyW6n z_WDKTi2srdOh9O8>hNvLqx^MM&o*t|sr~)O&EMx4IwILUdLP6+ySVw=d`5xYd<7;# z-<9mkpFG`W>JV6$cJ?_Y8DalI{<{_6X#{%6dq7F*ysdxv z{yf8r%M2@(RZ5>sTcsFv`IG|B0S3c~vw!^zTz5HMlr5pb$Z|!PpLN!RJFBZMZslTL z@Lp;`XsA@_y`Z;yqB`$X|9_*i;u_Eto(s=w?5A&j{@&Dm`afIA2F71XSxXJ9%9lO+ z7xdPE;g^}im8kl+F}GLkU%Stonc=_|k<-@SI3*tIrJ6QG9@x4uo`Knc!GU4HJDr5t zhl7g4u6*2i_0NjG=?qs44_sWGzw`UIsk_eCeF6I5)*9wLyIybq^3mPC{>!^37k`~+ z$jENE({aYuuAHUsUOk&bG_%S4V?1^FiLd_|A9%vSzzBw$8y1DWTk-oxZ0&popue*F z_FQ@!lL}_rTq~`OpO(7w{`>vKO110f#>CC7ufBKl8)rhp$(8@+A6TsXH)qNFth5B* zsQz;^oqsi}1cPceNK=CBg8h{{M4i|iJejAQdswY({{7|S1O}ID41tzb%6!sc+^)M2fR-gje$EU5+8CL0IwCf@@FQXun|TYGPvNN3n$(6DMY hBPcav0~ZwM+g$JYTljG{FkLe+c)I$ztaD0e0sxwwaQpxO literal 0 HcmV?d00001 diff --git a/images/char.png b/images/char-white.png similarity index 100% rename from images/char.png rename to images/char-white.png diff --git a/images/char-yellow.png b/images/char-yellow.png new file mode 100644 index 0000000000000000000000000000000000000000..e4b238b56a1191af3a36dbca083da2d9738847e3 GIT binary patch literal 869 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSY)RhkEYGO%h zib8p2Nrr;Er*A-tUMf2S1Jg-Q7srr_TW{~4?Y(Uv;E-53F{|i))s&D;o|@~_ZP$Jm zVr~>TC}jCZcz^j8-7d4X{I)5sy4($wJ6+9x{HUq_^W7!CKF_po{@(xTmBwt(p51;S zxaL0(10w@t!we~f>Zp(A;kLy$Z{C?&{pZc!bcQ2qn3uQ}=zo3@`TIOWM=ZOARA=?X z`PQF`ZqHo$pQpiw*~Rt4%{kj@YuC%KcXN9AZ~C&(|B?-iN$zDUJ?>@OYnea$w*Slb zbcQdP4OavwSrvI-6b1&WSr)g}<=x);GO>`?KvURW6z`jV`sU~Fvle^*pT~26p;mBfki=Z;;Ntp~ zWfBav(h6B?_m{29y?Q_F{%l5ugs85kvcFk+3a4+9I-v95)(!mzMg;~1hJbRh4QCTq zT57-gaVM+p%0FX`+Ixl1#1~+`6sjO-@c*rzhpuYO9L=90AZxVMXmBHwSU&_^Jic-IAH1? z_p)r=CJ@^}?)9!Zozt7{y#IdRa^k-0etPTA?YDiO^P9!sz>|>w{tqrrtTVgx{npy8 z!6zawFWL6F=Kt~Y@AoV9oMhNx)_?x{@%^G-cCC{?f6;>J_P@OS{Zb6I*BMrt9*z^` zdCedIgdB>0q8en*G0eEhaP#t{gY)*CW!N!|vFi5RZcYgX<*N)UEo;7{|2JZ2dmb6fxb literal 0 HcmV?d00001 diff --git a/images/gravel.png b/images/gravel.png deleted file mode 100644 index b82d10306be4f303fd86f16d1c6fe5306ecb96c4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 596 zcmV-a0;~OrP)WFU8GbZ8()Nlj2>E@cM*00Fp3L_t(o!_AgYt3go| z#eW`!D1Rauk<1v7d;=Ny0=}prBN@n8C=;bnN{SHjM!VkjZtvcA4DQ*pbMC(P+_m;T zd#ymdUVrf;;_)~D*XtF4$K#>)Pp1mYPC{&E-@aD0mx)B3hCi+Q0xEt3H!CcP0!Eg zGoSbgd$qu;o=cee3443MtDd`3?BXZv=>bzc|I|;|!+@!t3o=tbVGjeQdM>pz^%M5Y i0aHDfF!d9@Gw=<>&pI^gO5#yt=*`42~bEjGbEzKIX^cyHLnE7WngeFN=+N%ez%cr71u}ZsWnea`Z+^L zD62!{GzOKE{HwSbKGe!LBn2~U&|Gr1ZT0Ud^}p7Hb^hFUu|6oXx&BybtZz+e<-+O5 z<}k literal 0 HcmV?d00001 diff --git a/images/rock.png b/images/rock.png deleted file mode 100644 index 697fd3a2fdbd717884acaa10d5c1104be1d89df6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 518 zcmV+h0{Q)kP)R7l6Yl}WC`Fc3s5Cd{LdBIW)E%8p|oF_S<7 zEFQmQ{Z+hZyl7b+JFe=XI@@eElCmt*-+I5_X4|&4J!fXKnc3ItB`Jhp_Iy6uf4Z)d z6y&_}7;m2E_VQpbF#q%Ul+-lMKR-O!a|_S?emBPc40vZcomw0pkB8x6kqIJxy}9ay0pol=x0oId2V($NPL4(+Lp>Z0C0#C;pD+Lm*uhJkEEWsH z4kEr*Rb_bN{=nj2uh)`Jr_(POWXt7J(ssKw*LXaZgr6uMk4NhRkk)l=3}6gb7_3$+ z`;UJPz)mJW2O_$W=#!!Eg*>;u;Y^%4dVas(TUYxtAX3DaD!$!rh6{}FV^Q_=GmN=~ zj&d?q0sks0iXuth3iz@@bBcZER*zox7#Jn5C0sb>U-+^@Mn;!?^IpF{P1S*0il*_zK_bM)c^nh07*qo IM6N<$f@6r}B>(^b diff --git a/images/slate.png b/images/slate.png deleted file mode 100644 index 8a37ef870c8e84efb016256524f5eecb20b92b65..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 523 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdzwj^(N7a$D;Kb?2i11Zh|kH}&M z25w;xW@MN(M*=9wUgGKN%Kn&xgOf$Iv$87>C?uO15>euupPQSSR|4cRFgO>bCYGe8 zD3oWGWGJ|M`Ua%vrLr?HFy?!@IEF+VJ{x{y|;%#>OG{+E02^q-@6>5WUd)gFVy zW$U=Tx9D^;G;}a9WUXar2;y2JC>S*@=+@6UpWoGfwOY6Ae){eo=4wg;f>VOZ0#8C#8ydRB}}*~Z-44leTx`&*P&f8=kfxUf#)<^9C!>s;BdpKxuScSQMt z;HR|brGhU6-`e-u-nNb0^F8IY;>nitctJU96{NN+(rMVz;mdc{%+cw>Osa_F#eZoBV-<(QH8@u}_ zT-cGi@9qH(#{MhUBL&V*IV|e(^^V>Bm+EqBTeiPCu;N?)ml~IAf%YHwiMf2Wx#j=I z&0XZ;=Q+hI=2yrw{!TA(Kfn0zk;VU6%U7wmgX9Y$&a<PLATFORMER -
+
+
+
\ No newline at end of file diff --git a/js-platformer.sublime-workspace b/js-platformer.sublime-workspace new file mode 100644 index 0000000..7d2c64a --- /dev/null +++ b/js-platformer.sublime-workspace @@ -0,0 +1,263 @@ +{ + "auto_complete": + { + "selected_items": + [ + [ + "piece", + "pieceOrig" + ], + [ + "ge", + "genPosition" + ], + [ + "up", + "updateWorldPosition" + ], + [ + "max", + "maxX" + ], + [ + "get", + "getPosition" + ], + [ + "char", + "characters" + ], + [ + "cha", + "characters" + ], + [ + "create", + "createMap" + ], + [ + "dis", + "disableLeftCollision" + ], + [ + "disa", + "disableTopCollision" + ] + ] + }, + "buffers": + [ + { + "file": "script.js", + "settings": + { + "buffer_size": 11539, + "encoding": "UTF-8", + "line_ending": "Unix" + } + } + ], + "build_system": "", + "build_system_choices": + [ + ], + "build_varint": "", + "command_palette": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + [ + "remove", + "Package Control: Remove Package" + ], + [ + "install\t", + "Package Control: Install Package" + ] + ], + "width": 0.0 + }, + "console": + { + "height": 0.0, + "history": + [ + ] + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "expanded_folders": + [ + "/home/keep/Projects/js-platformer", + "/home/keep/Projects/js-platformer/images" + ], + "file_history": + [ + "/home/keep/Projects/js-platformer/index.html" + ], + "find": + { + "height": 40.0 + }, + "find_in_files": + { + "height": 0.0, + "where_history": + [ + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + "world", + "\"root\"", + "var i =", + "images/block.png" + ], + "highlight": true, + "in_selection": false, + "preserve_case": false, + "regex": false, + "replace_history": + [ + "\"world\"", + "block" + ], + "reverse": false, + "show_context": true, + "use_buffer2": true, + "whole_word": false, + "wrap": true + }, + "groups": + [ + { + "selected": 0, + "sheets": + [ + { + "buffer": 0, + "file": "script.js", + "semi_transient": false, + "settings": + { + "buffer_size": 11539, + "regions": + { + }, + "selection": + [ + [ + 10182, + 10182 + ] + ], + "settings": + { + "syntax": "Packages/JavaScript/JavaScript.sublime-syntax", + "translate_tabs_to_spaces": false + }, + "translation.x": 0.0, + "translation.y": 536.0, + "zoom_level": 1.0 + }, + "stack_index": 0, + "type": "text" + } + ] + } + ], + "incremental_find": + { + "height": 22.0 + }, + "input": + { + "height": 35.0 + }, + "layout": + { + "cells": + [ + [ + 0, + 0, + 1, + 1 + ] + ], + "cols": + [ + 0.0, + 1.0 + ], + "rows": + [ + 0.0, + 1.0 + ] + }, + "menu_visible": true, + "output.find_results": + { + "height": 0.0 + }, + "pinned_build_system": "", + "project": "js-platformer.sublime-project", + "replace": + { + "height": 71.0 + }, + "save_all_on_build": true, + "select_file": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "select_project": + { + "height": 500.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 380.0 + }, + "select_symbol": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "selected_group": 0, + "settings": + { + }, + "show_minimap": true, + "show_open_files": false, + "show_tabs": true, + "side_bar_visible": true, + "side_bar_width": 218.0, + "status_bar_visible": true, + "template_settings": + { + } +} diff --git a/script.js b/script.js index f91f94b..674f12d 100644 --- a/script.js +++ b/script.js @@ -6,11 +6,14 @@ var CHUNK_SIZE = UNIT *8; //collision elements should be larger than one chunk! var keyboard = {}, keyboardStamps = {}, keyboardDoubles = {}; +var world; + var worldObjects = []; var sprites = []; +var characters = []; + var timePosition = 0; -var char; function createNode(pos, size, src) { @@ -42,11 +45,16 @@ function createNode(pos, size, src) { this.pos = {x:x, y:y}; this.dev.style.left = this.pos.x +"px"; this.dev.style.top = this.pos.y +"px"; + }, + + remove: function() { + wolrd.removeChild(this.dev); + delete this; } }; node.move(pos.x, pos.y); - document.getElementById("root").appendChild(dev); + world.appendChild(dev); return node; } @@ -64,8 +72,22 @@ function getSurroundingWorldObjects(pos) { return objs; } -function createWorldObject(pos, size, src) { - obj = createNode(pos, size, src); +function createWorldObject(pos, size, id) { + obj = createNode(pos, size, "images/" +id +".png"); + switch (id) { + case "platform": + obj.disableRightCollision = true; + obj.disableLeftCollision = true; + obj.disableBottomCollision = true; + break; + case "booster": + obj.disableRightCollision = true; + obj.disableLeftCollision = true; + obj.disableBottomCollision = true; + obj.disableTopCollision = true; + break; + } + obj.id = id; var chunkX = Math.floor(pos.x /CHUNK_SIZE); var chunkY = Math.floor(pos.y /CHUNK_SIZE); if (!worldObjects[chunkX]) worldObjects[chunkX] = []; @@ -81,7 +103,7 @@ function createWorldObject(pos, size, src) { if (oldChunkX != newChunkX || oldChunkY != newChunkY) { if (worldObjects[oldChunkX] && worldObjects[oldChunkX][oldChunkY]) { var oldChunk = worldObjects[oldChunkX][oldChunkY]; - for (var i = 0; i < oldChunk.length; i++) { + for (var i=0; i < oldChunk.length; i++) { if (oldChunk[i] == obj) { oldChunk.splice(i, 1); break; @@ -137,13 +159,44 @@ function createSprite(pos, size, src) { return sprite; } -function createCharacter(x, y, src, controls) { - var chr = createSprite({x:x, y:y}, {x:UNIT /2, y:UNIT}, src); +function createCharacter(x, y, id) { + var color, controls; + switch (id) { + case 0: + color = "white"; + controls = { + left: "KeyA", + up: "KeyW", + right: "KeyD", + down: "KeyS" + }; + break; + case 1: + color = "pink"; + controls = { + left: "ArrowLeft", + up: "ArrowUp", + right: "ArrowRight", + down: "ArrowDown" + }; + break; + case 2: + color = "yellow"; + controls = { + left: "KeyH", + up: "KeyU", + right: "KeyK", + down: "KeyJ" + }; + break; + } + var chr = createSprite({x:x, y:y}, {x:UNIT /2, y:UNIT}, "images/char-" +color +".png"); chr.addAnimation("walking", [{x:1, y:0}, {x:2, y:0}, {x:3, y:0}, {x:4, y:0}, {x:5, y:0}, {x:6, y:0}], 5); chr.addAnimation("running", [{x:1, y:1}, {x:2, y:1}, {x:3, y:1}, {x:4, y:1}, {x:5, y:1}, {x:6, y:1}], 3); chr.addAnimation("jumping", [{x:0, y:2}, {x:1, y:2}, {x:2, y:2}], 3); chr.controls = controls; chr.velY = 0; + characters[id] = chr; return chr } @@ -161,87 +214,218 @@ function getCollision(pos, size, obstacles) { } -function update() { - timePosition++; - - if (timePosition %2 == 0) { - var walking, running, canJump; +function updateCharacter(char) { + var walking = null, running = null, canJump = null; - var pos = char.getPosition(); - if (keyboard[char.controls.left]) { - if (keyboardDoubles[char.controls.left]) { - pos.x -= 3; - running = true; - } + var pos = char.getPosition(); + if (keyboard[char.controls.left]) { + if (keyboardDoubles[char.controls.left]) { pos.x -= 3; - char.transform(-1); - walking = true; + running = true; } - else if (keyboard[char.controls.right]) { - if (keyboardDoubles[char.controls.right]) { - pos.x += 3; - running = true; - } + pos.x -= 3; + char.transform(-1); + walking = true; + } + else if (keyboard[char.controls.right]) { + if (keyboardDoubles[char.controls.right]) { pos.x += 3; - char.transform(1); - walking = true; + running = true; + } + pos.x += 3; + char.transform(1); + walking = true; + } + + var charSize = char.getSize(); + + var col = getCollision(pos, charSize, getSurroundingWorldObjects(pos)); + for (var i=0; i < col.length; i++) { + var colPosition = col[i].getPosition(), colSize = col[i].getSize(); + var charCenter = char.getCenter(), colCenter = col[i].getCenter(); + if (charCenter.x -colCenter.x > 0 && !col[i].disableRightCollision) { + pos.x = colPosition.x +colSize.x; + } else if (!col[i].disableLeftCollision) { + pos.x = colPosition.x -charSize.x; } + } + + char.velY += 1.5; + //char.velY = Math.max(Math.min(char.velY, 48), -48); + var fall = Math.floor(char.velY); + var step = 16; + + for (var j = 0; j <= Math.floor(Math.abs(char.velY /step)); j++) { + + if (j == 0) pos.y += Math.floor(char.velY %step); + else if (char.velY > 0) pos.y += step; + else pos.y -= step; - var charSize = char.getSize(); + var breakFall; - var col = getCollision(pos, charSize, getSurroundingWorldObjects(pos)); + col = getCollision(pos, charSize, getSurroundingWorldObjects(pos)); for (var i=0; i < col.length; i++) { + switch (col[i].id) { + case "booster": + if (char.velY <= 0) { + char.velY -= 2; + } + break; + } var colPosition = col[i].getPosition(), colSize = col[i].getSize(); var charCenter = char.getCenter(), colCenter = col[i].getCenter(); - if (charCenter.x -colCenter.x > 0) { - pos.x = colPosition.x +colSize.x; - } else { - pos.x = colPosition.x -charSize.x; + if (charCenter.y -colCenter.y > 0 && !col[i].disableBottomCollision) { + pos.y = colPosition.y +colSize.y; + char.velY = 0; + breakFall = true; + } else if (!col[i].disableTopCollision && char.velY >= 0) { + pos.y = colPosition.y -charSize.y; + char.velY = 0; + canJump = true; + breakFall = true; } } + if (breakFall) break; + } - char.velY += 1.5; - char.velY = Math.max(Math.min(char.velY, 48), -48); - var fall = Math.floor(char.velY); - var step = 16; - - for (var j = 0; j <= Math.floor(Math.abs(char.velY /step)); j++) { - - if (j == 0) pos.y += Math.floor(char.velY %step); - else if (char.velY > 0) pos.y += step; - else pos.y -= step; - - var breakFall; - - col = getCollision(pos, charSize, getSurroundingWorldObjects(pos)); - for (var i=0; i < col.length; i++) { - var colPosition = col[i].getPosition(), colSize = col[i].getSize(); - var charCenter = char.getCenter(), colCenter = col[i].getCenter(); - if (charCenter.y -colCenter.y > 0) { - pos.y = colPosition.y +colSize.y; - char.velY = 0; - breakFall = true; - } else { - pos.y = colPosition.y -charSize.y; - char.velY = 0; - canJump = true; - breakFall = true; - } + if (canJump) { + if (keyboard[char.controls.up]) { + char.setAnimation("jumping", {override: true}); + char.velY = -16; + } else if (running) char.setAnimation("running"); + else if (walking) char.setAnimation("walking"); + else char.setFrame(0, 0, true); + } + else if (char.velY > 0) char.setFrame(2, 2, true); + + char.move(pos.x, pos.y); +} + +function updateWorldPosition() { + var min = null, max = null; + for (var i=0; i < characters.length; i++) { + if (characters[i]) { + var pos = characters[i].getCenter(); + if (min && max) { + min.x = Math.min(min.x, pos.x); + min.y = Math.min(min.y, pos.y); + max.x = Math.max(max.x, pos.x); + max.y = Math.max(max.y, pos.y); + } else { + min = {x:pos.x, y:pos.y}; + max = {x:pos.x, y:pos.y}; } - if (breakFall) break; } + } + var center = {x:Math.floor((min.x +max.x) /2), y:Math.floor((min.y +max.y) /2)}; + world.style.left = -center.x +"px" + world.style.top = -center.y +"px" +} - if (canJump) { - if (keyboard[char.controls.up]) { - char.setAnimation("jumping", {override: true}); - char.velY = -16; - } else if (running) char.setAnimation("running"); - else if (walking) char.setAnimation("walking"); - else char.setFrame(0, 0, true); +function createMap(map, offset, dir) { + if (!dir) dir = 1; + + var createdObjects = []; + for (var y=0; y < map.length; y++) { + var layer = map[y]; + for (var x=0; x < layer.length; x++) { + var id = null; + switch (layer[x]) { + case "#": + id = "block"; + break; + case "-": + id = "platform"; + break; + case "^": + id = "booster"; + break; + } + if (id) createdObjects.push(createWorldObject({x:x *UNIT *dir +offset.x, y:y *UNIT +offset.y}, {x:UNIT, y:UNIT}, id)); } - else if (char.velY > 0) char.setFrame(2, 2, true); + } + return createdObjects; +} + - char.move(pos.x, pos.y); +var genPieces = [ + { + map: [".-...--", + "", + "", + "##"], + start: {x:0, y:3}, + end: {x:6, y:0} + }, + { + map: ["----"], + start: {x:0, y:0}, + end: {x:3, y:0} + }, + { + map: ["--"], + start: {x:0, y:0}, + end: {x:1, y:0} + }, + { + map: ["...-", + "", + "..^", + "", + "", + "--"], + start: {x:0, y:4}, + end: {x:4, y:0} + }, + { + map: ["#", + "", + "", + "", + "..^", + "", + "---"], + start: {x:0, y:6}, + end: {x:0, y:0} + }, + { + map: ["#"], + start: {x:0, y:0}, + end: {x:0, y:0} + } +]; +var genStepVariants = [ + {x:2, y:-2}, + {x:3, y:-2} +]; +var genPosition = {x:Math.fround(Math.random() *7) -4, y:0}; + +function createNextPiece() { + var piece = genPieces[Math.floor(Math.random() *genPieces.length)]; + var step = genStepVariants[Math.floor(Math.random() *genStepVariants.length)]; + var dir = Math.random() > .5 ? -1 : 1; + + genPosition.x += (step.x -piece.start.x) *dir; + genPosition.y += step.y -piece.start.y; + + createMap(piece.map, {x:genPosition.x *UNIT, y:genPosition.y *UNIT}, dir); + + genPosition.x += piece.end.x *dir; + genPosition.y += piece.end.y; +} + + +function update() { + timePosition++; + + if (timePosition %2 == 0) { + for (var i=0; i < characters.length; i++) { + if (characters[i]) updateCharacter(characters[i]); + } + updateWorldPosition(); + if (timePosition %120 == 0) { + createNextPiece(); + } } } @@ -273,30 +457,21 @@ function loop() { } -function main() { - - createWorldObject({x:-UNIT *4, y:0}, {x:UNIT *12, y:UNIT}, "images/block.png"); - createWorldObject({x:-UNIT *4, y:-UNIT}, {x:UNIT, y:UNIT}, "images/block.png"); - createWorldObject({x:UNIT *3, y:-UNIT *2}, {x:UNIT, y:UNIT *2}, "images/block.png"); - createWorldObject({x:-UNIT *2, y:-UNIT *2}, {x:UNIT, y:UNIT}, "images/block.png"); - createWorldObject({x:UNIT, y:-UNIT *3}, {x:UNIT, y:UNIT}, "images/block.png"); - - char = createCharacter(-UNIT /4, -UNIT, "images/char.png", { - left: "KeyA", - up: "KeyW", - right: "KeyD", - down: "KeyS" - }); +function main() { - var bull = createSprite({x:128, y:-44}, {x:64, y:44}, "images/bull.png"); - bull.addAnimation("walking", [ - {x:1, y:0}, {x:2, y:0}, {x:3, y:0}, - {x:1, y:1}, {x:2, y:1}, {x:3, y:1} - ], 5); - bull.setAnimation("walking", {loop: true}); + world = document.getElementById("world"); + createMap([ + "................", + "................", + "################", + "################" + ], {x:-UNIT *8, y:-UNIT *2}); + createCharacter(UNIT *(-.25), -UNIT, 0); + createCharacter(UNIT *(-1.25), -UNIT, 1); + //createCharacter(UNIT *.75, -UNIT, 2); document.addEventListener("keydown", function(evt) { if (!keyboard[evt.code] && keyboardStamps[evt.code] && timePosition -keyboardStamps[evt.code] < 15) {