Added alot of things, waypoints, grid for editor, game camera system, enemy moving on pre defined waypoint (path)
|
After Width: | Height: | Size: 243 B |
|
After Width: | Height: | Size: 99 B |
|
After Width: | Height: | Size: 99 B |
|
After Width: | Height: | Size: 99 B |
|
After Width: | Height: | Size: 134 B |
|
After Width: | Height: | Size: 99 B |
|
After Width: | Height: | Size: 99 B |
|
After Width: | Height: | Size: 106 B |
|
After Width: | Height: | Size: 114 B |
|
After Width: | Height: | Size: 109 B |
|
After Width: | Height: | Size: 112 B |
|
After Width: | Height: | Size: 110 B |
|
After Width: | Height: | Size: 106 B |
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"defaults": {
|
||||||
|
"colours": {
|
||||||
|
"normal_bg": "#252525",
|
||||||
|
"hovered_bg": "#353535",
|
||||||
|
"disabled_bg": "#202020",
|
||||||
|
"selected_bg": "#454545",
|
||||||
|
"active_bg": "#555555",
|
||||||
|
"normal_text": "#FFFFFF",
|
||||||
|
"hovered_text": "#FFFFFF",
|
||||||
|
"selected_text": "#FFFFFF",
|
||||||
|
"disabled_text": "#777777"
|
||||||
|
},
|
||||||
|
"font": {
|
||||||
|
"name": "assets/fonts/arial.ttf",
|
||||||
|
"size": "14",
|
||||||
|
"bold": "0",
|
||||||
|
"italic": "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
from game.util import load_image, load_images
|
import pygame_gui as pgi
|
||||||
|
from game.util import *
|
||||||
from game.tilemap import Tilemap
|
from game.tilemap import Tilemap
|
||||||
|
|
||||||
RENDER_SCALE = 2.0
|
RENDER_SCALE = 2.0
|
||||||
@@ -7,12 +8,15 @@ WINDOW_SIZE=(640, 480)
|
|||||||
|
|
||||||
class Editor():
|
class Editor():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
pg.font.init()
|
||||||
self.screen = pg.display.set_mode(WINDOW_SIZE)
|
self.screen = pg.display.set_mode(WINDOW_SIZE)
|
||||||
self.clock = pg.time.Clock()
|
self.clock = pg.time.Clock()
|
||||||
|
|
||||||
self.width, self.height = pg.display.get_surface().get_size()
|
self.width, self.height = pg.display.get_surface().get_size()
|
||||||
self.tilemap = Tilemap(self)
|
self.tilemap = Tilemap(self, 16)
|
||||||
self.display = pg.Surface((self.width/4*3, self.height/6*5))
|
self.display = pg.Surface((WINDOW_SIZE[0] / RENDER_SCALE, WINDOW_SIZE[1] / RENDER_SCALE))
|
||||||
|
|
||||||
|
self.manager = pgi.UIManager(self.screen.get_size(), "assets/theme/editor_theme.json")
|
||||||
|
|
||||||
self.map = 'test'
|
self.map = 'test'
|
||||||
self.path = 'assets/maps/' + str(self.map) + '.json'
|
self.path = 'assets/maps/' + str(self.map) + '.json'
|
||||||
@@ -25,7 +29,8 @@ class Editor():
|
|||||||
self.scroll = [0, 0]
|
self.scroll = [0, 0]
|
||||||
|
|
||||||
self.assets = {
|
self.assets = {
|
||||||
"base": load_images("/tiles/test")
|
"base": load_images("/tiles/test"),
|
||||||
|
"jungle": load_images("/tiles/jungle")
|
||||||
}
|
}
|
||||||
|
|
||||||
self.tile_list = list(self.assets)
|
self.tile_list = list(self.assets)
|
||||||
@@ -39,6 +44,14 @@ class Editor():
|
|||||||
self.right_clicking = False
|
self.right_clicking = False
|
||||||
self.shift = False
|
self.shift = False
|
||||||
self.ongrid = True
|
self.ongrid = True
|
||||||
|
self.edit_mode = "tile"
|
||||||
|
|
||||||
|
self.drop_down_controls = pgi.elements.UIDropDownMenu(
|
||||||
|
options_list=["Mode: Tile", "Mode: Path"],
|
||||||
|
starting_option="Mode: Tile",
|
||||||
|
relative_rect=pg.Rect((self.screen.get_width() -200, 0), (200, 30)),
|
||||||
|
manager=self.manager
|
||||||
|
)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
@@ -49,25 +62,35 @@ class Editor():
|
|||||||
return self.running
|
return self.running
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
#for pygame_gui
|
||||||
|
time_delta = self.clock.tick(60) / 1000.0
|
||||||
|
|
||||||
self.mpos = pg.mouse.get_pos()
|
self.mpos = pg.mouse.get_pos()
|
||||||
|
# print(self.mpos)
|
||||||
self.mpos = (self.mpos[0] / RENDER_SCALE, self.mpos[1] / RENDER_SCALE)
|
self.mpos = (self.mpos[0] / RENDER_SCALE, self.mpos[1] / RENDER_SCALE)
|
||||||
self.tile_pos = (int((self.mpos[0] + self.scroll[0]) // self.tilemap.tile_size), int((self.mpos[1] + self.scroll[1]) // self.tilemap.tile_size))
|
self.tile_pos = (int((self.mpos[0] + self.scroll[0]) // self.tilemap.tile_size), int((self.mpos[1] + self.scroll[1]) // self.tilemap.tile_size))
|
||||||
|
# print(self.tile_pos)
|
||||||
|
|
||||||
self.scroll[0] += (self.movment[1] - self.movment[0]) * 2
|
self.scroll[0] += (self.movment[1] - self.movment[0]) * 2
|
||||||
self.scroll[1] += (self.movment[3] - self.movment[2]) * 2
|
self.scroll[1] += (self.movment[3] - self.movment[2]) * 2
|
||||||
|
|
||||||
if self.clicking and self.ongrid:
|
self.process_clicks()
|
||||||
self.tilemap.tilemap[str(self.tile_pos[0]) + ';' + str(self.tile_pos[1])] = {'type': self.tile_list[self.tile_group], 'variant': self.tile_variant, 'pos': self.tile_pos}
|
|
||||||
if self.right_clicking:
|
|
||||||
tile_loc = str(self.tile_pos[0]) + ';' + str(self.tile_pos[1])
|
|
||||||
if tile_loc in self.tilemap.tilemap:
|
|
||||||
del self.tilemap.tilemap[tile_loc]
|
|
||||||
|
|
||||||
self.current_tile_img = self.assets[self.tile_list[self.tile_group]][self.tile_variant].copy()
|
self.current_tile_img = self.assets[self.tile_list[self.tile_group]][self.tile_variant].copy()
|
||||||
|
|
||||||
for event in pg.event.get():
|
for event in pg.event.get():
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.running = False
|
self.running = False
|
||||||
|
self.process_event(event)
|
||||||
|
|
||||||
|
self.manager.update(time_delta)
|
||||||
|
|
||||||
|
def process_event(self, event):
|
||||||
|
#pygame_gui
|
||||||
|
self.manager.process_events(event)
|
||||||
|
if event.type == pgi.UI_DROP_DOWN_MENU_CHANGED:
|
||||||
|
self.edit_mode = event.text.split(" ")[1].lower()
|
||||||
|
|
||||||
if event.type == pg.MOUSEBUTTONDOWN:
|
if event.type == pg.MOUSEBUTTONDOWN:
|
||||||
if event.button == 1:
|
if event.button == 1:
|
||||||
self.clicking = True
|
self.clicking = True
|
||||||
@@ -104,6 +127,12 @@ class Editor():
|
|||||||
self.shift = True
|
self.shift = True
|
||||||
if event.key == pg.K_o:
|
if event.key == pg.K_o:
|
||||||
self.tilemap.save(self.path)
|
self.tilemap.save(self.path)
|
||||||
|
if event.key == pg.K_BACKSPACE:
|
||||||
|
if self.edit_mode == "path":
|
||||||
|
self.remove_last_waypoint()
|
||||||
|
if event.key == pg.K_c:
|
||||||
|
if self.edit_mode == "path":
|
||||||
|
self.clear_waypoints()
|
||||||
if event.type == pg.KEYUP:
|
if event.type == pg.KEYUP:
|
||||||
if event.key == pg.K_a:
|
if event.key == pg.K_a:
|
||||||
self.movment[0] = False
|
self.movment[0] = False
|
||||||
@@ -116,18 +145,136 @@ class Editor():
|
|||||||
if event.key == pg.K_LSHIFT:
|
if event.key == pg.K_LSHIFT:
|
||||||
self.shift = False
|
self.shift = False
|
||||||
|
|
||||||
|
def process_clicks(self):
|
||||||
|
if self.manager.get_hovering_any_element():
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.clicking and self.ongrid and self.edit_mode == "tile":
|
||||||
|
self.tilemap.tilemap[str(self.tile_pos[0]) + ';' + str(self.tile_pos[1])] = {'type': self.tile_list[self.tile_group], 'variant': self.tile_variant, 'pos': self.tile_pos}
|
||||||
|
if self.clicking and self.ongrid and self.edit_mode == "path":
|
||||||
|
self.add_waypoint(self.tile_pos)
|
||||||
|
if self.right_clicking and self.edit_mode == "tile":
|
||||||
|
tile_loc = str(self.tile_pos[0]) + ';' + str(self.tile_pos[1])
|
||||||
|
if tile_loc in self.tilemap.tilemap:
|
||||||
|
del self.tilemap.tilemap[tile_loc]
|
||||||
|
if self.right_clicking and self.edit_mode == "path":
|
||||||
|
self.remove_waypoint_at(self.tile_pos)
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
self.display.fill((0, 0, 0))
|
self.display.fill((0, 0, 0))
|
||||||
|
|
||||||
render_scroll = (int(self.scroll[0]), int(self.scroll[1]))
|
render_scroll = (int(self.scroll[0]), int(self.scroll[1]))
|
||||||
|
|
||||||
self.display.blit(self.current_tile_img, (10, 10))
|
self.draw_grid(self.display, 60, render_scroll)
|
||||||
|
|
||||||
self.tilemap.render(self.display, offset=render_scroll)
|
self.tilemap.render(self.display, offset=render_scroll)
|
||||||
if self.ongrid:
|
|
||||||
|
if self.edit_mode == "path":
|
||||||
|
self.draw_path(self.display, self.tilemap.waypoints, self.scroll)
|
||||||
|
|
||||||
|
if self.ongrid and self.edit_mode == "tile":
|
||||||
self.display.blit(self.current_tile_img, (self.tile_pos[0] * self.tilemap.tile_size - self.scroll[0], self.tile_pos[1] * self.tilemap.tile_size - self.scroll[1]))
|
self.display.blit(self.current_tile_img, (self.tile_pos[0] * self.tilemap.tile_size - self.scroll[0], self.tile_pos[1] * self.tilemap.tile_size - self.scroll[1]))
|
||||||
|
elif self.ongrid and self.edit_mode == "path":
|
||||||
|
pg.draw.circle(self.display, (255, 0, 0), (self.tile_pos[0] * self.tilemap.tile_size - self.scroll[0] + self.tilemap.tile_size//2, self.tile_pos[1] * self.tilemap.tile_size - self.scroll[1] + self.tilemap.tile_size//2), 5)
|
||||||
else:
|
else:
|
||||||
|
if self.edit_mode == "tile":
|
||||||
self.display.blit(self.current_tile_img, self.mpos)
|
self.display.blit(self.current_tile_img, self.mpos)
|
||||||
|
|
||||||
|
self.display.blit(self.current_tile_img, (10, 10))
|
||||||
|
|
||||||
self.screen.blit(pg.transform.scale(self.display, self.screen.get_size()))
|
self.screen.blit(pg.transform.scale(self.display, self.screen.get_size()))
|
||||||
|
|
||||||
|
self.manager.draw_ui(self.screen)
|
||||||
|
|
||||||
pg.display.update()
|
pg.display.update()
|
||||||
|
|
||||||
|
def draw_grid(self, surf, alpha, offset=(0, 0)):
|
||||||
|
tile_size = self.tilemap.tile_size
|
||||||
|
temp_surf = pg.Surface((self.width, self.height), pg.SRCALPHA)
|
||||||
|
offset_x, offset_y = offset
|
||||||
|
|
||||||
|
start_x = (offset_x // tile_size) * tile_size # normalize
|
||||||
|
start_y = (offset_y // tile_size) * tile_size
|
||||||
|
|
||||||
|
for x in range(start_x, offset_x + self.width, tile_size):
|
||||||
|
screen_x = x - offset_x
|
||||||
|
pg.draw.line(
|
||||||
|
temp_surf,
|
||||||
|
(255, 255, 255, alpha),
|
||||||
|
(screen_x, 0),
|
||||||
|
(screen_x, self.height)
|
||||||
|
)
|
||||||
|
|
||||||
|
for y in range(start_y, offset_y + self.height + tile_size, tile_size):
|
||||||
|
screen_y = y - offset_y
|
||||||
|
pg.draw.line(
|
||||||
|
temp_surf,
|
||||||
|
(255, 255, 255, alpha),
|
||||||
|
(0, screen_y),
|
||||||
|
(self.width, screen_y)
|
||||||
|
)
|
||||||
|
|
||||||
|
surf.blit(temp_surf, (0, 0))
|
||||||
|
|
||||||
|
def draw_path(self, surf, waypoints, offset):
|
||||||
|
if len(waypoints) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# draw first point even if there is no line yet
|
||||||
|
if len(waypoints) == 1:
|
||||||
|
pixel_point = tile_to_pixel(
|
||||||
|
waypoints[0],
|
||||||
|
self.tilemap.tile_size,
|
||||||
|
True
|
||||||
|
)
|
||||||
|
|
||||||
|
pixel_point_off = (
|
||||||
|
pixel_point[0] - offset[0],
|
||||||
|
pixel_point[1] - offset[1]
|
||||||
|
)
|
||||||
|
|
||||||
|
pg.draw.circle(surf, (255, 0, 0), pixel_point_off, 3)
|
||||||
|
return
|
||||||
|
|
||||||
|
for i in range(len(waypoints) -1):
|
||||||
|
tile_point1 = waypoints[i]
|
||||||
|
tile_point2 = waypoints[i+1]
|
||||||
|
|
||||||
|
pixel_point1 = tile_to_pixel(tile_point1, self.tilemap.tile_size, True)
|
||||||
|
pixel_point2 = tile_to_pixel(tile_point2, self.tilemap.tile_size, True)
|
||||||
|
pixel_point1_off = (pixel_point1[0] - offset[0], pixel_point1[1] - offset[1])
|
||||||
|
pixel_point2_off = (pixel_point2[0] - offset[0], pixel_point2[1] - offset[1])
|
||||||
|
pg.draw.circle(surf, (255, 0, 0), pixel_point1_off, 3)
|
||||||
|
|
||||||
|
pg.draw.line(
|
||||||
|
surf,
|
||||||
|
(255, 0, 0),
|
||||||
|
pixel_point1_off,
|
||||||
|
pixel_point2_off
|
||||||
|
)
|
||||||
|
pg.draw.circle(surf, (255, 0, 0), pixel_point2_off, 3)
|
||||||
|
|
||||||
|
def add_waypoint(self, tile_pos):
|
||||||
|
waypoint = [tile_pos[0], tile_pos[1]]
|
||||||
|
|
||||||
|
if waypoint in self.tilemap.waypoints:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.tilemap.waypoints.append(waypoint)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_waypoint_at(self, tile_pos):
|
||||||
|
waypoint = [tile_pos[0], tile_pos[1]]
|
||||||
|
|
||||||
|
if waypoint in self.tilemap.waypoints:
|
||||||
|
self.tilemap.waypoints.remove(waypoint)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_last_waypoint(self):
|
||||||
|
if self.tilemap.waypoints:
|
||||||
|
self.tilemap.waypoints.pop()
|
||||||
|
|
||||||
|
|
||||||
|
def clear_waypoints(self):
|
||||||
|
self.tilemap.waypoints.clear()
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import pygame as pg
|
||||||
|
|
||||||
|
class Camera():
|
||||||
|
def __init__(self):
|
||||||
|
self.zoom = 1
|
||||||
|
self.offset = pg.math.Vector2(0, 0)
|
||||||
|
|
||||||
|
def world_to_screen(self, pos):
|
||||||
|
return (
|
||||||
|
int((pos[0] - self.offset.x) * self.zoom),
|
||||||
|
int((pos[1] - self.offset.y) * self.zoom)
|
||||||
|
)
|
||||||
|
|
||||||
|
def screen_to_world(self, pos):
|
||||||
|
return (
|
||||||
|
pos[0] / self.zoom + self.offset.x,
|
||||||
|
pos[1] / self.zoom + self.offset.y
|
||||||
|
)
|
||||||
|
|
||||||
|
def move(self, dx, dy):
|
||||||
|
self.offset.x += dx
|
||||||
|
self.offset.y += dy
|
||||||
|
|
||||||
|
def change_zoom(self, amount):
|
||||||
|
self.zoom += amount
|
||||||
|
self.zoom = max(0.5, min(self.zoom, 4.0))
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
from game.util import *
|
||||||
|
import pygame as pg
|
||||||
|
|
||||||
|
class Enemy(pg.sprite.Group):
|
||||||
|
def __init__(self, game, speed, health):
|
||||||
|
super().__init__()
|
||||||
|
self.game = game
|
||||||
|
self.waypoints = self.game.tilemap.waypoints
|
||||||
|
print(self.waypoints)
|
||||||
|
|
||||||
|
self.speed = speed
|
||||||
|
self.health = health
|
||||||
|
|
||||||
|
self.image = self.game.assets['enemy']
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
|
||||||
|
self.pos = pg.Vector2(
|
||||||
|
tile_to_pixel(self.waypoints[0], self.game.tilemap.tile_size, True)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.target_index = 1
|
||||||
|
|
||||||
|
self.rect.center = self.pos
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
if self.target_index >= len(self.waypoints):
|
||||||
|
return
|
||||||
|
|
||||||
|
tile_size = self.game.tilemap.tile_size
|
||||||
|
|
||||||
|
target = pg.Vector2(
|
||||||
|
tile_to_pixel(
|
||||||
|
self.waypoints[self.target_index],
|
||||||
|
tile_size,
|
||||||
|
True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
direction = target - self.pos
|
||||||
|
distance = direction.length()
|
||||||
|
|
||||||
|
if distance <= self.speed:
|
||||||
|
self.pos = target
|
||||||
|
self.target_index += 1
|
||||||
|
else:
|
||||||
|
direction = direction.normalize()
|
||||||
|
self.pos += direction * self.speed
|
||||||
|
|
||||||
|
self.rect.center = self.pos
|
||||||
|
|
||||||
|
def render(self, surf, camera):
|
||||||
|
zoom = camera.zoom
|
||||||
|
scaled_rect = self.rect.scale_by(zoom)
|
||||||
|
|
||||||
|
scaled_image = pg.transform.scale(
|
||||||
|
self.image,
|
||||||
|
(scaled_rect.size[0], scaled_rect.size[1])
|
||||||
|
)
|
||||||
|
|
||||||
|
scaled_pos = camera.world_to_screen(self.pos)
|
||||||
|
scaled_rect.center = scaled_pos
|
||||||
|
|
||||||
|
surf.blit(scaled_image, scaled_rect)
|
||||||
@@ -20,12 +20,21 @@ class Game:
|
|||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
self.ui_manager = pygame_gui.UIManager(
|
self.ui_manager = pygame_gui.UIManager(
|
||||||
WINDOW_SIZE,
|
WINDOW_SIZE,
|
||||||
"theme.json"
|
"assets/theme/theme.json"
|
||||||
)
|
)
|
||||||
self.tilemap = Tilemap(self)
|
self.tilemap = Tilemap(self)
|
||||||
|
self.map = 'test'
|
||||||
|
self.path = 'assets/maps/' + str(self.map) + '.json'
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.tilemap.load(self.path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
print('File not Found')
|
||||||
|
print(self.tilemap.waypoints)
|
||||||
self.assets={
|
self.assets={
|
||||||
"base": load_images('/tiles/test')
|
"base": load_images('/tiles/test'),
|
||||||
|
"jungle": load_images('/tiles/jungle'),
|
||||||
|
"enemy": load_image('/entities/enemies/sand_monster.png')
|
||||||
}
|
}
|
||||||
|
|
||||||
self.screens = {
|
self.screens = {
|
||||||
@@ -59,6 +68,8 @@ class Game:
|
|||||||
|
|
||||||
self.current_screen.process_event(event)
|
self.current_screen.process_event(event)
|
||||||
|
|
||||||
|
self.current_screen.update(dt)
|
||||||
|
|
||||||
self.ui_manager.update(dt)
|
self.ui_manager.update(dt)
|
||||||
|
|
||||||
self.window.fill((20, 20, 20))
|
self.window.fill((20, 20, 20))
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
from game.screens.base_screen import BaseScreen
|
from game.screens.base_screen import BaseScreen
|
||||||
|
from game.enemy import Enemy
|
||||||
|
from game.camera import Camera
|
||||||
|
from game.util import *
|
||||||
import pygame as pg
|
import pygame as pg
|
||||||
import pygame_gui as pgi
|
import pygame_gui as pgi
|
||||||
|
|
||||||
@@ -6,30 +9,62 @@ class GamePlay(BaseScreen):
|
|||||||
def __init__(self, game):
|
def __init__(self, game):
|
||||||
super().__init__(game)
|
super().__init__(game)
|
||||||
|
|
||||||
self.tile_multiplicator = 2.0
|
# self.tile_multiplicator = 2.0
|
||||||
self.width, self.height = pg.display.get_surface().get_size()
|
self.width, self.height = pg.display.get_surface().get_size()
|
||||||
self.map_screen = pg.surface.Surface((int(self.width/4*3),int(self.height/6*5)))
|
self.game_surface = pg.surface.Surface((int(self.width/4*3),int(self.height/6*5)))
|
||||||
|
# self.map_surface = pg.surface.Surface((self.game_surface.get_width()/self.tile_multiplicator,self.game_surface.get_height()/self.tile_multiplicator))
|
||||||
|
|
||||||
self.scroll = [0, 0]
|
self.camera = Camera()
|
||||||
|
self.movment = [False, False, False, False]
|
||||||
|
|
||||||
self.map = 'test'
|
self.map = 'test'
|
||||||
self.path = 'assets/maps/' + str(self.map) + '.json'
|
self.path = 'assets/maps/' + str(self.map) + '.json'
|
||||||
|
|
||||||
|
self.enemy = Enemy(self.game, 0.5, 100)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.game.tilemap.load(self.path)
|
self.game.tilemap.load(self.path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('File not Found')
|
print('File not Found')
|
||||||
|
|
||||||
def process_event(self, event):
|
def process_event(self, event):
|
||||||
pass
|
if event.type == pg.KEYDOWN:
|
||||||
|
if event.key == pg.K_a:
|
||||||
|
self.movment[0] = True
|
||||||
|
if event.key == pg.K_d:
|
||||||
|
self.movment[1] = True
|
||||||
|
if event.key == pg.K_w:
|
||||||
|
self.movment[2] = True
|
||||||
|
if event.key == pg.K_s:
|
||||||
|
self.movment[3] = True
|
||||||
|
|
||||||
|
if event.key == pg.K_q:
|
||||||
|
self.camera.change_zoom(-0.25)
|
||||||
|
if event.key == pg.K_e:
|
||||||
|
self.camera.change_zoom(0.25)
|
||||||
|
if event.type == pg.KEYUP:
|
||||||
|
if event.key == pg.K_a:
|
||||||
|
self.movment[0] = False
|
||||||
|
if event.key == pg.K_d:
|
||||||
|
self.movment[1] = False
|
||||||
|
if event.key == pg.K_w:
|
||||||
|
self.movment[2] = False
|
||||||
|
if event.key == pg.K_s:
|
||||||
|
self.movment[3] = False
|
||||||
|
|
||||||
def update(self, dt):
|
def update(self, dt):
|
||||||
pass
|
self.enemy.update()
|
||||||
|
|
||||||
|
self.camera.move((self.movment[1] - self.movment[0]) * 2, (self.movment[3] - self.movment[2]) * 2)
|
||||||
|
|
||||||
def draw(self, surface):
|
def draw(self, surface):
|
||||||
self.map_screen.fill((40, 40, 40))
|
self.game_surface.fill((40, 40, 40))
|
||||||
|
# self.map_surface.fill((40, 40, 40))
|
||||||
|
|
||||||
render_scroll = (int(self.scroll[0]), int(self.scroll[1]))
|
self.game.tilemap.render_game(self.game_surface, self.camera)
|
||||||
self.game.tilemap.render(self.map_screen, offset=render_scroll)
|
|
||||||
|
|
||||||
self.game.window.blit(self.map_screen, (0, self.height/6))
|
self.enemy.render(self.game_surface, self.camera)
|
||||||
|
|
||||||
|
# self.game_surface.blit(pg.transform.scale(self.map_surface, self.game_surface.get_size()))
|
||||||
|
|
||||||
|
surface.blit(self.game_surface, (0, self.height/6))
|
||||||
|
|||||||
@@ -1,14 +1,21 @@
|
|||||||
import pygame, json
|
import pygame, json
|
||||||
|
|
||||||
|
"""
|
||||||
|
Todo:
|
||||||
|
* Add prop system for Trees, etc. (like off_grid), handle blocked with game objects over props
|
||||||
|
"""
|
||||||
|
|
||||||
class Tilemap:
|
class Tilemap:
|
||||||
def __init__(self, game, tile=16):
|
def __init__(self, game, tile=16):
|
||||||
self.game = game
|
self.game = game
|
||||||
self.tile_size = tile
|
self.tile_size = tile
|
||||||
self.tilemap = {}
|
self.tilemap = {}
|
||||||
|
self.offgrid_tiles = []
|
||||||
|
self.waypoints=[]
|
||||||
|
|
||||||
def save(self, path):
|
def save(self, path):
|
||||||
f = open(path, 'w')
|
f = open(path, 'w')
|
||||||
json.dump({'tilemap': self.tilemap, 'tile_size': self.tile_size}, f)
|
json.dump({'tilemap': self.tilemap, 'tile_size': self.tile_size, 'waypoints': self.waypoints}, f)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def load(self, path):
|
def load(self, path):
|
||||||
@@ -18,7 +25,7 @@ class Tilemap:
|
|||||||
|
|
||||||
self.tilemap = map_data['tilemap']
|
self.tilemap = map_data['tilemap']
|
||||||
self.tile_size = map_data['tile_size']
|
self.tile_size = map_data['tile_size']
|
||||||
|
self.waypoints = map_data['waypoints']
|
||||||
|
|
||||||
def render(self, surf, offset=(0, 0)):
|
def render(self, surf, offset=(0, 0)):
|
||||||
for x in range(offset[0] // self.tile_size, (offset[0] + surf.get_width()) // self.tile_size + 1):
|
for x in range(offset[0] // self.tile_size, (offset[0] + surf.get_width()) // self.tile_size + 1):
|
||||||
@@ -27,3 +34,36 @@ class Tilemap:
|
|||||||
if loc in self.tilemap:
|
if loc in self.tilemap:
|
||||||
tile = self.tilemap[loc]
|
tile = self.tilemap[loc]
|
||||||
surf.blit(self.game.assets[tile['type']][tile['variant']], (tile['pos'][0] * self.tile_size - offset[0], tile['pos'][1] * self.tile_size - offset[1]))
|
surf.blit(self.game.assets[tile['type']][tile['variant']], (tile['pos'][0] * self.tile_size - offset[0], tile['pos'][1] * self.tile_size - offset[1]))
|
||||||
|
|
||||||
|
def render_game(self, surf, camera):
|
||||||
|
tile_size = self.tile_size
|
||||||
|
zoom = camera.zoom
|
||||||
|
scaled_tile_size = int(tile_size * zoom)
|
||||||
|
|
||||||
|
start_x = int(camera.offset.x // tile_size)
|
||||||
|
start_y = int(camera.offset.y // tile_size)
|
||||||
|
|
||||||
|
end_x = int((camera.offset.x + surf.get_width() / zoom) // tile_size) + 2
|
||||||
|
end_y = int((camera.offset.y + surf.get_height() / zoom) // tile_size) + 2
|
||||||
|
|
||||||
|
for x in range(start_x, end_x):
|
||||||
|
for y in range(start_y, end_y):
|
||||||
|
loc = f"{x};{y}"
|
||||||
|
|
||||||
|
if loc in self.tilemap:
|
||||||
|
tile = self.tilemap[loc]
|
||||||
|
image = self.game.assets[tile["type"]][tile["variant"]]
|
||||||
|
|
||||||
|
scaled_image = pygame.transform.scale(
|
||||||
|
image,
|
||||||
|
(scaled_tile_size, scaled_tile_size)
|
||||||
|
)
|
||||||
|
|
||||||
|
world_pos = (
|
||||||
|
tile["pos"][0] * tile_size,
|
||||||
|
tile["pos"][1] * tile_size
|
||||||
|
)
|
||||||
|
|
||||||
|
screen_pos = camera.world_to_screen(world_pos)
|
||||||
|
|
||||||
|
surf.blit(scaled_image, screen_pos)
|
||||||
|
|||||||
@@ -14,3 +14,15 @@ def load_images(dir_path):
|
|||||||
for img_name in os.listdir(img_path + dir_path):
|
for img_name in os.listdir(img_path + dir_path):
|
||||||
images.append(load_image(dir_path + '/' + img_name))
|
images.append(load_image(dir_path + '/' + img_name))
|
||||||
return images
|
return images
|
||||||
|
|
||||||
|
def tile_to_pixel(waypoint, tile_size, center=False):
|
||||||
|
if center:
|
||||||
|
return (
|
||||||
|
waypoint[0] * tile_size + tile_size // 2,
|
||||||
|
waypoint[1] * tile_size + tile_size // 2
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return (
|
||||||
|
waypoint[0] * tile_size,
|
||||||
|
waypoint[1] * tile_size
|
||||||
|
)
|
||||||
|
|||||||