Haden2 / Spectrum

A horror game under development
0 stars 0 forks source link

Light Monster effect #63

Open Haden2 opened 9 years ago

Haden2 commented 9 years ago

Ok, so the light monster is probably the most complex enemy that I have to code. I am not dumping this on you by any means. Since this enemy is such a big project, I figure it would be best if we start out tackling it together. I've worked on it throughout the night (I had to work 3rd shift this last weekend so my sleep schedule is all out of wack at the moment) and what I have so far is the following:

  1. The light monster, player, and a spotlight have been isolated and moved above the map so they are away from other lights. This has been my testing area for her.
  2. The light monster has working code that finds all of the lights in the game and figures out which one is the closest one.
  3. That closest light is the spotlight that I have been working with. It spins around so we can test the transitioning between fade and solid.
  4. The light has 3 rays on it. One for the middle of the light, and 2 on the sides which start the light The thing that is needed is to find a way to figure out the distance from the side of the circle to the center.
  5. The lights angle and distance from player

The light monsters functionality is that she is only visible in the dark and light makes her disappear. https://www.youtube.com/watch?v=-fDzdDfviLI The challenge here is that there are multiple kinds of lights, and multiple variations like range, intensity, and angle.

The thing that we need is to figure out how to calculate the distance from the side of the circle to the middle of the circle. I believe this can be done with trig. For example, I know that the spotlight has an angle of 40 from the source. This means that from one of the edges to the middle, the angle is 20. The adjacent leg is 22, which is the range of the light. However, the light passes through the wall in order to get the brightness of the light to be visible. So the adjacent leg is 22, but to the player it is 9. This leaves the Hypotenuse and Opposite side unknown. I calculated it out and the far edge where the light actually ends is: Opposite: 8 Hypotenuse: 23.41

From the player it is: Opposite: 3.2757 Hypotenuse: 9.5775

There needs to be a formula in place that can calculate for this each time. Knowing that the distance between the enemy and the edge of the right circle gives us one of our factors for calculating the rate of fade.

With that, I came up with a calculation that can figure out the rate of fade. For example, lets say that the light was dim, and 5 away. From 5 away, the alpha (transparency factor) would be 100%. With a decrease rate of 20% per unit away. So at 4 away, it would have an 80% alpha. DistanceFromTarget * RateOfDim = Alpha %

If the light was bright, we could change the rate at which it fades. DistanceFromTarget/2 * RateOfBright = Alpha %

That gives us the formula for adjusting transparency based on how close the monster is to the light. So this would be applied to all lights, including spot and point. The issue that happens with the spotlight is that there is the source of the light, and then there is the actual light spot that we can see. That is why it requires additional coding. The point light is just a big ball that has a source in the middle. For that one, I can just put a collier on the ball so that when the monster walks in, I start the fading there, and then just base it on the distance from the source and the brightness.

renderer.material.color.a = changing the alpha and transparency. 1 is solid, 0 is transparent.

rateOfDim = 20; rateOfBright = 40;

if(light.intensity <5) { dim = true } if(light.intensity >5) { bright = true } If(dim) { distanceFromTarget * rateOfDim = alpha% ///// 5ft * 20 %/distance = 100% alpha } if(bright) { (distanceFromTarget/2) * rateOfBright = alpha% }