diff --git a/forges/__init__.py b/forges/__init__.py index 8a38537..ea02ed2 100644 --- a/forges/__init__.py +++ b/forges/__init__.py @@ -23,4 +23,8 @@ def run(): forges.run() def get(): - return forges \ No newline at end of file + return forges + +def import_sdl2(): + global sdl2 + import sdl2 \ No newline at end of file diff --git a/forges/build.py b/forges/build.py new file mode 100644 index 0000000..db0afd5 --- /dev/null +++ b/forges/build.py @@ -0,0 +1,54 @@ +import os, shutil, sys + +class Builder: + def __init__(self, file_name, noconsole = True, flags = []): + self.file_name = file_name + self.default_flags = [] + + if noconsole: + self.default_flags.append("--noconsole") + + else: + self.default_flags.append("--console") + + self.flags = self.default_flags + flags + self.flags_string = "" + + space = 0 + + for i in self.flags: + if space != 0: + space -= 1 + self.flags_string += " " + + self.flags_string += i + space += 1 + + for i in sys.path: + if "site-packages" in i: + files = os.listdir(i) + + state = 0 + + for j in files: + if "forges" == j: + state += 1 + self.forges_path = i + "\\" + j + + if "sdl2" == j: + state += 1 + self.sdl2_path = i + "\\" + j + + if "sdl2dll" == j: + state += 1 + self.sdl2dll_path = i + "\\" + j + + if state == 3: + self.site_packages_path = i + + def build(self): + os.system(f"pyinstaller {self.file_name} {self.flags_string} --noconfirm --onefile --clean --icon={self.forges_path}/assets/icon/icon_ico.ico --add-data \"{self.sdl2dll_path};sdl2dll/\" --add-data \"{self.forges_path};forges/\"") + shutil.copy(f"dist/{os.path.splitext(os.path.basename(self.file_name))[0]}.exe", ".") + shutil.rmtree("build", ignore_errors = True) + shutil.rmtree("dist", ignore_errors = True) + os.remove(f"{os.path.splitext(os.path.basename(self.file_name))[0]}.spec") \ No newline at end of file diff --git a/forges/prefabs/__init__.py b/forges/prefabs/__init__.py index 23ca0a0..50b9796 100644 --- a/forges/prefabs/__init__.py +++ b/forges/prefabs/__init__.py @@ -1 +1,3 @@ -from forges.prefabs.top_view_controller import TopViewController \ No newline at end of file +from forges.prefabs.top_view_controller import TopViewController +from forges.prefabs.platformer_controller import PlatformerController +from forges.prefabs.button import Button \ No newline at end of file diff --git a/forges/prefabs/button.py b/forges/prefabs/button.py new file mode 100644 index 0000000..dd77a0c --- /dev/null +++ b/forges/prefabs/button.py @@ -0,0 +1,64 @@ +import forges + +class Button(forges.Entity): + def __init__(self, *args, **kwargs): + super().__init__() + + self.width, self.height = 100, 40 + self.normal_color, self.press_color, self.highlight_color = forges.color.Color(200, 200, 200), forges.color.Color(100, 100, 100), forges.color.Color(150, 150, 150) + + self.color = forges.color.Color(self.normal_color.r, self.normal_color.g, self.normal_color.b, self.normal_color.a) + + for i in kwargs: + setattr(self, i, kwargs[i]) + + self.window = forges.get().get_window() + self.functions = {} + self.target_color = forges.color.Color(self.color.r, self.color.g, self.color.b, self.color.a) + + def update(self): + if self.hit(self.window.input.mouse_pos()): + if self.window.input.mouse_pressed(self.window.buttons["LEFT"]): + self.press() + + else: + if self.pressed: + self.release() + + self.highlight() + + else: + self.natural() + self.pressed = False + + self.color.r, self.color.g, self.color.b, self.color.a = int(self.target_color.r), int(self.target_color.g), int(self.target_color.b), int(self.target_color.a) + + def natural(self): + self.target_color = forges.math.lerp(self.target_color, self.normal_color, 0.1) + + def press(self): + self.pressed = True + + self.target_color = forges.math.lerp(self.target_color, self.press_color, 0.3) + + if "on_press" in self.functions: + self.functions["on_press"]() + + def release(self): + self.pressed = False + + self.target_color = forges.math.lerp(self.target_color, self.highlight_color, 0.1) + + if "on_release" in self.functions: + self.functions["on_release"]() + + def highlight(self): + self.pressed = False + + self.target_color = forges.math.lerp(self.target_color, self.highlight_color, 0.2) + + if "on_highlight" in self.functions: + self.functions["on_highlight"]() + + def event(self, func): + self.functions[func.__name__] = func \ No newline at end of file diff --git a/forges/prefabs/platformer_controller.py b/forges/prefabs/platformer_controller.py new file mode 100644 index 0000000..118c268 --- /dev/null +++ b/forges/prefabs/platformer_controller.py @@ -0,0 +1,38 @@ +import forges + +class PlatformerController(forges.Entity): + def __init__(self, *args, **kwargs): + super().__init__() + + self.window = forges.forges.get_window() + + self.speed = 5 + self.sprint_speed = 10 + self.jumpforce = -13 + + for i in kwargs: + setattr(self, i, kwargs[i]) + + self.target_speed = self.speed + self.add_script(forges.scripts.Gravity()) + + def add_object(self, object): + self.get_script(0).add_object(object) + + def update(self): + if self.window.input.key_pressed(self.window.keys["LSHIFT"]): + self.target_speed = self.sprint_speed + + else: + self.target_speed = self.speed + + if self.window.input.key_pressed(self.window.keys["D"]): + self.x += self.target_speed + + if self.window.input.key_pressed(self.window.keys["A"]): + self.x -= self.target_speed + + if self.window.input.key_pressed(self.window.keys["SPACE"]): + if self.get_script(0).grounded: + self.get_script(0).grounded = False + self.get_script(0).force(forges.math.Vector2(0, self.jumpforce)) \ No newline at end of file diff --git a/forges/prefabs/top_view_controller.py b/forges/prefabs/top_view_controller.py index 64d8aa8..1331a41 100644 --- a/forges/prefabs/top_view_controller.py +++ b/forges/prefabs/top_view_controller.py @@ -1,15 +1,18 @@ import forges class TopViewController(forges.Entity): - def __init__(self, width = 50, height = 100, x = 0, y = 0, color = forges.color.Color(20, 20, 20), fill = True, layer = 1, speed = 3, sprint_speed = 6): - super().__init__(width = width, height = height, x = x, y = y, color = color, fill = fill, layer = layer) + def __init__(self, *args, **kwargs): + super().__init__() self.window = forges.forges.get_window() - self.speed = speed - self.sprint_speed = sprint_speed + self.speed = 3 + self.sprint_speed = 6 - self.target_speed = speed + for i in kwargs: + setattr(self, i, kwargs[i]) + + self.target_speed = self.speed def update(self): if self.window.input.key_pressed(self.window.keys["LSHIFT"]): diff --git a/forges/scripts/__init__.py b/forges/scripts/__init__.py index 49caeb0..0552762 100644 --- a/forges/scripts/__init__.py +++ b/forges/scripts/__init__.py @@ -1 +1,2 @@ -from forges.scripts.toggle_fullscreen import ToggleFullscreen \ No newline at end of file +from forges.scripts.toggle_fullscreen import ToggleFullscreen +from forges.scripts.gravity import Gravity \ No newline at end of file diff --git a/forges/scripts/gravity.py b/forges/scripts/gravity.py new file mode 100644 index 0000000..30280fc --- /dev/null +++ b/forges/scripts/gravity.py @@ -0,0 +1,36 @@ +import forges + +class Gravity(forges.Script): + def __init__(self, *args, **kwargs): + super().__init__() + + self.acceleration = forges.math.Vector2(0, 0.5) + self.velocity = forges.math.Vector2(0, 0) + self.friction = -0.01 + + self.objects = [] + + for i in kwargs: + setattr(self, i, kwargs[i]) + + def add_object(self, object): + self.objects.append(object) + + def force(self, force): + self.velocity += force + + def update(self, window, entity): + self.acceleration = forges.math.Vector2(0, 0.5) + self.grounded = False + + for i in self.objects: + if i.hit(entity): + if entity.y < i.y: + entity.y = i.y - entity.height + self.velocity = forges.math.Vector2(0, 0) + self.grounded = True + + if not self.grounded: + self.acceleration.x += self.velocity.x * self.friction + self.velocity += self.acceleration + entity.set_pos(entity.get_pos() + self.velocity + forges.math.Vector2(0, 0.5) + self.acceleration) \ No newline at end of file diff --git a/forges/window.py b/forges/window.py index a608c7d..7764132 100644 --- a/forges/window.py +++ b/forges/window.py @@ -155,6 +155,9 @@ def update_handler(self): self.functions["update"]() def draw_handler(self): + if "on_draw" in self.functions: + self.functions["on_draw"]() + for layer in dict(sorted(self.engine.objects.items(), reverse = True)): for object in self.engine.objects[layer]: if object.destroyed: @@ -163,6 +166,9 @@ def draw_handler(self): else: if hasattr(object, "visible"): if object.visible: + if hasattr(object, "on_draw"): + object.on_draw() + object.draw() def set_fullscreen(self, fullscreen = True): diff --git a/setup.py b/setup.py index 79400f0..8bd2bc2 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name = "forges", - version = "0.0.2", + version = "0.0.3", description = "A game engine made with SDL 2.", long_description = long_desc, long_description_content_type = "text/markdown",