Started work on NPC dialogues
[butaba-adventures.git] / leveleditor.py
old mode 100644 (file)
new mode 100755 (executable)
index 784199b..bbfcbf7
@@ -1,13 +1,14 @@
-#!/usr/bin/env python3
+#!/usr/bin/env python
 
 # level designer gui - this is used to design level graphics
 # very rough and ready - main purpose is to allow quick game
-# design. inputting levels by hand array is very tough
+# design. inputting levels by hand array is very tedious
 
 import pygame
 import os.path
 import sys
 import cPickle
+import math
 
 import utility
 
@@ -25,13 +26,18 @@ def draw_level (screen, leveldata, tileset):
                                j += 1
                        i += 1
 
-def display_menu (screen):
+def display_menu (screen, tileset, tilerow, tilecol):
        utility.put_text (screen, 490, 10, 10, (255,255,0), "p to pick tile")
        utility.put_text (screen, 490, 50, 10, (255,255,0), "w to wall (solid)")
-       utility.put_text (screen, 490, 90, 10, (255,255,0), "<space> to place")
-       utility.put_text (screen, 490, 130, 10,(255,255,0), "Arrows to move around")
-       utility.put_text (screen, 490, 170, 10, (255,255,0), "s to Save level")
-       utility.put_text (screen, 490, 210, 10, (255,255,0), "q to Quit editor")
+       utility.put_text (screen, 490, 90, 10, (255,255,0), "<Space> or  <Enter>")
+       utility.put_text (screen, 490, 110, 10, (255,255,0), "to place")
+       utility.put_text (screen, 490, 150, 10,(255,255,0), "Arrows to move around")
+       utility.put_text (screen, 490, 190, 10, (255,255,0), "s to Save level")
+       utility.put_text (screen, 490, 230, 10, (255,255,0), "q to Quit editor")
+
+       # currently selected tile
+       utility.put_text (screen, 490, 270, 10, (255,255,255), "Tile selected")
+       screen.blit (tileset, (490, 290), (tilecol*48, tilerow*48, 48, 48))
 
 # draw the tile cursor
 def draw_cursor (screen, currow, curcol):
@@ -42,6 +48,102 @@ def make_wall (leveldata, row, col):
        # get the actual data in that place
        leveldata[row][col][2] = not leveldata[row][col][2]
 
