Robot Foot Games Helping you improve your games as we improve our own. :)

30May/11Off

Adding a field of view to enemies

Here's a video showing off the final product: Please note, the texture is being drawn just to show how/when/where his field of view is. You'd probably take the code out for drawing the spotlight rectangle unless it's part of your design!

spotlight2

 

For the image above, Right Click>Save As and save it to “HighResolutionContent>Overlays” folder (or wherever you see fit, just remember the location for the LoadContent call)

Remember to load it into the project solution by navigating to “HighResolutionContent>Overlays” and right click on the Overlays folder and Add>ExistingItem>spotlight2.png.
All these changes are done in Enemy.cs

Add these underneath after public Rectangle BoundingRectangle{...}

Next add the following line in LoadContent method at the bottom:

 

Now in the Update method add the following at the top:

In the Draw method add the following to the bottom:

 

Now I’m sure you’ve noticed a few flaws, like

Hey, he can see me through walls! or

Hey, his field of view is a rectangle…

At this point you might want to add clipping (so obstacles block the enemy’s field of view) or change the field of view to something more realistic. The solution to these problems will involve using some math skills you were saving for something like this! I found this to be the quickest solution to test a field of view for enemies. Eventually I’ll make it more complex but this works fine for the near future.

 

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

No trackbacks yet.