Separating game/scene logic

This commit is contained in:
duckduckdoof 2025-02-05 15:33:07 -05:00
parent ebb1460e25
commit 455f392965
9 changed files with 334 additions and 225 deletions

View file

@ -5,7 +5,7 @@ author: Caleb Scott
Configuration file for modifying the visual scene of the game.
This is not the same as engine/game_config.py, which is used to adjust
game objects (apart from their visuals, like sprites)
game objects (apart from their visuals)
"""
# CONSTANTS ---------------------------------------------------------
@ -18,7 +18,7 @@ SCREEN_WIDTH = 960
SCREEN_HEIGHT = 960
SCREEN_TITLE = "Miniopolis Demo"
# World size
# World size in pixels
WORLD_WIDTH = 960
WORLD_HEIGHT = 640
@ -41,30 +41,30 @@ JUNCTION_RES = RES + "connector.png"
MAPS = "maps/"
TEST_MAP = MAPS + "default-map.json"
# Tile property which gets its name as string
TILE_NAME = 'class'
# Layers for Map
LAYER_STRUCTURES = "Structures"
LAYER_FOUNDATIONS = "Foundations"
LAYER_ENVIRONMENT = "Environment"
# Tile Types
LOGGER = "LoggerTile"
CROPS = "CropsTile"
HYDROPOWER = "HydroPowerTile"
HOUSING = "HousingTile"
MINER = "MinerTile"
FACTORY = "FactoryTile"
JUNCTION = "JunctionTile"
# Layer ordering (bottom to top)
LAYERS = [
LAYER_ENVIRONMENT,
LAYER_FOUNDATIONS,
LAYER_STRUCTURES
]
FOUNDATION = "FoundationTile"
GROUND = "GroundTile"
WATER = "WaterTile"
METAL = "MetalTile"
TREES = "TreesTile"
# Game Starting Resources
STARTING_RESOURCES = {
"metal": 50,
"wood": 100,
"people": 10,
"food": 200
# Layer Options (for spatial hashing)
LAYER_OPTIONS = {
LAYER_ENVIRONMENT: {
"use_spatial_hash": True
},
LAYER_FOUNDATIONS: {
"use_spatial_hash": True
},
LAYER_STRUCTURES: {
"use_spatial_hash": True
}
}

36
lib/scene/scene_logic.py Normal file
View file

@ -0,0 +1,36 @@
"""
scene_logic.py
author: Caleb Scott
Logic for interpreting the visual elements of the game scene.
This includes importing tilesets and passing them to the game logic module,
interpreting player inputs on the screen, etc.
"""
# IMPORTS -----------------------------------------------------------
from arcade import Scene
from scene_config import *
from global_config import *
# FUNCTIONS ---------------------------------------------------------
def info_from_layered_tilemap(scene: Scene):
"""
Takes tilemap from arcade scene, and initializes arrays of
tile information from the tile objects.
This can then be passed to the game engine to convert these into
game objects
"""
scene_info = {}
for layer in LAYERS:
# Initialize empty game board (2D array)
scene_info[layer] = [[""] * BOARD_HEIGHT for _ in range(BOARD_WIDTH)]
# Populate the layer info as 2D arrays
for i, S in enumerate(scene[layer]):
scene_info[layer][i//BOARD_WIDTH][i%BOARD_HEIGHT] = scene[layer].properties[TILE_NAME]
return scene_info

View file

@ -1,91 +0,0 @@
"""
scene_tiles.py
author: Caleb Scott
Contains all classes of tiles used in miniopolis
"""
# IMPORTS -----------------------------------------------------------
import arcade
from lib.scene.scene_config import *
# CLASSES -----------------------------------------------------------
class HousingTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = HOUSING_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = HOUSING
self.center_x = x
self.center_y = y
class LoggerTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = LOGGER_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = LOGGER
self.center_x = x
self.center_y = y
class CropsTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = CROPS_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = CROPS
self.center_x = x
self.center_y = y
class HyroPowerTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = HYDRO_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = HYDROPOWER
self.center_x = x
self.center_y = y
class MinerTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = MINING_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = MINER
self.center_x = x
self.center_y = y
class FactoryTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = FACTORY_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = FACTORY
self.center_x = x
self.center_y = y
class JunctionTile(arcade.Sprite):
def __init__(self, x, y):
self.image_file_name = JUNCTION_RES
super().__init__(self.image_file_name, TILE_SCALE)
# Set custom properties
self.properties["type"] = JUNCTION
self.center_x = x
self.center_y = y