Guest User

Untitled

a guest
Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1.  
  2.  
  3.  
  4. <!DOCTYPE html>
  5.  
  6. <html>
  7.  
  8. <head>
  9.  
  10. <title>raycaster</title>
  11.  
  12.  
  13.  
  14. <meta charset="utf-8">
  15.  
  16.  
  17.  
  18. <script>
  19.  
  20. var map = {};
  21.  
  22. map.data = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  23.  
  24. [1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1],
  25.  
  26. [1, 0, 0, 0, 3, 0, 2, 0, 2, 0, 3, 0, 0, 0, 0, 1],
  27.  
  28. [1, 0, 0, 0, 2, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 1],
  29.  
  30. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  31.  
  32. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  33.  
  34. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  35.  
  36. [1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1]];
  37.  
  38. map.width = map.data[0].length;
  39.  
  40. map.height = map.data.length;
  41.  
  42. map.tileSize = 32;
  43.  
  44.  
  45.  
  46. var colors = ["white", "gray", "blue", "red"];
  47.  
  48.  
  49.  
  50. var canvasWidth = map.width * map.tileSize;
  51.  
  52. var canvasHeight = map.height * map.tileSize;
  53.  
  54.  
  55.  
  56. var player = {};
  57.  
  58. player.x = ((map.width / 2) - 2.5) * map.tileSize;
  59.  
  60. player.y = ((map.height / 2) + 0.5) * map.tileSize;
  61.  
  62. player.angle = 0;//Math.PI / 2;
  63.  
  64. player.size = 16;
  65.  
  66.  
  67.  
  68. var rendererContext, mapContext;
  69.  
  70.  
  71.  
  72. var keys = [];
  73.  
  74. var KEY_LEFT = 37, KEY_UP = 38, KEY_RIGHT = 39, KEY_DOWN = 40;
  75.  
  76.  
  77.  
  78. var fov = Math.PI / 2.5, raySize = 2, rayCount = canvasWidth / raySize;
  79.  
  80.  
  81.  
  82. window.onload = function() {
  83.  
  84. var rendererCanvas = document.createElement("canvas");
  85.  
  86. rendererCanvas.width = canvasWidth;
  87.  
  88. rendererCanvas.height = canvasHeight;
  89.  
  90. document.body.appendChild(rendererCanvas);
  91.  
  92. rendererContext = rendererCanvas.getContext("2d");
  93.  
  94.  
  95.  
  96. var mapCanvas = document.createElement("canvas");
  97.  
  98. mapCanvas.width = canvasWidth;
  99.  
  100. mapCanvas.height = canvasHeight;
  101.  
  102. document.body.appendChild(mapCanvas);
  103.  
  104. mapContext = mapCanvas.getContext("2d");
  105.  
  106. };
  107.  
  108.  
  109.  
  110. window.setInterval(function() {
  111.  
  112. update();
  113.  
  114. draw();
  115.  
  116. }, 1000 / 30);
  117.  
  118.  
  119.  
  120. window.onkeydown = function(event) {
  121.  
  122. keys[event.which] = true;
  123.  
  124. };
  125.  
  126.  
  127.  
  128. window.onkeyup = function(event) {
  129.  
  130. keys[event.which] = false;
  131.  
  132. };
  133.  
  134.  
  135.  
  136. function update() {
  137.  
  138. if (keys[KEY_UP]) {
  139.  
  140. player.x += Math.cos(player.angle) * 2;
  141.  
  142. player.y -= Math.sin(player.angle) * 2;
  143.  
  144. }
  145.  
  146.  
  147.  
  148. if (keys[KEY_DOWN]) {
  149.  
  150. player.x -= Math.cos(player.angle);
  151.  
  152. player.y += Math.sin(player.angle);
  153.  
  154. }
  155.  
  156.  
  157.  
  158. if (keys[KEY_RIGHT])
  159.  
  160. player.angle -= 0.1;
  161.  
  162.  
  163.  
  164. if (keys[KEY_LEFT])
  165.  
  166. player.angle += 0.1;
  167.  
  168. }
  169.  
  170.  
  171.  
  172. function draw() {
  173.  
  174. //Map
  175.  
  176. mapContext.clearRect(0, 0, canvasWidth, canvasHeight);
  177.  
  178.  
  179.  
  180. for(var y = 0; y < map.height; y++) {
  181.  
  182. for(var x = 0; x < map.width; x++) {
  183.  
  184. if (map.data[y][x] == 0)
  185.  
  186. continue;
  187.  
  188.  
  189.  
  190. mapContext.fillStyle = colors[map.data[y][x]];
  191.  
  192. mapContext.fillRect(x * map.tileSize, y * map.tileSize, map.tileSize, map.tileSize);
  193.  
  194. }
  195.  
  196. }
  197.  
  198.  
  199.  
  200. mapContext.beginPath();
  201.  
  202. mapContext.arc(player.x, player.y, player.size / 2, 0, Math.PI * 2);
  203.  
  204. mapContext.fillStyle = "green";
  205.  
  206. mapContext.fill();
  207.  
  208.  
  209.  
  210. //Renderer
  211.  
  212. rendererContext.clearRect(0, 0, canvasWidth, canvasHeight);
  213.  
  214.  
  215.  
  216. var hits = [];
  217.  
  218. for(var i = 0; i < rayCount; i++) {
  219.  
  220. var angle = (player.angle + (fov / 2)) - fov * (i / rayCount);
  221.  
  222. var hit = castRay(player.x, player.y, angle);
  223.  
  224. hit.i = i;
  225.  
  226. hits.push(hit);
  227.  
  228.  
  229.  
  230. mapContext.beginPath();
  231.  
  232. mapContext.moveTo(hit.ox, hit.oy);
  233.  
  234. mapContext.lineTo(hit.x, hit.y);
  235.  
  236. mapContext.strokeStyle = "yellow";
  237.  
  238. mapContext.stroke();
  239.  
  240. }
  241.  
  242.  
  243.  
  244. hits.sort(function(a, b) {
  245.  
  246. return b.dist - a.dist;
  247.  
  248. });
  249.  
  250.  
  251.  
  252. for(var i = 0; i < hits.length; i++) {
  253.  
  254. var hit = hits[i];
  255.  
  256. var mh = canvasHeight*16;
  257.  
  258. var height = Math.max(mh-(mh-(1/hit.dist)*mh), 0);
  259.  
  260. rendererContext.fillStyle = hit.color;
  261.  
  262. rendererContext.fillRect(hit.i * raySize, (canvasHeight / 2) - (height / 2), raySize, height);
  263.  
  264. rendererContext.fill();
  265.  
  266. }
  267.  
  268. }
  269.  
  270.  
  271.  
  272. function castRay(ox, oy, angle) {
  273.  
  274. var x = ox;
  275.  
  276. var y = oy;
  277.  
  278.  
  279.  
  280. var color = 0;
  281.  
  282.  
  283.  
  284. angle = angle%(Math.PI*2);
  285.  
  286.  
  287.  
  288. while(true) {
  289.  
  290. if (x < 0 || y < 0 || x > map.width * map.tileSize || y > map.height * map.tileSize)
  291.  
  292. break;
  293.  
  294.  
  295.  
  296. var mapX = Math.floor(x / map.tileSize);
  297.  
  298. var mapY = Math.floor(y / map.tileSize);
  299.  
  300.  
  301.  
  302. if (map.data[mapY][mapX] != 0) {
  303.  
  304. color = colors[map.data[mapY][mapX]];
  305.  
  306. break;
  307.  
  308. }
  309.  
  310.  
  311.  
  312. x += Math.cos(angle);
  313.  
  314. y -= Math.sin(angle);
  315.  
  316. }
  317.  
  318.  
  319.  
  320. var dist = Math.sqrt(Math.pow(x - ox, 2) + Math.pow(y - oy, 2));
  321.  
  322. var ndist = dist*Math.cos(player.angle-angle);
  323.  
  324.  
  325.  
  326. return {ox: ox, oy: oy, x: x, y: y, color: color, dist: ndist};
  327.  
  328. }
  329.  
  330. </script>
  331.  
  332. </head>
  333.  
  334. <body>
  335.  
  336. </body>
  337.  
  338. </html>
Add Comment
Please, Sign In to add comment