Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit ef231f44df278aeeae11cd8dd93507086e05e47e
parent 9cfbf8ab4398046a6078aa679f2648a58e7a3c06
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sat, 12 Sep 2020 21:39:33 -0500

Basic visual tile cursor functioning

Diffstat:
Adata/img/cursor1.png | 0
Mdata/json/sheets.json | 5+++++
Mdata/map/testmap1.tmx | 4++--
Mmain.py | 4++++
Msrc/board.py | 14++++++++++++++
Asrc/cursor.py | 2++
Msrc/game.py | 27+++++++++++++++++++++++++--
Msrc/vgo.py | 30++++++++++++++++++++++++++++++
8 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/data/img/cursor1.png b/data/img/cursor1.png Binary files differ. diff --git a/data/json/sheets.json b/data/json/sheets.json @@ -3,5 +3,10 @@ "filename" : "jisella_1.png", "dimensions" : [64, 64], "total_sprites" : 72 + }, + "cursor1" : { + "filename" : "cursor1.png", + "dimensions" : [64, 64], + "total_sprites" : 4 } } diff --git a/data/map/testmap1.tmx b/data/map/testmap1.tmx @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> -<map version="1.4" tiledversion="1.4.1" orientation="orthogonal" renderorder="right-down" width="18" height="18" tilewidth="64" tileheight="64" infinite="0" nextlayerid="2" nextobjectid="1"> +<map version="1.4" tiledversion="1.4.1" orientation="orthogonal" renderorder="right-down" width="18" height="18" tilewidth="64" tileheight="64" infinite="0" nextlayerid="3" nextobjectid="1"> <tileset firstgid="1" source="tsx/testtiles1.tsx"/> - <layer id="1" name="Tile Layer 1" width="18" height="18"> + <layer id="1" name="BaseTileLayer" width="18" height="18"> <data encoding="csv"> 1,1,1,1,1,1,2,2,3,4,4,4,4,4,4,4,4,4, 1,1,1,1,1,1,2,2,3,4,4,4,4,4,4,4,4,4, diff --git a/main.py b/main.py @@ -1,6 +1,10 @@ import pygame from src import game +## GLOBAL TODO: +# 1. Decide on final method for managing the cursor. Should it be managed by EntityManager? BoardManager? GameInterface? A seperate manager object? +# + pygame.init() if __name__ == "__main__": diff --git a/src/board.py b/src/board.py @@ -44,6 +44,20 @@ class BoardManager(manager.Manager): if boardname in self.loaded_boards.keys(): self.current_board = self.loaded_boards[boardname] + def get_tile_at_position(self, pos): + """ + Return (x, y, gid) if there is tile at 'pos', and return + None otherwise. + """ + w = self.current_board.tmx_data.tilewidth + h = self.current_board.tmx_data.tileheight + for layer in self.current_board.tmx_data.visible_layers: + if isinstance(layer, pytmx.TiledTileLayer): + for x, y, gid in layer: + if pos[0] >= x * w and pos[0] < (x + 1) * w and pos[1] >= y * w and pos[1] < (y + 1) * w: + return (x, y, gid) + return None + def update_board(self, surface = None): """ Update the current board. diff --git a/src/cursor.py b/src/cursor.py @@ -0,0 +1,2 @@ +import pygame + diff --git a/src/game.py b/src/game.py @@ -1,6 +1,6 @@ import pygame from . import images, board, vgo -from .constants import SCREEN_WIDTH, SCREEN_HEIGHT, FRAMERATE +from .constants import SCREEN_WIDTH, SCREEN_HEIGHT, FRAMERATE, TILE_WIDTH, TILE_HEIGHT ########### # game.py # @@ -49,6 +49,7 @@ class Game(object): self.sheet_manager.load_animations("anims.json") self.board_manager.load_board_from_file("testmap1.tmx") self.board_manager.switch_to_board("testmap1.tmx") + self.entity_manager.load_tile_cursor("cursor1") self.entity_manager.load_entities_from_json("testmap1.json") def shift_frames(self): @@ -68,6 +69,7 @@ class Game(object): # Next, update all the subsurfaces/game objects and draw them # TODO: This is WIP and will change + self.interface.update_interface() self.board_manager.update_board(self.screen) self.entity_manager.update_entities(self.screen) @@ -136,7 +138,6 @@ class GameInterface(GameSubsystem): """ Handle any kind of PyGame event and react appropriately. """ - for event in events: if event.type == pygame.KEYDOWN: self.handle_key_press(event) @@ -156,3 +157,25 @@ class GameInterface(GameSubsystem): React to a key being released. """ pass + + def handle_mouse_click(self, mousebutton): + """ + React to a mousebutton being clicked. + """ + pass + + def handle_mouse_release(self, mousebutton): + """ + React to a mousebutton being released. + """ + pass + + def update_interface(self): + """ + Update interface elements (such as the cursor) once + per frame. This is not the same as a drawing update for + e.g. an Entity, and is logic-only. + """ + # Update cursor position + t = self.game.board_manager.get_tile_at_position(pygame.mouse.get_pos()) + self.game.entity_manager.position_tile_cursor((t[0] * TILE_WIDTH, t[1] * TILE_HEIGHT)) diff --git a/src/vgo.py b/src/vgo.py @@ -116,6 +116,22 @@ class Entity(VisibleGameObject): # Section 3 - Various Entity subclasses # ######################################### +class TileCursor(Entity): + """ + Object that follows the cursor to indicate selected/highlighted + tiles. + """ + + def snap_to_tile(self): + """ + Snap the TileCursor to its current tile. + """ + self.rect.topleft = self.tile.rect.topleft + + def act(self): + if self.tile != None: + self.snap_to_tile + ############################################## # Section 4 - Managers for VGOs & Subclasses # ############################################## @@ -133,6 +149,7 @@ class EntityManager(manager.Manager): # Entity values self.loaded_entities = pygame.sprite.Group() + self.tile_cursor = None def load_entities_from_json(self, entsjson): """ @@ -146,6 +163,19 @@ class EntityManager(manager.Manager): self.game.sheet_manager.animations[j[e]["sheet"]][j[e]["animation"]], j[e]["animated"], j[e]["passable"], j[e]["tile"])) + def load_tile_cursor(self, sheet, pos = (0, 0)): + """ + Load a TileCursor object to highlight selected tiles. + """ + self.tile_cursor = TileCursor(self.game.sheet_manager.loaded_sheets[sheet], pos) + self.loaded_entities.add(self.tile_cursor) + + def position_tile_cursor(self, pos): + """ + Snap the TileCursor object's position to 'pos'. + """ + self.tile_cursor.rect.topleft = pos + def update_entities(self, surface = None): """ Update all loaded entities.