Skip to content

Limited vision #22

Description

@ericlippert

Let's go back to display logic. The game is too easy because the player can see the entire level and the location of every monster and item. When copying the map to the display, only copy map coordinates and entities to the display that are in this disk of the player's location:

    ...  
   ..... 
  .......
  ...@... 
  .......
   ..... 
    ...  

The way we'll do this seems a bit weird but it will make sense later. Make a method that takes (1) a map grid (2) the x,y position of the player and (3) a radius. It returns a grid. The returned grid is the same size as the map grid, and is false everywhere that is not visible, and true everywhere that is visible -- so for now, the task is just to make a disk of true around a particular point.

Then when you copy the map to the display, only do the copy if the corresponding Boolean is true.

This system for vision has two problems: (1) it's reasonable for the game to display stuff the player can't see if they've seen it before, and (2) you can see around corners and through walls, which is not very realistic. We'll fix these problems in later tasks.

Tip:

  • A cell (a, b) is within radius r of (x, y) if (a-x)*(a-x) + (b-y)*(b-y) <= r*r, but it is actually better to use < instead of <=. That's because if you use <= you end up with weird "points" in the lighting pattern. For example, here's radius 6 using <=:
       .      
    .......
   .........
  ...........
  ...........
  ...........
 ......@......
  ...........
  ...........
  ...........
   .........
    .......
       .      

But using < you skip the pointy bits:

             
    .......
   .........
  ...........
  ...........
  ...........
  .....@.....
  ...........
  ...........
  ...........
   .........
    .......
       

which looks better.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions