Skip to content

Added some of the future features see description #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions components/map/mapmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
Map Manager class is responsible for creating, editing, saving and loading maps.

Future features:
- Save and load maps from file
- Map editor
- Object creator
- Object deleter
- Map saver
- Map loader
- UI for all of the above
- Map editor UI
- Find better function names
"""
class MapManager:

tiles = set()
debug = False

"""
MapManager constructor
"""
def __init__(self, window_surface, tile_size, vertical_tile_num, horizontal_tile_num):
self.window_surface = window_surface
self.tile_size = tile_size
Expand All @@ -25,23 +29,46 @@ def __init__(self, window_surface, tile_size, vertical_tile_num, horizontal_tile
def map_editor(self):
pass

def create_object(self):
pass
"""
Creates an object of type object_type at object_position
"""
def render_object(self, object_type, object_position):
if object_type == "tile":
object_position_x = object_position[0] * self.tile_size
object_position_y = object_position[1] * self.tile_size
object_dimensions = (self.tile_size, self.tile_size)
pg.draw.rect(self.window_surface, (0, 255, 0), (object_position_x, object_position_y, object_dimensions[0], object_dimensions[1]))

def delete_object(self):
pass
"""
Removes position from tiles set
"""
def delete_object(self, object_position):
if object_position in self.tiles:
self.tiles.remove(object_position)

def save_map(self):
pass
"""
Saves position to tiles set
"""
def save_map(self, object_position):
self.tiles.add(object_position)

"""
Returns tiles set
"""
def load_map(self):
pass
return self.tiles

"""
Draws debug grid on screen
"""
def draw_tile_grid(self):
for x in range(0, self.width, self.tile_size):
pg.draw.line(self.window_surface, (255,0,0), (x, 0), (x, self.height))
for y in range(0, self.height, self.tile_size):
pg.draw.line(self.window_surface, (255,0,0), (0, y), (self.width, y))

def update(self):
pass
def enable_debug(self):
self.debug = True

def disable_debug(self):
self.debug = False
31 changes: 26 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ def game_loop(self):

is_running = True

map_manager = MapManager(self.window_surface, TILE_SIZE, VERTICAL_TILE_NUM, HORIZONTAL_TILE_NUM)

player_coords = (WIDTH // 2, HEIGHT // 2)
player = Player(self.window_surface, player_coords[0], player_coords[1], 50, 50)

map_manager = MapManager(self.window_surface, TILE_SIZE, VERTICAL_TILE_NUM, HORIZONTAL_TILE_NUM)
map_manager.load_map()
map_manager.enable_debug()

while is_running:

#time_delta = clock.tick(FPS) / 1000.0
Expand All @@ -97,20 +99,39 @@ def game_loop(self):
if event.ui_element == btn:
log_info(f'{btn.text} was pressed!')

if event.type == pg.MOUSEBUTTONDOWN and map_manager.debug:
if event.button == 1:
log_info(f'Left mouse button was pressed at {events["mouse pos"]}')
# Create tile based on mouse position at nearest tile
closest_tile = (events["mouse pos"][0] // TILE_SIZE, events["mouse pos"][1] // TILE_SIZE)
log_info(f'Closest tile is {closest_tile}')
# selected_tiles.append(closest_tile)
map_manager.save_map(closest_tile)
if event.button == 3:
log_info(f'Right mouse button was pressed at {events["mouse pos"]}')
# Delete tile based on mouse position at nearest tile
closest_tile = (events["mouse pos"][0] // TILE_SIZE, events["mouse pos"][1] // TILE_SIZE)
log_info(f'Closest tile is {closest_tile}')
map_manager.delete_object(closest_tile)

self.manager.process_events(event)

self.manager.update(time_delta)

self.window_surface.blit(self.background, (0, 0))
self.manager.draw_ui(self.window_surface)

# map_manager.draw_tile_grid()

player.update()

# Draw UI
self.manager.draw_ui(self.window_surface)

if map_manager.debug:
map_manager.draw_tile_grid()

for tile in map_manager.load_map():
map_manager.render_object("tile", tile)

pg.display.update()


Expand Down