Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.92 KB | None | 0 0
  1. // Version 3 - Basic collision (viewbox), perfectly elastic (no energy loss)
  2. var canvas = document.getElementById("palco");
  3. var ctx = canvas.getContext('2d');
  4. var myScreen = {linhas:canvas.height/32, colunas:canvas.width/32};
  5.  
  6. var fps = 10;
  7. var myTeclado = {btnNumero:0, btnTecla:"", btnCtrl:false, btnAlt:false, btnShift:false};
  8. var myMouse = {x:0, y:0, c:0, l:0};
  9. var seExaminar=false, seColisionVisible=false;
  10. var contLoadImagens=0, MaxLoadImagens=2;
  11. var mapa=null, sprite=null, sptChar=null;
  12.  
  13. document.onmousemove = onMouseMove;
  14. document.onmousedown = onMouseDown;
  15. document.onkeydown = onKeyDown;
  16. document.onkeypress = onKeyPress;
  17. //document.onkeypress = onKeyPress(event);} // Não está funcionando do Chromium
  18.  
  19. function loadXML(url){
  20. //by Micox: micoxjcg@yahoo.com.br.
  21. if(window.XMLHttpRequest){
  22. var Loader = new XMLHttpRequest();
  23. Loader.open("GET", url ,false);
  24. Loader.send(null);
  25. return Loader.responseXML;
  26. }else if(window.ActiveXObject){
  27. var Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
  28. Loader.async = false;
  29. Loader.load(url);
  30. return Loader;
  31. }
  32. }
  33. function RascunhoDeCodigo(){
  34. /*var map={
  35. url:"z10.tmx",
  36. version:"1.0",
  37. orientation:"orthogonal",
  38. width:20,
  39. height:15,
  40. tilewidth:32,
  41. tileheight:32,
  42. tileset:[
  43. {
  44. firstgid:1,
  45. name:"collision",
  46. tilewidth:32,
  47. tileheight:32,
  48. image:"../tilesets/collision.png"
  49. },{
  50. firstgid:3,
  51. name:"woodland_ground",
  52. tilewidth:32,
  53. tileheight:32,
  54. image:"../tilesets/woodland_ground.png"
  55. }
  56. ],
  57. layer:[
  58. {
  59. name:"background1",
  60. width:20,
  61. height:15,
  62. opacity:1.0,
  63.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,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ],
  96. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ],
  97. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ],
  98. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ],
  99. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ],
  100. [1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ]
  101. ]
  102. }
  103. ]
  104. };/**/
  105. }
  106. function loadMap(url){
  107. xmlNode = loadXML(url);
  108. for(var n=0;n<xmlNode.childNodes.length;n++){//percorrendo os filhos do nó
  109. novoMap=[];
  110. if(xmlNode.childNodes[n].nodeType == 1){//ignorar espaços em branco
  111. nodeMap = xmlNode.childNodes[n];
  112. if(nodeMap.nodeName=="map"){
  113. novoMap.url=url;
  114. for(var a=0;a<nodeMap.attributes.length;a++){
  115. atrib = nodeMap.attributes[a];
  116. if(atrib.nodeName=="version"){
  117. novoMap.version=atrib.nodeValue;
  118. }else if(atrib.nodeName=="orientation"){
  119. novoMap.orientation=atrib.nodeValue;
  120. }else if(atrib.nodeName=="width"){
  121. novoMap.width=atrib.nodeValue;
  122. }else if(atrib.nodeName=="height"){
  123. novoMap.height=atrib.nodeValue;
  124. }else if(atrib.nodeName=="tilewidth"){
  125. novoMap.tilewidth=atrib.nodeValue;
  126. }else if(atrib.nodeName=="tileheight"){
  127. novoMap.tileheight=atrib.nodeValue;
  128. }else if(atrib.nodeName=="tileset"){
  129.  
  130. }
  131. }
  132.  
  133. novoMap.tileset=[];
  134. novoMap.layer=[];
  135.  
  136. for(var t=0;t<nodeMap.childNodes.length;t++){
  137. nodeProp = nodeMap.childNodes[t];
  138. if(nodeProp.nodeType == 1){
  139. if(nodeProp.nodeName=="tileset"){
  140. contTile=novoMap.tileset.length;
  141. novoMap.tileset[contTile]=[];
  142. for(var a=0;a<nodeProp.attributes.length;a++){
  143. var atrib = nodeProp.attributes[a];
  144.  
  145. if(atrib.nodeName=="firstgid"){
  146. novoMap.tileset[contTile].firstgid=atrib.nodeValue;
  147. }else if(atrib.nodeName=="name"){
  148. novoMap.tileset[contTile].name=atrib.nodeValue;
  149. }else if(atrib.nodeName=="tilewidth"){
  150. novoMap.tileset[contTile].tilewidth=atrib.nodeValue;
  151. }else if(atrib.nodeName=="tilewidth"){
  152. novoMap.tileset[contTile].name=atrib.nodeValue;
  153. }else if(nodeProp.childNodes.length>=1){
  154. for(var contImage=0;contImage<nodeProp.childNodes.length;contImage++){
  155. nodeImage = nodeProp.childNodes[contImage];
  156. if(nodeImage.nodeType == 1 && nodeImage.nodeName=="image"){
  157. for(var ai=0;ai<nodeImage.attributes.length;ai++){
  158. atribImage = nodeImage.attributes[ai];
  159. if(atribImage.nodeName=="source"){
  160. novoMap.tileset[contTile].image=atribImage.nodeValue;
  161. }
  162. }
  163.  
  164. }
  165. }
  166. }
  167. }
  168. }else if(nodeProp.nodeName=="layer"){
  169. contLayer=novoMap.layer.length;
  170. novoMap.layer[contLayer]=[];
  171. novoMap.layer[contLayer].opacity=1.0;
  172. for(var al=0;al<nodeProp.attributes.length;al++){
  173. atribLayer = nodeProp.attributes[al];
  174. if(atribLayer.nodeName=="name"){
  175. novoMap.layer[contLayer].name=atribLayer.nodeValue;
  176. if(atribLayer.nodeValue="collision") novoMap.layercollision=contLayer;
  177. }else if(atribLayer.nodeName=="opacity"){
  178. novoMap.layer[contLayer].opacity=atribLayer.nodeValue;
  179. }else if(atribLayer.nodeName=="width"){
  180. novoMap.layer[contLayer].width=atribLayer.nodeValue;
  181. }else if(atribLayer.nodeName=="height"){
  182. novoMap.layer[contLayer].height=atribLayer.nodeValue;
  183. }
  184. }
  185. //document.write("novoMap.layer["+(contLayer)+"].name='"+novoMap.layer[contLayer].name+"'<br/>");
  186.  
  187. for(var np=0;np<nodeProp.childNodes.length;np++){
  188. nodeData = nodeProp.childNodes[np];
  189. if(nodeData.nodeType == 1){
  190. if(nodeData.nodeName=="data"){
  191. //document.write("nodeData.nodeName='"+nodeData.nodeName+"'<br/>");
  192. novoMap.layer[contLayer].data=[];
  193. for(var e=0;e<nodeData.childNodes.length;e++){
  194. nodeTile = nodeData.childNodes[e];
  195. if(nodeTile.nodeType == 1){
  196. if(nodeTile.nodeName=="tile"){
  197. contData=novoMap.layer[contLayer].data.length;
  198. for(var nd=0;nd<nodeTile.attributes.length;nd++){
  199. atribTile=nodeTile.attributes[nd];
  200. if(atribTile.nodeName=="gid"){
  201. novoMap.layer[contLayer].data[contData]=atribTile.nodeValue;
  202. }
  203. }
  204. //document.write("novoMap.layer["+(contLayer)+"].data["+contData+"]='"+novoMap.layer[contLayer].data[contData]+"'<br/>");
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. return novoMap;
  218. }
  219. function loadImagens(){
  220. sprite = {imagem:null, linhas:16, colunas:16};
  221. var imgBkg = new Image();
  222. imgBkg.src = "tilesets/woodland_ground.png";
  223. imgBkg.onload = onLoadImagem(sprite, imgBkg);
  224.  
  225. sptChar = {
  226. imagem:null,
  227. linhas:8, colunas:9,
  228. c:10, l:7,
  229. x:canvas.width/2-16,
  230. y:canvas.height/2+8
  231. };
  232. var imgChar = new Image();
  233. imgChar.src = "sprites/body/player_white_male.png";
  234. imgChar.onload = onLoadImagem(sptChar, imgChar);/**/
  235.  
  236. } loadImagens();
  237. function loadMaps(){
  238. /*mapa={
  239. url:"z10.tmx",
  240. version:"1.0",
  241. orientation:"orthogonal",
  242. layercollision:2,
  243. width:20,
  244. height:15,
  245. tilewidth:32,
  246. tileheight:32,
  247. tileset:[
  248. {
  249. firstgid:1,
  250. name:"collision",
  251. tilewidth:32,
  252. tileheight:32,
  253. image:"../tilesets/collision.png"
  254. },{
  255. firstgid:3,
  256. name:"woodland_ground",
  257. tilewidth:32,
  258. tileheight:32,
  259. image:"../tilesets/woodland_ground.png"
  260. }
  261. ],
  262. layer:[
  263. {
  264. name:"background1",
  265. width:20,
  266. height:15,
  267. opacity:1.0,
  268. data:[
  269. [212,212,212,212,212,212,212,212,212,0 ,0 ,0 ,0 ,0 ,0 ,212,212,212,212,212],
  270. [0 ,69 ,70 ,70 ,70 ,71 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,69 ],
  271. [0 ,85 ,125,86 ,86 ,87 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  272. [0 ,85 ,141,86 ,86 ,87 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  273. [0 ,85 ,86 ,86 ,86 ,87 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  274. [0 ,101,102,102,102,103,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  275. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  276. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  277. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,85 ],
  278. [0 ,0 ,0 ,0 ,0 ,0 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,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ],
  344. [0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ],
  345. [1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ]
  346. ]
  347. }/**/
  348. }loadMaps();
  349.  
  350. function init() {
  351. setInterval(update, 1000/fps);
  352. }
  353. function onKeyPress(event){
  354. myTeclado.btnNumero = event.keyCode;
  355. myTeclado.btnTecla = String.fromCharCode(event.charCode);
  356. myTeclado.btnCtrl = event.ctrlKey;
  357. myTeclado.btnAlt = event.altKey;
  358. myTeclado.btnShift = event.shiftKey;
  359. }
  360. function onKeyDown(event){
  361. myTeclado.btnNumero = event.keyCode;
  362. myTeclado.btnTecla = String.fromCharCode(event.charCode);
  363. c=Math.round((sptChar.x+32)/32)+1;
  364. l=Math.round((sptChar.y-32)/32);
  365. if(myTeclado.btnNumero == 27){ //ESC
  366. seColisionVisible = !seColisionVisible;
  367. seExaminar = !seExaminar;
  368. }else if(myTeclado.btnNumero == 40){ //abaixo
  369. sptChar.l+=1;
  370. if(sptChar.l>myScreen.linhas-1 || mapa.colision[sptChar.l][sptChar.c]) sptChar.l-=1;
  371. }else if(myTeclado.btnNumero == 38){ //acima
  372. sptChar.l-=1;
  373. if(sptChar.l<0 || mapa.colision[sptChar.l][sptChar.c]) sptChar.l+=1;
  374. }else if(myTeclado.btnNumero == 39){ //direita
  375. sptChar.c+=1;
  376. if(sptChar.c>myScreen.colunas-1 || mapa.colision[sptChar.l][sptChar.c]) sptChar.c-=1;
  377. }else if(myTeclado.btnNumero == 37){ //esqueda
  378. sptChar.c-=1;
  379. if(sptChar.c<0 || mapa.colision[sptChar.l][sptChar.c]) sptChar.c+=1;
  380. }
  381. }
  382. function onMouseDown(event){
  383. onMouseMove(event);
  384. if(!mapa.colision[myMouse.l][myMouse.c]){
  385. sptChar.c=myMouse.c;
  386. sptChar.l=myMouse.l;
  387. }
  388. }
  389. function onMouseMove(event){
  390. if (event.layerX || event.layerX == 0) { // Firefox
  391. var x = event.layerX-canvas.offsetLeft;
  392. var y = event.layerY-canvas.offsetTop;
  393. } else if (event.offsetX || event.offsetX == 0) { // Opera
  394. var x = event.offsetX-canvas.offsetLeft;
  395. var y = event.offsetY-canvas.offsetTop;
  396. }
  397. var c = Math.round(x/32)-1;
  398. var l = Math.round(y/32)-1;
  399. if(c>=0 && c<=myScreen.colunas-1 && l>=0 && l<=myScreen.linhas-1){
  400. myMouse.x=x; myMouse.y=y;
  401. myMouse.c=c; myMouse.l=l;
  402. }
  403. }
  404. function onLoadImagem(mySprite, myImagem){
  405. mySprite.imagem=myImagem;
  406. //mySprite.linhas=mySprite.imagem.height/blkHeight;
  407. //mySprite.colunas=mySprite.imagem.width/blkWidth;
  408. contLoadImagens++;
  409. if(contLoadImagens>=MaxLoadImagens) init();
  410. }
  411. function printTesto(myTesto, X, Y) {
  412. printTesto(myTesto, X, Y, "", "", "");
  413. }
  414. function printTesto(myTesto, X, Y, myFonte) {
  415. printTesto(myTesto, X, Y, myFonte, "", "");
  416. }
  417. function printTesto(myTesto, X, Y, myFonte, corFonte) {
  418. printTesto(myTesto, X, Y, myFonte, corFonte, "");
  419. }
  420. function printTesto(myTesto, X, Y, myFonte, corFonte, corAureula) {
  421. if(myTesto!=undefined && myTesto!=""){
  422. if(myFonte!=undefined && myFonte!="") ctx.font = myFonte;
  423. if(corAureula!=undefined && corAureula!=""){
  424. //myTesto+=" ["+corAureula+"]";
  425. ctx.fillStyle = corAureula;
  426. ctx.fillText(myTesto, X, Y-1);
  427. ctx.fillText(myTesto, X, Y+1);
  428. ctx.fillText(myTesto, X-1, Y);
  429. ctx.fillText(myTesto, X+1, Y);
  430. }
  431. if(corFonte!=undefined && corFonte!="") ctx.fillStyle = corFonte;
  432. ctx.fillText(myTesto, X, Y);
  433. }
  434. }
  435.  
  436. function drawSprite(mySprite,x,y,bloco){
  437. w=mySprite.imagem.width/mySprite.colunas;
  438. h=mySprite.imagem.height/mySprite.linhas;
  439. x1=Math.round(bloco % mySprite.colunas) * w;
  440. y1=Math.round(bloco / mySprite.colunas) * h;
  441. ctx.drawImage(
  442. mySprite.imagem,
  443. x1,y1,w,h,
  444. x,y,w,h
  445. );
  446. //printTesto("x:"+x+" y:"+y+" ("+mySprite.colunas+"/"+mySprite.linhas+")",5, 30, "10pt Verdana", "rgba(255,255,255,1.0)");
  447. //printTesto("linha: "+linha+" coluna:"+coluna,5, 45, "10pt Verdana", "rgba(255,255,255,1.0)");
  448. }
  449. function drawTileset(x,y,bloco){
  450. var selTile=0;
  451. blocoReal=0;
  452. for(var t=mapa.tileset.length-1;t>=0;t++){
  453. if(bloco>mapa.tileset[t].firstgid){
  454. selTile=t+1;
  455. blocoReal=bloco-mapa.tileset[t].firstgid;
  456. break;
  457. }
  458. }
  459. w=mySprite.imagem.width/mapa.width;
  460. h=mySprite.imagem.height/mapa.width;
  461.  
  462.  
  463. tileset
  464. w=mySprite.imagem.width/mySprite.colunas;
  465. h=mySprite.imagem.height/mySprite.linhas;
  466. x1=Math.round(blocoReal % mySprite.colunas);
  467. y1=Math.round(blocoReal / mySprite.colunas);
  468. ctx.drawImage(
  469. mySprite.imagem,
  470. x1*w, y1*h, w, h,
  471. x*w, y*h, w, h
  472. );
  473. //printTesto("x:"+x+" y:"+y+" ("+mySprite.colunas+"/"+mySprite.linhas+")",5, 30, "10pt Verdana", "rgba(255,255,255,1.0)");
  474. //printTesto("linha: "+linha+" coluna:"+coluna,5, 45, "10pt Verdana", "rgba(255,255,255,1.0)");
  475. }
  476. function drawMap(){
  477. ctx.save();
  478. ctx.fillStyle = "rgba(128,128,128, 1.0)";
  479. ctx.fillRect(0, 0, canvas.width, canvas.height);
  480. //drawSprite(sprite,0,1,0);
  481. //drawSprite(sprite,1,1,0);
  482. var t=0;
  483. ctx.fillStyle = "rgb(255,255,255)";
  484. for(var l=0;l<myScreen.linhas;l++){
  485. for(var c=0;c<myScreen.colunas;c++){
  486. //ctx.drawImage(sprite.imagem,0,0,32,32,c*32,l*32,32,32);
  487.  
  488. //drawTileset(l,c,Math.round(mapa.layer[0].data[l*c]));
  489. drawSprite(sprite,c*32,l*32,Math.round(mapa.background1[l][c]));
  490.  
  491.  
  492. if(seColisionVisible && mapa.colision[l][c]){
  493. //if(seColisionVisible && mapa.layer[mapa.layercollision].data[l*c]){
  494. ctx.fillStyle = "rgba(255,0,0,0.25)";
  495. ctx.fillRect(c*32+1,l*32+1,32-2, 32-2);
  496. }
  497. if(seExaminar){
  498. ctx.fillStyle = "rgba(255,255,255,0.75)";
  499. printTesto(""+t,(c*32)+5,(l*32)+20);
  500. }
  501. }
  502. }/**/
  503. ctx.restore();
  504. }
  505. function drawCusor(){
  506. ctx.save();
  507. //myMouse.c=Math.round((myMouse.x-32)/32);
  508. //myMouse.l=Math.round((myMouse.y-32)/32);
  509. if(
  510. myMouse.c>=0 && myMouse.c<=myScreen.colunas-1 &&
  511. myMouse.l>=0 && myMouse.c<=myScreen.colunas-1 &&
  512. !mapa.colision[myMouse.l][myMouse.c]
  513. ){
  514. ctx.fillStyle = "rgba(0,0,255,0.5)";
  515. ctx.fillRect(myMouse.c*32,myMouse.l*32,32,32);
  516. //ctx.arc(c*32+16,l*32+16,16, 0, Math.PI * 2, true); ctx.fill();
  517. }
  518. ctx.restore();
  519. }
  520. function drawTestos(){
  521. ctx.save();
  522. ctx.fillStyle = "rgba(0,0,0,0.5)";
  523. ctx.fillRect(0,0,640,24);
  524. printTesto(
  525. "FPS:"+fps+" "+
  526. //"Screen:" +myScreen.colunas+"/"+myScreen.linhas+" - "+
  527. "Key:"+myTeclado.btnNumero+(myTeclado.btnTecla.trim()!=""?"["+myTeclado.btnTecla.trim()+"]":"")+" "+
  528. "Mouse:"+myMouse.x+"/"+myMouse.y+"("+myMouse.c+"/"+myMouse.l+") "+
  529. "sptChar:"+sptChar.x+"/"+sptChar.y+"("+sptChar.c+"/"+sptChar.l+") ",
  530. 5, 15, "10pt Verdana", "#000000", "#00FF00"
  531. );
  532. printTesto(
  533. "[Esc] to Debug!",
  534. 530, 15, "10pt Verdana", "#000000", "#FFFFFF"
  535. );
  536. ctx.restore();
  537. }
  538. function update(){
  539. ctx.save();
  540. // Clear display
  541. //ctx.drawImage(imgTileset,0,0,canvas.width,canvas.height);
  542. drawMap();
  543. //ctx.globalAlpha = 0.1;
  544.  
  545. drawCusor();
  546.  
  547. drawSprite(sptChar,sptChar.c*32-16,sptChar.l*32-40,0);
  548.  
  549. if(!seExaminar)drawTestos()
  550.  
  551. //
  552. //ctx.beginPath();
  553.  
  554. //ctx.fillStyle = ball.color;
  555.  
  556. //ctx.fillStyle = ball.color;
  557. //ctx.arc(0, 0, ball.r, 0, Math.PI * 2, true);
  558.  
  559. //ctx.drawImage(imgBola,0,0,imgBola.width,imgBola.height);
  560. //ctx.closePath();
  561. //ctx.fill();
  562.  
  563. ctx.restore();
  564.  
  565. //if(seTxtVisivel==true) showTesto();
  566. }
  567.  
  568. //init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement