Compare commits
9 Commits
8f2dcbfd21
...
1.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 265322f58b | |||
| 5dbd9211de | |||
| 2a0def7fa6 | |||
| 08d6261d3f | |||
| 95afab5431 | |||
| d897a3744d | |||
| 4161ec9c5b | |||
| a421069a63 | |||
| 44f75b52d0 |
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "gameUtils"
|
||||
version = "0.0.1.dev0"
|
||||
version = "1.0.0"
|
||||
description = "A set of utilities to make the game development process less painful (despite the organisation name) and more unified."
|
||||
authors = [{ name = "Brosef" }]
|
||||
dependencies = ["pygame-ce"]
|
||||
@ -2,3 +2,6 @@ from .base import Game
|
||||
from .utils import centre
|
||||
from .anim import AnimationHandler
|
||||
from .anim import AnimatedObject
|
||||
from .events import AnimStart
|
||||
from .events import AnimFinish
|
||||
from .events import Timeout
|
||||
@ -1,3 +1,5 @@
|
||||
from .events import AnimStart
|
||||
from .events import AnimFinish
|
||||
import pygame
|
||||
import time
|
||||
|
||||
@ -18,16 +20,21 @@ class AnimatedObject:
|
||||
"""
|
||||
|
||||
"""
|
||||
def __init__(self, baseFrame):
|
||||
def __init__(self, _game, objectID: str, baseFrame: str):
|
||||
"""
|
||||
Args:
|
||||
_game: Used internally.
|
||||
objectID: Used to identify this animated object.
|
||||
baseFrame: A still image that gets displayed when no other animation is playing.
|
||||
"""
|
||||
|
||||
self._game = _game
|
||||
self.objectID = objectID
|
||||
self._currentAnim = None
|
||||
self._animStart = None
|
||||
self._animations = {}
|
||||
self.baseFrame = pygame.image.load(baseFrame)
|
||||
self.size = self.baseFrame.size
|
||||
self._surf = pygame.Surface(self.baseFrame.size)
|
||||
|
||||
def getFrame(self) -> pygame.Surface:
|
||||
@ -46,6 +53,7 @@ class AnimatedObject:
|
||||
frame = min(round((time.perf_counter() - self._animStart) * self._currentAnim.fps), self._currentAnim.frameCount - 1)
|
||||
self._surf.blit(self._currentAnim.frames[frame][0], (0, 0))
|
||||
if frame == self._currentAnim.frameCount - 1:
|
||||
self._game.onEvent(AnimFinish(self.objectID, self._currentAnim.animationID))
|
||||
self._currentAnim = None
|
||||
self._animStart = None
|
||||
|
||||
@ -62,12 +70,16 @@ class AnimatedObject:
|
||||
"""
|
||||
self._animations.update({animation.animationID: animation})
|
||||
|
||||
def playAnim(self, animationID: str):
|
||||
def playAnim(self, animationID: str, overrideCurrentAnim=False):
|
||||
"""
|
||||
Plays an animation.
|
||||
|
||||
Args:
|
||||
animationID (str): The animation ID to play.
|
||||
"""
|
||||
|
||||
if self._currentAnim != None and not overrideCurrentAnim: return
|
||||
|
||||
self._animStart = time.perf_counter()
|
||||
self._currentAnim = self._animations[animationID]
|
||||
self._game.onEvent(AnimStart(self.objectID, animationID))
|
||||
|
||||
@ -1,4 +1,8 @@
|
||||
from .events import Timeout
|
||||
from .anim import AnimatedObject
|
||||
import NoPELib
|
||||
import tomllib
|
||||
import time
|
||||
|
||||
class Game:
|
||||
def __init__(self, surface):
|
||||
@ -9,18 +13,65 @@ class Game:
|
||||
surface (pygame.Surface): The surface the game devs draw on.
|
||||
"""
|
||||
|
||||
self.surf = surface
|
||||
self.size = self.surf.size
|
||||
self.pm = None
|
||||
|
||||
with open('./game.toml', 'r') as f:
|
||||
self.cfg = tomllib.loads(f.read())
|
||||
|
||||
self.surf = surface
|
||||
self.size = self.surf.size
|
||||
self.pm = NoPELib.PlayersManager(self.cfg['name'], playersPath='../players.json')
|
||||
# Holds all the timeouts that haven't been fired yet
|
||||
self._timeouts = []
|
||||
|
||||
def update(self):
|
||||
pass
|
||||
"""
|
||||
Updates some core things in the background.
|
||||
"""
|
||||
|
||||
for timeout in self._timeouts.copy():
|
||||
if timeout.fireOn <= time.perf_counter():
|
||||
self.onEvent(timeout)
|
||||
self._timeouts.remove(timeout)
|
||||
|
||||
def onEvent(self, event):
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
def createAnimObj(self, *args, **kwargs):
|
||||
"""
|
||||
Creates an animated object.
|
||||
|
||||
Args:
|
||||
objectID (str): The ID of the object.
|
||||
baseFrame (str): The path to the base frame.
|
||||
"""
|
||||
|
||||
return AnimatedObject(self, *args, **kwargs)
|
||||
|
||||
def timeout(self, id: str, delay: float):
|
||||
"""
|
||||
Fires a Timeout event with the specified ID
|
||||
after the specified delay.
|
||||
|
||||
Args:
|
||||
id (str): The timeout ID.
|
||||
delay (float): How long (in seconds)
|
||||
to wait before firing.
|
||||
"""
|
||||
|
||||
self._timeouts.append(Timeout(id, time.perf_counter()+delay))
|
||||
|
||||
def awaitingTimeout(self, id: str):
|
||||
"""
|
||||
Tells you if a timeout ID is on the timeout stack.
|
||||
|
||||
Args:
|
||||
id (str): The timeout ID.
|
||||
|
||||
Returns:
|
||||
bool: True if there is a timeout with that ID
|
||||
ID on the stack, False otherwise.
|
||||
"""
|
||||
|
||||
return any([timeout.timeoutID == id for timeout in self._timeouts])
|
||||
36
src/gameUtils/events.py
Normal file
36
src/gameUtils/events.py
Normal file
@ -0,0 +1,36 @@
|
||||
import time
|
||||
|
||||
class _event:
|
||||
def __init__(self):
|
||||
self.type = self.__class__
|
||||
|
||||
class AnimStart(_event):
|
||||
def __init__(self, objectID, animationID):
|
||||
super().__init__()
|
||||
self.objectID = objectID
|
||||
self.animationID = animationID
|
||||
|
||||
def __repr__(self):
|
||||
return f'<AnimFinish | {self.objectID=} | {self.animationID=}>'
|
||||
|
||||
class AnimFinish(_event):
|
||||
def __init__(self, objectID, animationID):
|
||||
super().__init__()
|
||||
self.objectID = objectID
|
||||
self.animationID = animationID
|
||||
|
||||
def __repr__(self):
|
||||
return f'<AnimFinish | {self.objectID=} | {self.animationID=}>'
|
||||
|
||||
class Timeout(_event):
|
||||
def __init__(self, timeoutID, fireOn):
|
||||
super().__init__()
|
||||
# The timeout ID specified by the user
|
||||
self.timeoutID = timeoutID
|
||||
# When the event should be fired
|
||||
self.fireOn = fireOn
|
||||
# When it was created
|
||||
self.created = time.perf_counter()
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Timeout | {self.timeoutID=} | {self.fireOn=} | {self.created=}>'
|
||||
@ -1,14 +1,22 @@
|
||||
import pygame
|
||||
|
||||
def centre(surface: pygame.Surface, rect: tuple[int, int, int, int]) -> pygame.Surface:
|
||||
def centre(surface: pygame.Surface, size: tuple[int, int]) -> pygame.Surface:
|
||||
"""
|
||||
Centres a surface within a given rectangle.
|
||||
|
||||
Args:
|
||||
surface: The surface to be centred.
|
||||
rect: The rectangle the surface will be centred in.
|
||||
size: The size of the rectangle the surface will be centred in.
|
||||
|
||||
Returns:
|
||||
pygame.Surface
|
||||
"""
|
||||
pass
|
||||
|
||||
surf = pygame.Surface(size)
|
||||
offX = surf.size[0]//2 - surface.size[0]//2
|
||||
offY = surf.size[1]//2 - surface.size[1]//2
|
||||
|
||||
surf.blit(surface, (offX, offY))
|
||||
|
||||
return surf
|
||||
|
||||
Reference in New Issue
Block a user