Here's the first part of my line of sight implementation.
I say tutorial, but it's basically me writing it as I go.
First, I want to be able to visualize my line of sight. Tilemap natively supports tile flashing, so I make use of that.
This is the code I use to draw my highlights.
It is updated every frame.
And here's some code that shows the player's line of sight
It currently does not implement obstacle checking, but that's the next step after being able to visualize it.
It also assumes 4-dir line of sight.
Feel free to point out anything that can be improved.
I say tutorial, but it's basically me writing it as I go.
First, I want to be able to visualize my line of sight. Tilemap natively supports tile flashing, so I make use of that.
This is the code I use to draw my highlights.
It is updated every frame.
class Scene_Map def update_highlight(tiles) @spriteset.reserve_highlights(tiles) if @spriteset endendclass Spriteset_Map alias :th_event_trigger_range_create_tilemap :create_tilemap def create_tilemap th_event_trigger_range_create_tilemap @reserved_tiles = [] clear_highlights end alias :th_trigger_range_highlight_update_tilemap :update_tilemap def update_tilemap th_trigger_range_highlight_update_tilemap update_highlights end def update_highlights clear_highlights @reserved_tiles.each do |pos| @tilemap.flash_data[pos[0], pos[1]] = 0x8f2 end @reserved_tiles.clear end def reserve_highlights(tiles) @reserved_tiles.concat(tiles) end def clear_highlights @tilemap.flash_data = Table.new($game_map.width, $game_map.height) endend
class Game_Player @@sight = 12 alias :th_los_update :update def update th_los_update update_line_of_sight end def update_line_of_sight arr = [] @@sight.times {|i| arr << [@x + normalize_dir_x * i, @y + normalize_dir_y * i] } SceneManager.scene.update_highlight(arr) end def normalize_dir_x case @direction when 4 return -1 when 6 return 1 else return 0 end end def normalize_dir_y case @direction when 2 return 1 when 8 return -1 else return 0 end endend
It also assumes 4-dir line of sight.
Feel free to point out anything that can be improved.