Initial Commit
This commit is contained in:
BIN
mods/LevelEditor/__pycache__/level.cpython-311.pyc
Normal file
BIN
mods/LevelEditor/__pycache__/level.cpython-311.pyc
Normal file
Binary file not shown.
BIN
mods/LevelEditor/data/image/editor/icon.png
Executable file
BIN
mods/LevelEditor/data/image/editor/icon.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
174
mods/LevelEditor/level.py
Normal file
174
mods/LevelEditor/level.py
Normal file
@@ -0,0 +1,174 @@
|
||||
#!~/.pyenv/versions/3.11.6/bin/python
|
||||
#
|
||||
# Copyright (c) 2024 Cutieguwu | Olivia Brooks
|
||||
#
|
||||
# -*- coding:utf-8 -*-
|
||||
# @Title: Levels in a game.
|
||||
# @Author: Cutieguwu | Olivia Brooks
|
||||
# @Email: owen.brooks77@gmail.com | obroo2@ocdsb.ca
|
||||
# @Description: Classes descibing different levels in a pygame game.
|
||||
#
|
||||
# @Script: level.py
|
||||
# @Date Created: 16 Apr, 2024
|
||||
# @Last Modified: 09 May, 2024
|
||||
# @Last Modified by: Cutieguwu | Olivia Brooks
|
||||
# ----------------------------------------------------------
|
||||
|
||||
from icecream import ic
|
||||
from lib.section import Section, Blocks
|
||||
from lib.scene import Scene
|
||||
|
||||
# Note: opt for importing IceCream in each library instead of using install() in case it is unavailable.
|
||||
# Taken from IceCream devs github at https://github.com/gruns/icecream
|
||||
try:
|
||||
from icecream import ic
|
||||
except ImportError: # Graceful fallback if IceCream isn't installed.
|
||||
ic = lambda *a: None if not a else (a[0] if len(a) == 1 else a) # Whatever the heck this filler function does.
|
||||
|
||||
|
||||
class Level():
|
||||
"""
|
||||
Creates and manages the level scape.
|
||||
"""
|
||||
|
||||
def __init__(self, parent):
|
||||
self.PARENT = parent
|
||||
self.WINDOW = self.PARENT.WINDOW
|
||||
self.SCALE = self.PARENT.SCALE
|
||||
self.SECTION = Section(self.PARENT)
|
||||
self.SCENE = Scene()
|
||||
|
||||
self.layout = Example() # Should be Main(), but Example for testing.
|
||||
|
||||
self.dx = 0
|
||||
self.dy = 0
|
||||
|
||||
self.get_layout_size()
|
||||
|
||||
def draw(self):
|
||||
"""
|
||||
Draws the level on the screen.
|
||||
"""
|
||||
|
||||
if self.layout.TYPE == "scene":
|
||||
self.SCENE.draw()
|
||||
elif self.layout.TYPE == "level":
|
||||
self.SECTION.draw(self.layout.LAYOUT)
|
||||
|
||||
def load_layout(self, layout:object):
|
||||
"""
|
||||
Load a new layout configuration.
|
||||
"""
|
||||
|
||||
self.layout = layout
|
||||
self.draw()
|
||||
self.get_layout_size()
|
||||
|
||||
def get_layout_size(self):
|
||||
"""
|
||||
Fetches the size of the active layout
|
||||
"""
|
||||
width = 0
|
||||
height = 0
|
||||
|
||||
for coords, block in self.layout.LAYOUT.items():
|
||||
try:
|
||||
if coords[2] > width:
|
||||
width = coords[2]
|
||||
|
||||
if coords[3] > height:
|
||||
height = coords[3]
|
||||
|
||||
except IndexError:
|
||||
if coords[0] > width:
|
||||
width = coords[0]
|
||||
|
||||
if coords[1] > height:
|
||||
height = coords[1]
|
||||
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
def update_deltas_relative_to_player(self):
|
||||
"""
|
||||
Called with a scale update.
|
||||
Recalculates self.dx and self.dy so that player is centered properly.
|
||||
"""
|
||||
|
||||
borderWidthX = (self.SCALE.gameX // 2) + 1
|
||||
borderWidthY = (self.SCALE.gameY // 2) + 1
|
||||
|
||||
if self.PARENT.PLAYER.x < borderWidthX:
|
||||
# Player is in left border
|
||||
self.dx = 0
|
||||
|
||||
elif self.PARENT.PLAYER.x > self.width - borderWidthX:
|
||||
# Player is in right border
|
||||
self.dx = self.width - self.SCALE.gameX
|
||||
|
||||
else:
|
||||
# Player is within x boundaries
|
||||
self.dx = self.PARENT.PLAYER.x - borderWidthX
|
||||
|
||||
|
||||
if self.PARENT.PLAYER.y < borderWidthY:
|
||||
# Player is in the top border
|
||||
self.dy = 0
|
||||
|
||||
elif self.PARENT.PLAYER.y > self.height - borderWidthY:
|
||||
# Player is in bottom border.
|
||||
self.dy = self.height - self.SCALE.gameY
|
||||
|
||||
else:
|
||||
# Player is within y boundaries.
|
||||
self.dy = self.PARENT.PLAYER.y - borderWidthY
|
||||
|
||||
self.dx = int(self.dx)
|
||||
self.dy = int(self.dy)
|
||||
|
||||
"""
|
||||
NAME :str
|
||||
HAS_PLAYER :bool
|
||||
TYPE :str = "scene", "menu", "level"
|
||||
|
||||
scene # Does not scale with gameScale or uiScale
|
||||
# Each obj in LAYOUT is a different frame, not block sections.
|
||||
# Does not have a player, so HAS_PLAYER is not checked, and can be removed from definition.
|
||||
|
||||
menu # Does not scale with gameScale
|
||||
|
||||
LAYOUT :dict = {(x, y, rangeX, rangeY): obj}
|
||||
BGFILL :tuple = (r, g, b)
|
||||
:pygame.surface.Surface
|
||||
"""
|
||||
|
||||
class Intro_Load():
|
||||
def __init__(self):
|
||||
|
||||
self.TYPE = "scene"
|
||||
self.LAYOUT = {}
|
||||
|
||||
class Main():
|
||||
def __init__(self):
|
||||
|
||||
self.TYPE = "menu"
|
||||
self.HAS_PLAYER = False
|
||||
self.LAYOUT = {}
|
||||
|
||||
class Settings():
|
||||
def __init__(self):
|
||||
|
||||
self.TYPE = "menu"
|
||||
self.HAS_PLAYER = False
|
||||
self.LAYOUT = {}
|
||||
|
||||
class Example():
|
||||
def __init__(self):
|
||||
|
||||
self.TYPE = "level"
|
||||
self.HAS_PLAYER = True
|
||||
self.LAYOUT = {(0, 0, 32, 32): Blocks.block_blue(),
|
||||
(2, 1): Blocks.block_rainbow(layer=1)
|
||||
}
|
||||
self.STARTPOSITIONS = {"center": (0, 0), "left": (-10, 0), "right": (10, 0), "up": (0, 10), "down": (0, -10)}
|
||||
self.BGFILL = (0, 0, 0)
|
||||
274
mods/LevelEditor/main.py
Normal file
274
mods/LevelEditor/main.py
Normal file
@@ -0,0 +1,274 @@
|
||||
#!~/.pyenv/versions/3.11.6/bin/python
|
||||
#
|
||||
# Copyright (c) 2024 Cutieguwu | Olivia Brooks
|
||||
#
|
||||
# -*- coding:utf-8 -*-
|
||||
# @Title: SoD: The Massacring Mushroom Level Editor.
|
||||
# @Author: Cutieguwu | Olivia Brooks
|
||||
# @Email: owen.brooks77@gmail.com | obroo2@ocdsb.ca
|
||||
# @Description: A Level Editor for SoD: The Massacring Mushroom.
|
||||
#
|
||||
# @Script: main.py
|
||||
# @Date Created: 10 May, 2024
|
||||
# @Last Modified: 10 May, 2024
|
||||
# @Last Modified by: Cutieguwu | Olivia Brooks
|
||||
# ----------------------------------------------------------
|
||||
|
||||
# Adapted from Serge Stroobandt on https://stackoverflow.com/questions/46419607/how-to-automatically-install-required-packages-from-a-python-script-as-necessary
|
||||
|
||||
from pkg_resources import working_set
|
||||
from subprocess import check_call, CalledProcessError
|
||||
from sys import executable
|
||||
|
||||
|
||||
# Non built-ins
|
||||
libsReq = {
|
||||
"pygame",
|
||||
"icecream"
|
||||
}
|
||||
|
||||
libsInst = {
|
||||
pkg.key for pkg in working_set # Find required and installed libraries.
|
||||
}
|
||||
|
||||
libsMiss = libsReq - libsInst # Remove the already installed libraries from the dependency list.
|
||||
|
||||
try:
|
||||
if len(libsMiss) != 0:
|
||||
check_call([executable, "-m", "pip", "install", "--upgrade", "pip"]) # Ensure pip is up-to-date.
|
||||
check_call([executable, "-m", "pip", "install", *libsMiss]) # Install all missing libraries.
|
||||
except CalledProcessError:
|
||||
print("Debug | CANNOT FIND OR INSTALL DEPENDENCIES - EXITING")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
import pygame, level
|
||||
from icecream import ic
|
||||
from os import path
|
||||
import math
|
||||
|
||||
|
||||
ic.configureOutput(prefix="DEBUG | ")
|
||||
|
||||
class Editor():
|
||||
"""
|
||||
Level Editor
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
editorIcon = pygame.image.load(path.dirname(__file__) + "/data/image/editor/icon.png")
|
||||
pygame.display.set_icon(editorIcon)
|
||||
|
||||
self.VERSION = "0.0.1-alpha"
|
||||
|
||||
pygame.display.set_caption(f"SoD: TMM Level Editor v{self.VERSION}")
|
||||
|
||||
# Initialise the window.
|
||||
self.WINDOW = self.Window(self,
|
||||
1440,
|
||||
720)
|
||||
|
||||
self.bgFill = (255, 255, 255)
|
||||
self.WINDOW.window.fill(self.bgFill)
|
||||
|
||||
self.SCALE = self.Scale(parent=self)
|
||||
|
||||
self.LEVEL = level.Level(self)
|
||||
|
||||
self.editorOn = True
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
Editor run loop.
|
||||
"""
|
||||
|
||||
while self.editorOn:
|
||||
self.clock.tick(self.WINDOW.framerate)
|
||||
|
||||
# Clear window
|
||||
if self.bgFill != self.LEVEL.layout.BGFILL: # If the background has changed (may or may not change between levels)
|
||||
bgFill = self.LEVEL.layout.BGFILL # Update it locally.
|
||||
|
||||
if isinstance(self.bgFill, pygame.surface.Surface): # If it's an image, scale it accordingly.
|
||||
width = bgFill.get_width() * self.PARENT.gameScale
|
||||
height = bgFill.get_height() * self.PARENT.gameScale
|
||||
|
||||
pygame.transform.scale(self.bgFill, (width, height))
|
||||
|
||||
if isinstance(bgFill, tuple): # If background is a solid colour fill
|
||||
self.WINDOW.window.fill(bgFill)
|
||||
elif isinstance(bgFill, pygame.surface.Surface): # Elif background is an image fill.
|
||||
self.WINDOW.window.blit(bgFill, (self.LEVEL.dx * self.PARENT.gameScale, self.LEVEL.dy * self.PARENT.gameScale))
|
||||
|
||||
self.LEVEL.draw()
|
||||
|
||||
self.PLAYER.update_move(playerActionMove)
|
||||
|
||||
self.PLAYER.draw()
|
||||
|
||||
# Refresh buttons.
|
||||
for b in self.gameButtons:
|
||||
b.draw()
|
||||
|
||||
# Update objects on screen.
|
||||
pygame.display.flip()
|
||||
|
||||
# Event handler
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
self.gameOn = False
|
||||
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE: # Window exit via esc.
|
||||
ic(event.key)
|
||||
self.gameOn = False
|
||||
|
||||
if self.LEVEL.layout.HAS_PLAYER: # If layout shows a player
|
||||
for controlsDict in [
|
||||
self.controlsPlayerMovement,
|
||||
self.controlsPlayerInteraction
|
||||
]:
|
||||
for action, key in controlsDict.items(): # Check movement triggers
|
||||
if key == event.key: # Key pressed is associated with a movement action.
|
||||
if controlsDict == self.controlsPlayerMovement:
|
||||
playerActionMove = action
|
||||
elif controlsDict == self.controlsPlayerInteraction:
|
||||
if action == "trigger":
|
||||
self.PLAYER.interact()
|
||||
|
||||
elif event.type == pygame.KEYUP:
|
||||
if self.LEVEL.layout.HAS_PLAYER: # If layout shows a player
|
||||
for controlsDict in [
|
||||
self.controlsPlayerMovement,
|
||||
self.controlsPlayerInteraction
|
||||
]:
|
||||
for action, key in controlsDict.items(): # Check movement triggers
|
||||
if key == event.key: # Key released is associated with a movement action.
|
||||
if controlsDict == self.controlsPlayerMovement:
|
||||
if action == playerActionMove: # Only if the most recent movement action key was released
|
||||
playerActionMove = "neutral" # stop the player.
|
||||
|
||||
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||
for button in self.gameButtons:
|
||||
if button.is_clicked:
|
||||
if button.ID == "settings":
|
||||
# open settings panel.
|
||||
pass
|
||||
|
||||
elif event.type == pygame.MOUSEWHEEL:
|
||||
|
||||
# Scale the level
|
||||
if self.SETTINGS["controls"]["mouse"]["is_inverted"]:
|
||||
scaleNew = self.SCALE.gameScale + event.y # event.y should be +1 or -1 generally. I have seen 0 and others before somehow.
|
||||
else:
|
||||
scaleNew = self.SCALE.gameScale - event.y
|
||||
|
||||
if scaleNew in range(4, 30): # Limit the scaling range (limit level zoom).
|
||||
|
||||
if self.SCALE.gameScale < scaleNew:
|
||||
# Source images need to be refreshed if character is growing in size.
|
||||
ic(self.PLAYER.update_images(self.PLAYER.character))
|
||||
else:
|
||||
ic(self.PLAYER.update_scale())
|
||||
|
||||
self.SCALE.gameScale = scaleNew
|
||||
|
||||
ic(self.SCALE.update_units_game())
|
||||
|
||||
ic(self.PLAYER.update_scale())
|
||||
|
||||
self.LEVEL.update_deltas_relative_to_player()
|
||||
|
||||
if isinstance(self.bgFill, pygame.surface.Surface): # Update the scaling of the bgFill if it's an image fill.
|
||||
width = self.WINDOW.width / self.PARENT.gameX
|
||||
height = self.WINDOW.height / self.PARENT.gameY
|
||||
|
||||
pygame.transform.scale(self.bgFill, (width, height))
|
||||
|
||||
class Window():
|
||||
"""
|
||||
Creates a window.
|
||||
"""
|
||||
|
||||
def __init__(self, parent, width:int = 1440, height:int = 720, framerate:int = 30):
|
||||
|
||||
self.PARENT = parent
|
||||
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.size = self.width, self.height
|
||||
self.framerate = framerate
|
||||
|
||||
self.update_aspect_ratio()
|
||||
|
||||
ic(self.aspectX, self.aspectY)
|
||||
|
||||
self.window = pygame.display.set_mode(self.size)
|
||||
|
||||
def update_aspect_ratio(self):
|
||||
"""
|
||||
Updates the aspect ratio for correct block scaling.
|
||||
"""
|
||||
|
||||
gcd = math.gcd(self.height, self.width)
|
||||
|
||||
self.aspectX = self.width / gcd
|
||||
self.aspectY = self.height / gcd
|
||||
|
||||
def get_smallest_window_edge(self):
|
||||
"""
|
||||
Returns the smallest edge of the window.
|
||||
"""
|
||||
|
||||
if self.height < self.width:
|
||||
return self.height
|
||||
else:
|
||||
return self.width
|
||||
|
||||
class Scale():
|
||||
"""
|
||||
Handles all globally referenced scaling.
|
||||
"""
|
||||
|
||||
def __init__(self, parent):
|
||||
self.PARENT = parent
|
||||
self.WINDOW = self.PARENT.WINDOW
|
||||
|
||||
self.editorScale = 4
|
||||
self.uiScale = 1
|
||||
|
||||
self.update_units_editor()
|
||||
|
||||
def update_units_editor(self):
|
||||
"""
|
||||
Gets editor units.
|
||||
"""
|
||||
|
||||
self.WINDOW.update_aspect_ratio()
|
||||
|
||||
self.editorX = self.editorScale * self.WINDOW.aspectX # Blocks along X
|
||||
self.editorY = self.editorScale * self.WINDOW.aspectY # Blocks along Y
|
||||
|
||||
ic(self.editorX, self.editorY)
|
||||
|
||||
if self.editorX > self.editorY: # Determine the blocks along the shortest edge.
|
||||
blocksAlongEdge = self.editorY
|
||||
else:
|
||||
blocksAlongEdge = self.editorX
|
||||
|
||||
self.unitBlockPx = self.WINDOW.get_smallest_window_edge() / blocksAlongEdge # Defines the edge length in pixels of a section.
|
||||
|
||||
"""
|
||||
if (self.unitBlockPx % 2) != 0: # Is not an integer.
|
||||
self.unitBlockPx = (self.unitBlockPx // 1) + 1 # Round up.
|
||||
"""
|
||||
|
||||
ic(self.unitBlockPx)
|
||||
|
||||
editor = Editor()
|
||||
editor.run()
|
||||
|
||||
pygame.quit()
|
||||
Reference in New Issue
Block a user