Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. function RayCastCamera(fov,resolution) {
  2. this.fov = fov;
  3. this.resolution = resolution; //How Many ray should we cast and draw;
  4. }
  5.  
  6. RayCastCamera.prototype.render = function(map, pos, angle, walls) {
  7. ctx.fillStyle = "black";
  8. ctx.fillRect(0,0,width,height*0.5); //The Top of the Screen is black
  9. ctx.fillStyle = "grey";
  10. ctx.fillRect(0,height*0.5,width,height*0.5); // The bottom is grey
  11. for(var i = 0; i<this.resolution; i++) {
  12. var x = i / this.resolution - 0.5;
  13. var deltaAngle = Math.atan2(x, this.fov); // Get the angle for this ray
  14. var ray = map.cast(post, angle+deltaAngle, 10, ~1); // cast a ray, from the pos in the given angle, for a maximum of 10 unit and colliding whit everyblock except id=1
  15. //Some maths to calculate the actual height of the wall based on the distance
  16. //A wall, 1 tile directly in front of the player would fit the whole screen
  17. var wallHeight = height / (ray.length * Math.cos(deltaAngle));
  18. if (walls[ray.id]) { // We got an image for this wall type
  19. var wall = walls[ray.id];
  20. //We draw a pixel collum from the image
  21. ctx.drawImage(wall(ray.offset*wall.width)|0,0,1,wall.height,i,(height-wallHeight)*0.5,1,wallHeight)
  22. } else {
  23. ctx.fillStyle = "white"; //We draw it white;
  24. ctx.fillRect(i,(height-wallHeight)*0.5,1,wallHeight)
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement