commit 773fc528fa5c5458f4baf6c58aaa1bbacc9b151d
parent 76fb326eacb4eba86e381939b5d8609d4522ce26
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Fri, 18 Sep 2020 18:01:20 -0500
Board overlay now being drawn, changed groups to LayeredDirty
Diffstat:
5 files changed, 70 insertions(+), 10 deletions(-)
diff --git a/data/img/board_overlays_1.png b/data/img/board_overlays_1.png
Binary files differ.
diff --git a/data/json/sheets.json b/data/json/sheets.json
@@ -8,5 +8,10 @@
"filename" : "cursor1.png",
"dimensions" : [64, 64],
"total_sprites" : 4
+ },
+ "board_overlays_1" : {
+ "filename" : "board_overlays_1.png",
+ "dimensions" : [64, 64],
+ "total_sprites" : 4
}
}
diff --git a/data/map/tsx/testtiles1.tsx b/data/map/tsx/testtiles1.tsx
@@ -1,4 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.4" tiledversion="1.4.1" name="testtiles1" tilewidth="64" tileheight="64" tilecount="4" columns="2">
<image source="../../img/testtiles1.png" trans="ff00ff" width="128" height="128"/>
+ <tile id="0">
+ <properties>
+ <property name="Passable" type="int" value="1"/>
+ </properties>
+ </tile>
+ <tile id="1">
+ <properties>
+ <property name="Passable" type="int" value="1"/>
+ </properties>
+ </tile>
+ <tile id="2">
+ <properties>
+ <property name="Passable" type="int" value="1"/>
+ </properties>
+ </tile>
+ <tile id="3">
+ <properties>
+ <property name="Passable" type="int" value="0"/>
+ </properties>
+ </tile>
</tileset>
diff --git a/src/board.py b/src/board.py
@@ -1,5 +1,5 @@
import pygame, pytmx, os
-from . import manager
+from . import manager, vgo
from .constants import BOARD_PATH
############
@@ -30,6 +30,7 @@ class BoardManager(manager.Manager):
# Board values
self.current_board = None
self.loaded_boards = {}
+ self.board_overlay = pygame.sprite.LayeredDirty()
def load_board_from_file(self, boardfile):
"""
@@ -37,12 +38,27 @@ class BoardManager(manager.Manager):
"""
self.loaded_boards[boardfile] = Board(self, boardfile)
+ def load_overlay(self):
+ """
+ Derive an overlay from available loaded board. Called in
+ init and should not be referenced directly.
+ """
+ self.board_overlay = pygame.sprite.LayeredDirty()
+ for layer in self.current_board.tmx_data.visible_layers:
+ if isinstance(layer, pytmx.TiledTileLayer):
+ for x, y, gid in layer:
+ if self.current_board.tmx_data.get_tile_properties_by_gid(gid)["Passable"] == 1:
+ v = vgo.VisibleGameObject(self.game.sheet_manager.loaded_sheets["board_overlays_1"])
+ v.set_position((x * self.current_board.tmx_data.tilewidth, y * self.current_board.tmx_data.tileheight))
+ self.board_overlay.add(v)
+
def switch_to_board(self, boardname):
"""
Switch to a given loaded board.
"""
if boardname in self.loaded_boards.keys():
self.current_board = self.loaded_boards[boardname]
+ self.load_overlay()
def get_tile_at_position(self, pos):
"""
@@ -76,6 +92,7 @@ class BoardManager(manager.Manager):
"""
if surface != None:
self.current_board.draw_board(surface)
+ self.board_overlay.update(surface)
############################
# Section 2 - Board Object #
@@ -110,3 +127,4 @@ class Board(object):
t = self.tmx_data.get_tile_image_by_gid(gid)
if t:
surface.blit(t, (x * self.tmx_data.tilewidth, y * self.tmx_data.tileheight))
+
diff --git a/src/vgo.py b/src/vgo.py
@@ -19,14 +19,14 @@ from .constants import ENTITY_JSON_PATH, TILE_WIDTH, TILE_HEIGHT
# Section 1 - Visible Game Object #
###################################
-class VisibleGameObject(pygame.sprite.Sprite):
+class VisibleGameObject(pygame.sprite.DirtySprite):
"""
The parent of all visible objects. VisibleGameObject (VGO) is
essentially an extended, customised version of the PyGame Sprite
object.
"""
- def __init__(self, sheet, visible = True, animation = None, animated = False):
+ def __init__(self, sheet, animation = None, animated = False):
# Parent initialization
super().__init__()
@@ -36,7 +36,11 @@ class VisibleGameObject(pygame.sprite.Sprite):
self.image = sheet.sprites[(0, 0)] # TODO: Should this be assigned??
self.rect = self.image.get_rect()
self.rect.topleft = (0, 0)
- self.visible = visible
+
+ # DirtySprite class defaults
+ self.visible = 1
+ self.dirty = 0
+ self.layer = 0
# Animation values
self.animated = False
@@ -46,6 +50,16 @@ class VisibleGameObject(pygame.sprite.Sprite):
if animated:
self.set_animation(animation, animated)
+ def set_sprite(self, sprite_coord):
+ """
+ Set the VGO's sprite to another one on the board. The
+ argument 'sprite_coord' is a tuple (x, y). This is used
+ to assign an image from the saved sheet to non-animated
+ VGO objects.
+ """
+ # TODO: Error-checking
+ self.image = self.sheet.sprites[sprite_coord]
+
def set_animation(self, new_animation, play = False, init_frame = 0):
"""
Assign a new animation to this VGO and configure all
@@ -114,10 +128,10 @@ class Entity(VisibleGameObject):
their statistics in gameplay.
"""
- def __init__(self, name, ent_id, sheet, visible = True, animation = None, animated = False, passable = False, unit = None):
+ def __init__(self, name, ent_id, sheet, animation = None, animated = False, passable = False, unit = None):
# Parent initialization
- super().__init__(sheet, visible, animation, animated)
+ super().__init__(sheet, animation, animated)
# Saved values
self.name = name
@@ -154,10 +168,13 @@ class TileCursor(Entity):
tiles.
"""
- def __init__(self, name, ent_id, sheet, visible = True, animation = None, animated = False, passable = True, unit = None):
+ def __init__(self, name, ent_id, sheet, animation = None, animated = False, passable = True, unit = None):
# Parent initialization
- super().__init__(name, ent_id, sheet, visible, animation, animated, passable, unit)
+ super().__init__(name, ent_id, sheet, animation, animated, passable, unit)
+
+ # DirtySprite settings
+ self.layer = 1
##############################################
# Section 4 - Managers for VGOs & Subclasses #
@@ -175,7 +192,7 @@ class EntityManager(manager.Manager):
super().__init__(game)
# Entity values
- self.loaded_entities = pygame.sprite.Group()
+ self.loaded_entities = pygame.sprite.LayeredDirty()
self.tile_cursor = None
self.total_entities = 0 # total number of unique entities loaded
@@ -196,7 +213,7 @@ class EntityManager(manager.Manager):
for e in j:
s = self.game.unit_manager.get_stats(j[e]["name"])
ne = Entity(j[e]["name"], self.total_entities,
- self.game.sheet_manager.loaded_sheets[j[e]["sheet"]], j[e]["visible"],
+ self.game.sheet_manager.loaded_sheets[j[e]["sheet"]],
self.game.sheet_manager.animations[j[e]["sheet"]][j[e]["animation"]],
j[e]["animated"], j[e]["passable"], unit.Unit(self, s))
ne.assign_tile(self.game.board_manager.get_tile_at_tile_pos(tuple(j[e]["tile"])))