Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[rew] Dynamic lighting?

Tim.

Member

Hey, I was on here a few months ago and a friend of mine sent me his game. In it, you could walk by lamps or lights and the player sprite would have a shadow, along with alot of other things. Sadly however, I haven't found it on here. (Yes I looked through all the resource forums) So, if anybody could post it or link me to where it is, that would be great.
 
..it works fine for me but i'll just post the code here.

Code:
#==============================================================================
# â–  Sprite_Shadow (Sprite_Ombre )
# Based on Genzai Kawakami's shadows, dynamisme&features by Rataime, extra features Boushy
#==============================================================================

CATERPILLAR_COMPATIBLE = true
SHADOW_WARN = true

class Game_Party
  attr_reader :characters
end

class Sprite_Shadow < RPG::Sprite

  attr_accessor :character
 
  def initialize(viewport, character = nil,id=0)
    super(viewport)
    params=$shadow_spriteset.shadows[id]
    @source=params[0]
    @anglemin=0
    @anglemin=params[1] if params.size>1
    @anglemax=0
    @anglemax=params[2] if params.size>2
    @self_opacity=100
    @self_opacity=params[4] if params.size>4
    @distancemax=350
    @distancemax=params[3] if params.size>3
    @character = character
    update
  end
 
  def update

    if !in_range?(@character, @source, @distancemax)
      self.opacity=0
      return
    end
    super
    
    if @tile_id != @character.tile_id or
       @character_name != @character.character_name or
       @character_hue != @character.character_hue
      @tile_id = @character.tile_id
      @character_name = @character.character_name
      @character_hue = @character.character_hue
      if @tile_id >= 384
        self.bitmap = RPG::Cache.tile($game_map.tileset_name,
          @tile_id, @character.character_hue)
        self.src_rect.set(0, 0, 32, 32)
        self.ox = 16
        self.oy = 32
      else
        self.bitmap = RPG::Cache.character(@character.character_name,
          @character.character_hue)
        @cw = bitmap.width / 4
        @ch = bitmap.height / 4
        self.ox = @cw / 2
        self.oy = @ch
      end
    end
    self.visible = (not @character.transparent)
    if @tile_id == 0
      sx = @character.pattern * @cw
      sy = (@character.direction - 2) / 2 * @ch
      if self.angle>90 or angle<-90
        if @character.direction== 6
               sy = ( 4- 2) / 2 * @ch
        end
        if @character.direction== 4
               sy = ( 6- 2) / 2 * @ch
        end
        if @character.direction== 2
               sy = ( 8- 2) / 2 * @ch
        end
        if @character.direction== 8
               sy = ( 2- 2) / 2 * @ch
        end
      end
      self.src_rect.set(sx, sy, @cw, @ch)
    end
    self.x = @character.screen_x
    self.y = @character.screen_y-5
    self.z = @character.screen_z(@ch)-1
    self.blend_type = @character.blend_type
    self.bush_depth = @character.bush_depth
    if @character.animation_id != 0
      animation = $data_animations[@character.animation_id]
      animation(animation, true)
      @character.animation_id = 0
    end
    @deltax=@source.screen_x-self.x
    @deltay= @source.screen_y-self.y
    self.color = Color.new(0, 0, 0)
    @distance = ((@deltax ** 2) + (@deltay ** 2))
    self.opacity = @self_opacity*13000/((@distance*370/@distancemax)+6000)
    self.angle = 57.3*Math.atan2(@deltax, @deltay )
    @angle_trigo=self.angle+90
    if @angle_trigo<0
        @angle_trigo=360+@angle_trigo
     end
    if @anglemin !=0 or @anglemax !=0
       if (@angle_trigo<@anglemin or @angle_trigo>@anglemax) and @anglemin<@anglemax
         self.opacity=0
         return
       end
       if (@angle_trigo<@anglemin and @angle_trigo>@anglemax) and @anglemin>@anglemax
         self.opacity=0
         return
       end     
     end
  end
   
  def in_range?(element, object, range)# From Near's Anti Lag Script, edited
    x = (element.screen_x - object.screen_x) * (element.screen_x - object.screen_x)
    y = (element.screen_y - object.screen_y) * (element.screen_y - object.screen_y)
    r = x + y
    if r <= (range * range)
       return true
    else
      return false
    end
  end
  
