- Home /
Change Lighting of grid base on distance from player.
Hello !
I want my grid is darker when far from player, lighter when nearby player, Unvisiable (dark) when not discovered yet (Or there is line of tree block player view (like a tree inside a jungle)). Like this :
Can you help me this problemt ? I dont know how to change the light of grid, detect if there is a line of tree.
Thanks for advice !
Answer by toddisarockstar · Sep 17, 2019 at 05:19 AM
you can calculate squares in a circular pattern using the distance formula.
this would print out all the squares around a given coordinate at a given distance:
void GetSquaresNearPoint (int x, int y, int dist){
int xx = dist*2;
while(xx>0){xx--;
int yy = dist*2;
int xd = xx - dist;
while(yy>0){yy--;
int yd = yy - dist;
if((xd*xd)+(yd*yd)<dist*dist){
print((x+xx-dist)+","+(y+yy-dist));
}
}}
}
in a game like this there should be some system in place to hold data for each square to tell if it is able to be lit or not.
this function should be called in Update???
is there any way to improve performance because in the game like this performance is everything.
I have the same problem and still, couldn't find the best way to do that.
there would be no reason to call this every frame!!! in a situation like the above picture this function shoulnt be called more than once each time the player changes what square he is standing on.
and Yes ! There is a couple ways to improve the speed of my answer! if you are always checking a fixed distance then the circular pattern could be stored ahead of time into an array of coordinates. Then when you light the spaces, you could simply page through this array and add the players position. this would avoid the distance calculations.
also anytime you can get by with using a square pattern ins$$anonymous$$d of a circular pattern for whatever you are doing, You can remove the distance calculations and it's much faster.
Thanks.
$$anonymous$$y Game doesn't exactly like this.
It's something like 'DOTA2' and I'm trying to have the best performance so if you have any idea please help me.
Please please please change
if($$anonymous$$athf.Sqrt((xd*xd)+(yd*yd))<dist){
at least to
if(((xd*xd)+(yd*yd)) < dist*dist){
Sqrt is very slow and completely unnecessary here You could also move the X related stuff to the first loop, their values remain the same in the Y loop
@Panga$$anonymous$$i is correct and i have updated my answer to show his suggestion.
Also consider doing the whole thing on the GPU, I'd say this is a perfect candidate
Thanks you ! So how can i change the light of one grid ?
Your answer
Follow this Question
Related Questions
Change Lighting of grid base on distance from player. 0 Answers
Terrain shadow distance and lighting problem 0 Answers
Detect if player is in range? 2 Answers
Hexagon-Grid Distance 2 Answers