started implementing level editor, should change how levels are saved to the hashmap based one in tilemap

This commit is contained in:
StochasticMouse 2024-07-16 22:08:42 +00:00
parent 0e106be4d7
commit 47d54f43c3
4 changed files with 77 additions and 7 deletions

62
game.py
View File

@ -2,19 +2,20 @@ import pygame
import numpy as np
from scripts.tiles import Tilemap
from scripts.entities import Entity, Partner, GroupEntities
from scripts.utils import load_image, load_images, to_isometric_pixel, Button, change_status
from scripts.camera import Camera
from scripts.utils import load_image, load_images, to_isometric_pixel, Button, change_status, to_cartesian, TILE_DICT
from scripts.camera import Camera, CameraPlayerLess
# SETTINGS
WIDTH = 1280
HEIGHT = 720
TITLE = 'YuGiOh Tag Force Clone'
RENDER_SCALE = 2
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.display = pygame.Surface((WIDTH//2, HEIGHT//2))
self.display = pygame.Surface((WIDTH//RENDER_SCALE, HEIGHT//RENDER_SCALE))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
@ -73,6 +74,61 @@ class Game:
for button in buttons:
button.process()
pygame.display.update()
self.clock.tick(60)
def level_editor(self):
self.assets = {
'iso' : load_images('tiles/iso/')
}
self.tilemap = Tilemap(self, (64,32),'map_2.csv')
self.camera = CameraPlayerLess(self)
self.tile_list = list(self.assets)
self.tile_sel = 0
while self.status == 'level_editor':
click, r_click = False, False
scrollup,scrolldown = False, False
delta = np.array([0.0,0.0])
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.status = 'quit'
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 4:
scrollup = True
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 5:
scrolldown = True
keys = pygame.key.get_pressed()
mouse_butt = pygame.mouse.get_pressed(num_buttons = 5)
if keys[pygame.K_LEFT]:
delta += np.array([-5,0])
if keys[pygame.K_RIGHT]:
delta += np.array([5,0])
if keys[pygame.K_UP]:
delta += np.array([0,-5])
if keys[pygame.K_DOWN]:
delta += np.array([0,5])
if mouse_butt[0]: #on left click
click = True
if mouse_butt[2]: #on right click
r_click = True
if scrollup: #on scroll up(maybe)
self.tile_sel = ((self.tile_sel - 1) % len(TILE_DICT))
if scrolldown: #on scroll down(maybe)
self.tile_sel = ((self.tile_sel + 1) % len(TILE_DICT))
mpos = np.array(pygame.mouse.get_pos()) / RENDER_SCALE
pos = np.array(to_cartesian(mpos + self.camera.scroll,self.tilemap.tile_size),dtype=int)
if click:
self.tilemap.tilemap[*pos] = {'type' : TILE_DICT[self.tile_sel][0], 'variant' : TILE_DICT[self.tile_sel][1], 'pos': pos, 'walkable': TILE_DICT[self.tile_sel][2]}
current_tile_img = self.assets[TILE_DICT[self.tile_sel][0]][TILE_DICT[self.tile_sel][1]].copy()
current_tile_img.set_alpha(100)
self.camera.update(delta)
self.display.fill('white')
self.tilemap.render(self.display, offset = self.camera.scroll)
self.display.blit(current_tile_img, to_isometric_pixel(pos)*self.tilemap.tile_size-self.camera.scroll)
self.screen.blit(pygame.transform.scale(self.display,self.screen.get_size()), (0,0))
pygame.display.update()
self.clock.tick(60)
def run(self):
self.status = 'menu'

View File

@ -2,9 +2,18 @@ import pygame
import numpy as np
from scripts.utils import to_isometric_pixel
class Camera:
class CameraPlayerLess:
def __init__(self, game, scroll = np.array([0.,0.])):
self.scroll = scroll
self.game = game
def update(self, delta):
self.scroll += delta
class Camera(CameraPlayerLess):
#def __init__(self, game, scroll = np.array([0.,0.])):
# self.scroll = scroll
# self.game = game
def update(self):
self.scroll += (to_isometric_pixel(self.game.player.pos-np.array([48,80]))*np.array([1,0.5]) - np.array(self.game.display.get_size())/2 - self.scroll)/30
super().update((to_isometric_pixel(self.game.player.pos-np.array([48,80]))*np.array([1,0.5]) - np.array(self.game.display.get_size())/2 - self.scroll)/30)
#self.scroll += (to_isometric_pixel(self.game.player.pos-np.array([48,80]))*np.array([1,0.5]) - np.array(self.game.display.get_size())/2 - self.scroll)/30

View File

@ -7,10 +7,11 @@ NEIGHBOR_OFFSETS = [np.array((i,j)) for i in (-1,0,1) for j in (-1,0,1)]
class Tilemap:
def __init__(self, game, tile_size, map_path):
def __init__(self, game, tile_size, map_path = None):
self.game = game
self.tile_size = np.array(tile_size)
self.offgrid_tiles = []
if map_path:
self.load_map(map_path)
def load_map(self, map_path):
print(map_path)

View File

@ -2,6 +2,7 @@ import pygame
import os
import numpy as np
TILE_DICT = {
0 : ('iso',6,False),
1 : ('iso',0,True),
2 : ('iso',1,False),
3 : ('iso',2,False),
@ -37,6 +38,9 @@ def to_isometric_pixel(coord):
a = np.array([[1,-1],[1,1]])
a = 0.5 * a
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
def to_cartesian_pixel(coord):
a = np.array([[1,1],[-1,1]])
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
class Animation:
def __init__(self, images, img_dur=5, loop=True):