Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function RayCastCamera(fov,resolution) {
- this.fov = fov;
- this.resolution = resolution; //How Many ray should we cast and draw;
- }
- RayCastCamera.prototype.render = function(map, pos, angle, walls) {
- ctx.fillStyle = "black";
- ctx.fillRect(0,0,width,height*0.5); //The Top of the Screen is black
- ctx.fillStyle = "grey";
- ctx.fillRect(0,height*0.5,width,height*0.5); // The bottom is grey
- for(var i = 0; i<this.resolution; i++) {
- var x = i / this.resolution - 0.5;
- var deltaAngle = Math.atan2(x, this.fov); // Get the angle for this ray
- 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
- //Some maths to calculate the actual height of the wall based on the distance
- //A wall, 1 tile directly in front of the player would fit the whole screen
- var wallHeight = height / (ray.length * Math.cos(deltaAngle));
- if (walls[ray.id]) { // We got an image for this wall type
- var wall = walls[ray.id];
- //We draw a pixel collum from the image
- ctx.drawImage(wall(ray.offset*wall.width)|0,0,1,wall.height,i,(height-wallHeight)*0.5,1,wallHeight)
- } else {
- ctx.fillStyle = "white"; //We draw it white;
- ctx.fillRect(i,(height-wallHeight)*0.5,1,wallHeight)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement