func_fade.qc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) 1996-2022 id Software LLC
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. See file, 'COPYING', for details.
  14. */
  15. const float FUNC_FADE_STATE_HIDDEN = 0;
  16. const float FUNC_FADE_STATE_VISIBLE = 1;
  17. const float FUNC_FADE_DISTANCE_MAXSPEED = 320;
  18. float func_fade_get_shortest_playerdistance()
  19. {
  20. float dist = 32767;
  21. float d;
  22. vector vpos;
  23. entity pl = find (world, classname, "player");
  24. while (pl != world)
  25. {
  26. vpos = pl.origin + pl.view_ofs;
  27. d = vlen(self.oldorigin - vpos);
  28. if(d < dist) dist = d;
  29. pl = find (pl, classname, "player");
  30. }
  31. return dist;
  32. }
  33. void func_fade_set_nextthink_for_distance(float dist)
  34. {
  35. self.nextthink = self.ltime + Clamp(dist / FUNC_FADE_DISTANCE_MAXSPEED, 0.05, 2.0);
  36. }
  37. void func_fade_set_alpha(float a, float force=FALSE)
  38. {
  39. a = Clamp(a, 0, 1);
  40. if( !force && fabs(a - self.alpha) < 0.001) return;
  41. if (self.state == FUNC_FADE_STATE_HIDDEN && a > 0)
  42. {
  43. self.state = FUNC_FADE_STATE_VISIBLE;
  44. self.model = self.mdl;
  45. }
  46. else if (self.state == FUNC_FADE_STATE_VISIBLE && a == 0)
  47. {
  48. self.state = FUNC_FADE_STATE_HIDDEN;
  49. self.model = string_null;
  50. }
  51. self.alpha = a;
  52. }
  53. void func_fade_think()
  54. {
  55. float dist = func_fade_get_shortest_playerdistance();
  56. float diff = dist - self.distance;
  57. if (diff < 0)
  58. {
  59. //Outside the transition zone, inside the circle
  60. func_fade_set_alpha(1);
  61. dist = -diff;
  62. func_fade_set_nextthink_for_distance(dist);
  63. }
  64. else if(diff > self.lip)
  65. {
  66. //Outside the transition zone, outside the circle
  67. func_fade_set_alpha(0);
  68. dist = diff - self.lip;
  69. func_fade_set_nextthink_for_distance(dist);
  70. }
  71. else
  72. {
  73. //In the transition zone
  74. diff/= self.lip;
  75. func_fade_set_alpha(1 - diff);
  76. self.nextthink = self.ltime + 0.05;
  77. }
  78. }
  79. void func_fade()
  80. {
  81. if(TRUE)
  82. {
  83. dprint("WARNING: A func_fade was spawned. This entity is obsolete, and was converted to a func_wall instead.\n")
  84. func_wall();
  85. return;
  86. }
  87. if(!self.distance) self.distance = 2048; //Radius of fade circle
  88. if(!self.lip) self.lip = 256; // Lip around fade circle
  89. self.waitmax = self.distance + self.lip;
  90. self.solid = SOLID_BSP;
  91. self.movetype = MOVETYPE_PUSH;
  92. setmodel(self, self.model);
  93. self.mdl = self.model;
  94. self.oldorigin = self.mins + ((self.maxs - self.mins) * 0.5);
  95. self.state = FUNC_FADE_STATE_VISIBLE;
  96. self.think = func_fade_think;
  97. self.nextthink = self.ltime + 0.2 + random() * 0.2;
  98. func_fade_set_alpha(0, TRUE);
  99. }