You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
6.7 KiB
87 lines
6.7 KiB
from enne2engine.sdl2_wrapper import SDL2Wrapper |
|
from enne2engine.pyglet_wrapper import PygletWrapper |
|
from enne2engine.controls import UserControls |
|
import sys |
|
import os |
|
|
|
from enne2engine.Entities.entity import Entity |
|
|
|
class GameEngine(UserControls): |
|
def __init__(self): |
|
super().__init__() |
|
if "--pyglet" in sys.argv: |
|
self.graphics = PygletWrapper() |
|
else: |
|
self.graphics = SDL2Wrapper() |
|
self.map = [ |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }], |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }], |
|
[{ 'wall': True, 'tile': "landscapeTiles_066" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': True, 'tile': "landscapeTiles_066" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }], |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': True, 'tile': "landscapeTiles_066" }], |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }], |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }], |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }], |
|
[{ 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': False, 'tile': "landscapeTiles_067" }, { 'wall': True, 'tile': "landscapeTiles_066" }] |
|
] |
|
self.frame_time = 0 |
|
self.cursor_pos = (0, 0) |
|
|
|
self.load_assets() |
|
self.entities = [] |
|
|
|
def run(self): |
|
running = True |
|
self.entities.append(Entity("knight", 0, 0, "idle", 1, 1, self)) |
|
self.graphics.create_background(self.map, "tiles") |
|
|
|
while running: |
|
perf_counter = self.graphics.get_perf_counter() |
|
event = self.graphics.handle_events() |
|
if event: |
|
self.handle_events("keymap_game", event) |
|
running = False if event == "QUIT" else True |
|
self.graphics.clear_screen() |
|
self.render_background() |
|
self.cursor_pos = self.graphics.draw_cursor() |
|
for entity in self.entities: |
|
entity.update() |
|
self.graphics.update_status(f"Frame time: {round(self.frame_time)}ms - FPS: {round(1000/self.frame_time if self.frame_time != 0 else 1)}") |
|
self.graphics.present_renderer() |
|
self.frame_time = self.graphics.get_frame_time(perf_counter) |
|
self.graphics.delay_frame(self.frame_time,50) |
|
self.graphics.quit() |
|
def set_cursor(self, x, y): |
|
self.graphics.cursor = (x, y) |
|
|
|
def render_background(self): |
|
for y, row in enumerate(self.map): |
|
for x, cell in enumerate(row): |
|
cell["occlusion"] = self.get_occlusion(x, y) |
|
if cell['occlusion']==1: |
|
continue |
|
self.graphics.render_tile("tiles", cell, x, y) |
|
|
|
def get_occlusion(self, x, y): |
|
if (x, y) == self.cursor_pos: |
|
return 0 |
|
distance = self.graphics.get_distance((x, y), self.cursor_pos) |
|
if distance <2: |
|
return 0.2 |
|
elif distance < 3: |
|
return 0.5 |
|
else: |
|
return 0.8 |
|
|
|
|
|
def load_assets(self): |
|
self.graphics.load_tilesheet("tiles", "assets/tiles/landscapeTiles_sheet.png") |
|
for dir in os.listdir("assets/KnightBasic"): |
|
for file in os.listdir(f"assets/KnightBasic/{dir}"): |
|
if file.endswith(".json"): |
|
self.graphics.load_spritesheet(file[:-5].lower(), f"assets/KnightBasic/{dir}/{file}") |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
engine = GameEngine() |
|
engine.run() |