+# place a tile at current spot
+def put_tile (leveldata, row, col, tilerow, tilecol):
+       leveldata[row][col][0] = tilerow
+       leveldata[row][col][1] = tilecol
+
+# picking a tile from the tileset
+def pick_tile (screen, tileset, selrow, selcol):
+
+       # total number of rows and columns
+       totalrows = tileset.get_height () / 48
+       totalcols = tileset.get_width () / 48
+
+       # implement scrolling within the tileset image in a 480x480 viewport
+
+       # set the current page
+       curpage_cols = selrow / 10
+       curpage_rows = selcol / 10
+
+       # cursor row and column
+       cursor_row = selrow % 10
+       cursor_col = selcol % 10
+
+       # the code for scrolling through the tileset is slightly complicated here are the steps
+       # logic goes like this
+       # e.g. horizontal movement
+       # left arrow key movement:
+       # 1. reduce the absolute column by 1
+       # 2. if absolute column < 0 then set absolute column to last column as total columns - 1
+       # 3. calculate the current page based on absolute column as absolute column  / 10
+       #    10 being the number of tile columns per page
+       # 4. calculate the relative column on screen as (abolute column modulo 10) giving a
+       #    division remainder between 0 and 9.
+
+       # same logic is used for all other movements
+
+       while 1:
+               screen.fill (pygame.Color (0, 0, 0))
+               screen.blit (tileset, (0, 0), (curpage_cols * 480, curpage_rows * 480, 480, 480))
+               draw_cursor (screen, cursor_row, cursor_col)
+               utility.put_text (screen, 490, 10,10, (255, 255, 0), "<Space> or <Enter>")
+               utility.put_text (screen, 490, 30,10, (255, 255, 0), "to select tile")
+               utility.put_text (screen, 490, 70,10, (255, 255, 0), "<Escape> to return")
+               utility.put_text (screen, 10, 490,10, (255, 255, 0), "Selected: %d row, %d col" % (selrow, selcol))
+               pygame.display.update ()
+               for event in pygame.event.get ():
+                       if event.type == pygame.KEYDOWN:
+                               if event.key == pygame.K_ESCAPE:
+                                       return None
+                               elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
+                                       return selrow, selcol
+                               elif event.key == pygame.K_UP:
+                                       # reduce the selected row by 1
+                                       selrow -= 1
+                                       if selrow < 0:
+                                               selrow = totalrows - 1
+
+                                       # calculate the current page
+                                       curpage_rows = selrow / 10
+
+                                       # calculate the current cursor row
+                                       cursor_row = selrow % 10
+
+                               elif event.key == pygame.K_DOWN:
+                                       # increase the selected row by 1
+                                       selrow += 1
+                                       if selrow >= totalrows:
+                                               selrow = 0
+
+                                       # calculate the current row page
+                                       curpage_rows = selrow / 10
+
+                                       # calculate the cursor row
+                                       cursor_row = selrow % 10
+                               elif event.key == pygame.K_LEFT:
+                                       # decrease the selected col by 1
+                                       selcol -= 1
+                                       if selcol < 0:
+                                               selcol = totalcols - 1
+
+                                       # calculate the current column page
+                                       curpage_cols = selcol / 10
+
+                                       # calculate the cursor column
+                                       cursor_col = selcol % 10
+                               elif event.key == pygame.K_RIGHT:
+                                       # increase the selected column by 1
+                                       selcol += 1
+                                       if selcol >= totalcols:
+                                               selcol = 0
+
+                                       # calculate the current column page
+                                       curpage_cols = selcol / 10
+
+                                       # calculate the cursor column
+                                       cursor_col = selcol % 10
+
 # the actual level editor
 def level_editor (fname):
        # load level data
@@ -51,12 +153,13 @@ def level_editor (fname):
        # load the tileset
        tileset = pygame.image.load (os.path.join ("background", "tileset.png"))
        screen = pygame.display.set_mode ((720, 512))
-       pygame.display.set_caption ("Level editor")
+       pygame.display.set_caption ("Level editor: %s" % fname)
        currow, curcol = 0, 0
+       tilerow, tilecol = 0, 0
 
        while 1:
                screen.fill (pygame.Color (0, 0, 0))
-               display_menu (screen)
+               display_menu (screen, tileset, tilerow, tilecol)
                draw_level (screen, leveldata, tileset)
                draw_cursor (screen, currow, curcol)
                pygame.display.update ()
@@ -65,6 +168,9 @@ def level_editor (fname):
                                if event.key == ord ("q"):
                                        pygame.quit ()
                                        return
+                               elif event.key == ord ("s"):
+                                       cPickle.dump (leveldata, file (fname, "w"))
+                                       print ("Level saved")
                                elif event.key == pygame.K_DOWN:
                                        currow += 1
                                        if currow > 9:
@@ -81,6 +187,13 @@ def level_editor (fname):
                                        curcol += 1
                                        if curcol > 9:
                                                curcol = 0
+                               elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
+                                       put_tile (leveldata, currow, curcol, tilerow, tilecol)
+                               elif event.key == ord ("p"):
+                                       tile = pick_tile (screen, tileset, tilerow, tilecol)
+                                       if tile is not None:
+                                               tilerow = tile[0]
+                                               tilecol = tile[1]
                                elif event.key == ord ("w"):
                                        make_wall (leveldata, currow, curcol)
 
@@ -107,7 +220,8 @@ def new_level ():
        level_editor (fname)
 
 def load_level ():
-       pass
+       fname = raw_input ("Level file to load: ")
+       level_editor (fname)
 
 def main ():
        while 1:
@@ -130,4 +244,4 @@ def main ():
 
 
 if __name__=="__main__":
-       main ()
\ No newline at end of file
+       main ()