Skip to main content
added 41 characters in body
Source Link
Mangata
  • 2.8k
  • 1
  • 4
  • 10

Here is a suggestion:

Manage a unit number matrix like map_unit_num_matrix, The number of units on each tile is recorded. And manage a dirty mark list recording the tiles that change the number of units each frame.

Fake code for example:

#--- unit.py
class Unit:
    def __init__(self) -> None:
        pass  // your unit variables
        self.tilePos = [0,0]

    def move(self):
        pass  // your unit moving logic
        self.onMoving()

    def onMoving():
        newTilePos = self.get_coord()
        if newTilePos[0] == self.tilePos[0] and newTilePos[1] == self.tilePos[1]:
            return
        GameMapManager.instance.unit_move(self.tilePos,newTilePos) //name is just for example
        self.tilePos = newTilePos
        
#--- map.py
class GameMapManager: //it may not exist
    def __init__(self) -> None:
        self.map_vision_matrix = []
        self.map_explored_matrix = []
        self.map_unit_num_matrix = []
        self.map_dirty_mark_list = []

    def unit_move(self,tileFrom,tileTo):
        self.map_unit_num_matrix[tileFrom[0]][tileFrom[1]] -= 1
        self.map_unit_num_matrix[tileTo[0]][tileTo[1]] += 1
        self.map_dirty_mark_list.append(tileFrom)
        self.map_dirty_mark_list.append(tileTo)

    def update_vision(self):
        if not self.map_dirty_mark_list:
            return
        for tilePos in self.map_dirty_mark_list:
            hasUnit = self.map_unit_num_matrix[tilePos[0]][tilePos[1]] > 0
            pass // your vision/fog of war logic
        self.map_dirty_mark_list = []

If the units have different field of view, you can record a field of view list or a unit list rather than unit number in the matrix.

Here is a suggestion:

Manage a unit number matrix like map_unit_num_matrix, The number of units on each tile is recorded. And manage a dirty mark list recording the tiles that change the number of units each frame.

Fake code for example:

#--- unit.py
class Unit:
    def __init__(self) -> None:
        pass  // your unit variables
        self.tilePos = [0,0]

    def move(self):
        pass  // your unit moving logic
        self.onMoving()

    def onMoving():
        newTilePos = self.get_coord()
        if newTilePos[0] == self.tilePos[0] and newTilePos[1] == self.tilePos[1]:
            return
        GameMapManager.instance.unit_move(self.tilePos,newTilePos) //name is just for example
        self.tilePos = newTilePos
        
#--- map.py
class GameMapManager: //it may not exist
    def __init__(self) -> None:
        self.map_vision_matrix = []
        self.map_explored_matrix = []
        self.map_unit_num_matrix = []
        self.map_dirty_mark_list = []

    def unit_move(self,tileFrom,tileTo):
        self.map_unit_num_matrix[tileFrom[0]][tileFrom[1]] -= 1
        self.map_unit_num_matrix[tileTo[0]][tileTo[1]] += 1
        self.map_dirty_mark_list.append(tileFrom)
        self.map_dirty_mark_list.append(tileTo)

    def update_vision(self):
        for tilePos in self.map_dirty_mark_list:
            hasUnit = self.map_unit_num_matrix[tilePos[0]][tilePos[1]] > 0
            pass // your vision/fog of war logic

If the units have different field of view, you can record a field of view list or a unit list rather than unit number in the matrix.

Here is a suggestion:

Manage a unit number matrix like map_unit_num_matrix, The number of units on each tile is recorded. And manage a dirty mark list recording the tiles that change the number of units each frame.

Fake code for example:

#--- unit.py
class Unit:
    def __init__(self) -> None:
        pass  // your unit variables
        self.tilePos = [0,0]

    def move(self):
        pass  // your unit moving logic
        self.onMoving()

    def onMoving():
        newTilePos = self.get_coord()
        if newTilePos[0] == self.tilePos[0] and newTilePos[1] == self.tilePos[1]:
            return
        GameMapManager.instance.unit_move(self.tilePos,newTilePos) //name is just for example
        self.tilePos = newTilePos
        
#--- map.py
class GameMapManager: //it may not exist
    def __init__(self) -> None:
        self.map_vision_matrix = []
        self.map_explored_matrix = []
        self.map_unit_num_matrix = []
        self.map_dirty_mark_list = []

    def unit_move(self,tileFrom,tileTo):
        self.map_unit_num_matrix[tileFrom[0]][tileFrom[1]] -= 1
        self.map_unit_num_matrix[tileTo[0]][tileTo[1]] += 1
        self.map_dirty_mark_list.append(tileFrom)
        self.map_dirty_mark_list.append(tileTo)

    def update_vision(self):
        if not self.map_dirty_mark_list:
            return
        for tilePos in self.map_dirty_mark_list:
            hasUnit = self.map_unit_num_matrix[tilePos[0]][tilePos[1]] > 0
            pass // your vision/fog of war logic
        self.map_dirty_mark_list = []

If the units have different field of view, you can record a field of view list or a unit list rather than unit number in the matrix.

Source Link
Mangata
  • 2.8k
  • 1
  • 4
  • 10

Here is a suggestion:

Manage a unit number matrix like map_unit_num_matrix, The number of units on each tile is recorded. And manage a dirty mark list recording the tiles that change the number of units each frame.

Fake code for example:

#--- unit.py
class Unit:
    def __init__(self) -> None:
        pass  // your unit variables
        self.tilePos = [0,0]

    def move(self):
        pass  // your unit moving logic
        self.onMoving()

    def onMoving():
        newTilePos = self.get_coord()
        if newTilePos[0] == self.tilePos[0] and newTilePos[1] == self.tilePos[1]:
            return
        GameMapManager.instance.unit_move(self.tilePos,newTilePos) //name is just for example
        self.tilePos = newTilePos
        
#--- map.py
class GameMapManager: //it may not exist
    def __init__(self) -> None:
        self.map_vision_matrix = []
        self.map_explored_matrix = []
        self.map_unit_num_matrix = []
        self.map_dirty_mark_list = []

    def unit_move(self,tileFrom,tileTo):
        self.map_unit_num_matrix[tileFrom[0]][tileFrom[1]] -= 1
        self.map_unit_num_matrix[tileTo[0]][tileTo[1]] += 1
        self.map_dirty_mark_list.append(tileFrom)
        self.map_dirty_mark_list.append(tileTo)

    def update_vision(self):
        for tilePos in self.map_dirty_mark_list:
            hasUnit = self.map_unit_num_matrix[tilePos[0]][tilePos[1]] > 0
            pass // your vision/fog of war logic

If the units have different field of view, you can record a field of view list or a unit list rather than unit number in the matrix.