end

#===================================================
# â–¼ CLASS Sprite_Character edit
#===================================================

class Sprite_Character < RPG::Sprite
  alias shadow_initialize initialize
  
  def initialize(viewport, character = nil)
    @character = character
    super(viewport)
    @ombrelist=[]
    if character.is_a?(Game_Event) and $shadow_spriteset.shadows!=[]
      params = XPML_read("Shadow",@character.id,4)
      if params!=nil
        for i in 0...$shadow_spriteset.shadows.size
          @ombrelist.push(Sprite_Shadow.new(viewport, @character,i))
        end
      end
    end
    if character.is_a?(Game_Player) and $shadow_spriteset.shadows!=[]
      for i in 0...$shadow_spriteset.shadows.size
        @ombrelist.push(Sprite_Shadow.new(viewport, $game_player,i))
      end
      #===================================================
      # ● Compatibility with fukuyama's caterpillar script
      #===================================================
      if CATERPILLAR_COMPATIBLE and $game_party.characters!=nil
        for member in $game_party.characters
          for i in 0...$shadow_spriteset.shadows.size
            @ombrelist.push(Sprite_Shadow.new(viewport, member,i))
          end
        end
      end
      #===================================================
      # ● End of the compatibility
      #===================================================
    end
    shadow_initialize(viewport, @character)
  end
  
  alias shadow_update update
  
  def update
    shadow_update
    if @ombrelist!=[]
      for i in 0...@ombrelist.size
        @ombrelist[i].update
      end
    end
  end  
  
end

#===================================================
# â–¼ CLASS Game_Event edit
#===================================================
class Game_Event
  attr_accessor :id
end

#===================================================
# â–¼ CLASS Spriteset_Map edit
#===================================================
class Spriteset_Map
  attr_accessor :shadows
  alias shadow_initialize initialize
  
  def initialize
    $shadow_spriteset=self
    @shadows=[]
    warn=false
    for k in $game_map.events.keys.sort
      warn=true if ($game_map.events[k].list!=nil and $game_map.events[k].list[0].code == 108 and ($game_map.events[k].list[0].parameters == ["s"] or $game_map.events[k].list[0].parameters == ["o"]))
      params = XPML_read("Shadow Source",k,4)
      $shadow_spriteset.shadows.push([$game_map.events[k]]+params) if params!=nil
    end
    p "Warning : At least one event on this map uses the obsolete way to add shadows" if warn == true and SHADOW_WARN
    shadow_initialize
  end  
end



#===================================================
# â–¼ XPML Definition, by Rataime, using ideas from Near Fantastica
#
#   Returns nil if the markup wasn't present at all,
#   returns [] if there wasn't any parameters, else
#   returns a parameters list with "int" converted as int
#   eg :
#   begin first
#   begin second
#   param1 1
#   param2 two
#   begin third
#   anything 3
#
#   p XPML_read("first", event_id) -> []
#   p XPML_read("second", event_id) -> [1,"two"]
#   p XPML_read("third", event_id) -> [3]
#   p XPML_read("forth", event_id) -> nil
#===================================================

  def XPML_read(markup,event_id,max_param_number=0)
    parameter_list = nil
    event=$game_map.events[event_id]
    return if event.list==nil
      for i in 0...event.list.size
        if event.list[i].code == 108 and event.list[i].parameters[0].downcase == "begin "+markup.downcase
          parameter_list = [] if parameter_list == nil
          for j in i+1...event.list.size
            if event.list[j].code == 108
              parts = event.list[j].parameters[0].split
              if parts.size!=1 and parts[0].downcase!="begin"
                if parts[1].to_i!=0 or parts[1]=="0"
                  parameter_list.push(parts[1].to_i)
                else
                  parameter_list.push(parts[1])
                end
              else
                return parameter_list
              end
            else
              return parameter_list
            end
            return parameter_list if max_param_number!=0 and j==i+max_param_number
          end
        end
      end
    return parameter_list
  end

use comments that say 'Begin Shadow Source' or 'Begin Shadow'
add comments below those to configure the effects such as:
'anglemin 260'
'anglemax 90'
'distancemax 300'
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top