Browse Source

Add project as it was left from like 2008

Getty Ritter 4 years ago
commit
441490d4ac
15 changed files with 557 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 20 0
      src/common.py
  3. 9 0
      src/error.py
  4. 128 0
      src/esse.py
  5. 119 0
      src/game.py
  6. 125 0
      src/graphics.py
  7. 51 0
      src/kelciste.py
  8. 13 0
      src/maps/house
  9. 36 0
      src/maps/main
  10. 12 0
      src/maps/store
  11. 9 0
      src/models/block.pmd
  12. 5 0
      src/models/door.pmd
  13. 5 0
      src/models/player.pmd
  14. 18 0
      src/models/sign.pmd
  15. 5 0
      src/models/tile.pmd

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+*~
+*.pyc

+ 20 - 0
src/common.py

@@ -0,0 +1,20 @@
+
+# 
+class Directions:
+    UP = 'up'
+    DOWN = 'down'
+    LEFT = 'left'
+    RIGHT = 'right'
+    STOP = 'stop'
+    
+    UP_T = 0, -1
+    DOWN_T = 0, 1
+    LEFT_T = -1, 0
+    RIGHT_T = 1, 0
+    STOP_T = 0, 0
+    
+    TUPS = {UP: UP_T,
+          DOWN: DOWN_T,
+          LEFT: LEFT_T,
+          RIGHT: RIGHT_T,
+          STOP: STOP_T}

+ 9 - 0
src/error.py

@@ -0,0 +1,9 @@
+#
+#Error lookup stuff
+
+def error(type):
+    print "\x1b[31m", error_lookup[type], "\x1b[39m"
+    
+error_lookup = {'bad_command': 'Cannot parse command!\n',
+      'block': 'Illegal direction!\n',
+      'file': 'File does not exist!'}

+ 128 - 0
src/esse.py

@@ -0,0 +1,128 @@
+from common import Directions
+
+# Esse - any interactible object which exists on the esse layer
+class esse:
+    def __init__(self, x, y):
+        self._x = _x
+        self._y = _y
+    def getX(self):
+        return self._x
+    def getY(self):
+        return self._y
+    def setX(self, x):
+        self._x = x
+    def setY(self, y):
+        self._y = y
+    def interact(self, game):
+        print 'Interacted with an Esse!'
+    def __str__(self):
+        return "E"
+    def passable(self):
+        return False
+    def getModelName(self):
+        return 'tile.pmd'
+    def getColors(self):
+        return (1.0, 1.0, 1.0)
+
+# Nothing - when esse layer does not contain an esse        
+class nothing:
+    def interact(self, game):
+        print 'Ain\'t nothin\' here!'
+    def __str__(self):
+        return " "
+    def passable(self):
+        return True
+    def getModelName(self):
+        return 'tile.pmd'
+    def getColors(self):
+        return (1.0, 1.0, 1.0)
+
+# Parse initial player data
+def p_pc(L):
+    myX, myY = map(int, L[1:3])
+    return pc(myX, myY, 100)
+
+# Player - inherits from esse
+class pc(esse):
+    def __init__(self, x, y, life):
+        self._x = x
+        self._y = y
+        self.life = life
+        self.facing = Directions.UP
+    def __str__(self):
+        if self.facing == Directions.UP:
+            return "^"
+        if self.facing == Directions.DOWN:
+            return "v"
+        if self.facing == Directions.LEFT:
+            return "<"
+        if self.facing == Directions.RIGHT:
+            return ">"
+        return "P"
+    def face(self, dir):
+        self.facing = dir
+    def getFacing(self):
+        return self.facing
+    def getModelName(self):
+        return 'player.pmd'
+    def getColors(self):
+        return (0.75, 0.0, 0.0)
+
+# Parse initial sign data        
+def p_sign(L):
+    myX, myY = map(int, L[0:2])
+    myText = ' '.join(L[2:])
+    return sign(myX, myY, myText)
+
+# Sign class - displays text
+class sign(esse):
+    def __init__(self, x, y, text):
+        self._x = x
+        self._y = y
+        self.text = text
+    def interact(self, game):
+        print self.text
+    def __str__(self):
+        return "S"
+    def getModelName(self):
+        return 'sign.pmd'
+    def getColors(self):
+        return (0.0, 0.0, 1.0)
+
+# NPC class (right now, a glorified signpost)        
+class npc(esse):
+    def __init__(self, x, y, text):
+        self._x = x
+        self._y = y
+        self.text = text
+    def interact(self, game):
+        print text
+        
+# Parse door placement
+def p_door(L):
+    myX, myY= map(int, L[0:2])
+    myE = L[2]
+    myDest = L[3]
+    return door(myX, myY, myDest, myE)
+
+# Door class (portal to another map)
+class door(esse):
+    def __init__(self, x, y, destination, entry):
+        self._x = x
+        self._y = y
+        self.dest = destination
+        self.entry = entry
+    def interact(self, game):
+        game.load(self.dest, self.entry)
+    def passable(self):
+        return self.open
+    def __str__(self):
+        return "D"
+    def getModelName(self):
+        return 'door.pmd'
+    def getColors(self):
+        return (0.0, 1.0, 0.0)
+# Esse lookup table - for parsing purposes        
+esse_lookup = {'sign': p_sign,
+               'entry': p_pc,
+               'door': p_door}

+ 119 - 0
src/game.py

@@ -0,0 +1,119 @@
+#
+#Game - contains the primary game code
+
+import os
+import error
+import esse
+from common import Directions
+
+class game:
+    def __init__(self):
+        self.board = gameBoard()
+        self.player = self.board.getPlayer()
+    def move(self, direction):
+        self.player.face(direction)
+        self.board.moveObject(self.player, direction)
+    def interact(self):
+        add = Directions.TUPS[self.player.getFacing()]
+        targetX, targetY = self.player.getX() + add[0], self.player.getY() + add[1]
+        self.board.getObject(targetX, targetY).interact(self)
+    def load(self, map, entry='a'):
+        self.player = None
+        self.board.readPassMap(map, entry)
+        self.player = self.board.getPlayer()
+    def __str__(self):
+        return self.board.__str__()
+    
+class gameBoard:
+    def __init__(self):
+        self.readPassMap('main', 'a')
+        
+    def readPassMap(self, fileName, entry):
+        self.theNothing = esse.nothing()
+        fullPath = os.path.join('maps', fileName)
+        self.player = None
+        if os.path.exists(fullPath):
+            f = open(fullPath)
+            #Get the name
+            self.name = f.readline()
+            #Create new objects
+            l = f.readline() #Ignore the #objects directive
+            l = f.readline()
+            newObjects = []
+            while '#map' not in l:
+                command = l.strip().split()
+                if command[0] == 'entry' and command[1] != entry:
+                	print "Ignoring command: ", command
+                else:
+                    newObjects.append(esse.esse_lookup[command[0]](command[1:]))
+                    if command[0] == 'entry':
+                        self.player = newObjects[-1]
+                    print "Added new: ", newObjects[-1]
+                l = f.readline()
+            #Get the pass data
+            l = f.readline()
+            self.passData, self.objectData = [], []
+            n = 0
+            while l != '':
+                self.passData.append([])
+                self.objectData.append([])
+                for char in l:
+                    self.passData[n].append(char)
+                    self.objectData[n].append(self.theNothing)
+                n += 1
+                l = f.readline()
+            #Populate object data
+            for object in newObjects:
+                self.objectData[object.getY()][object.getX()] = object
+        else:
+            error.error('file')
+            
+    def getPlayer(self):
+        return self.player
+            
+    def getPass(self, x, y):
+        if self.passData[y][x] == ' ':
+            if self.objectData[y][x] == self.theNothing:
+                return True
+        return False 
+    
+    def getObject(self, x, y):
+        return self.objectData[y][x]
+    
+    def putObject(self, x, y, object):
+        self.objectData[y][x] = object
+    
+    def removeObject(self, x, y):
+        self.objectData[y][x] = self.theNothing
+    
+    def moveObject(self, object, dir):
+        oldx, oldy = object.getX(), object.getY()
+        add = Directions.TUPS[dir]
+        x, y = oldx + add[0], oldy + add[1]
+        print "Moving ", object, " to location ", x, y
+        if self.getPass(x, y):
+            print "That place is free!"
+            object.setX(x)
+            object.setY(y)
+            self.putObject(x, y, object)
+            self.removeObject(oldx, oldy)
+            print "Move successful!"
+        else:
+            print "That place is blocked by ", self.getObject(x, y)
+            print "It also has a floor of ", self.passData[y][x]
+            error.error('block')
+        
+    def __str__(self):
+        returnValue = ''
+        y = 0
+        for a in self.passData:
+            x = 0
+            for b in a:
+                if self.passData[y][x] != ' ':
+                    returnValue += "X" 
+                else:
+                    returnValue += self.getObject(x, y).__str__()
+                x += 1
+            returnValue += '\n'
+            y += 1
+        return returnValue

+ 125 - 0
src/graphics.py

@@ -0,0 +1,125 @@
+import os
+from OpenGL.GL import *
+from OpenGL.GLU import *
+import pygame, pygame.image
+from pygame.locals import *
+import sys
+from common import Directions
+
+# TODO: Implement OBJ loading
+def loadModel(name):
+    fullpath = os.path.join('models', name)
+    f = open(fullpath)
+    drawtable = []
+    for line in f.readlines():
+        if line[0] != "#":
+            drawtable.append(map(float, line.strip().split()))
+    f.close()
+    return drawtable
+
+# TODO: About eight billion optimizations!
+def drawModel(name, xoff, yoff, (r, g, b)):
+    xoff *= 2
+    yoff *= 2
+    table = loadModel(name)
+    glBegin(GL_QUADS)
+    glColor3f(r, g, b)
+    for point in table:
+        glVertex3f(point[0]+xoff, point[1]+yoff, point[2])
+    glEnd()
+    
+def init():
+    glShadeModel(GL_SMOOTH)
+    glClearColor(0.0, 0.0, 0.0, 0.0)
+    glClearDepth(1.0)
+    glEnable(GL_DEPTH_TEST)
+    glDepthFunc(GL_LEQUAL)
+    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
+    rtri = 0.0
+    rquad = 0.0
+    
+def resize((width, height)):
+    if height==0:
+        height=1
+    glViewport(0, 0, width, height)
+    glMatrixMode(GL_PROJECTION)
+    glLoadIdentity()
+    gluPerspective(45, 1.0*width/height, 0.1, 100.0)
+    glMatrixMode(GL_MODELVIEW)
+    glLoadIdentity()
+    
+def draw(gameState):
+    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+    glLoadIdentity()
+    glTranslatef(-30.0, -30.0, -90.0)
+    
+    y = 0
+    for a in gameState.board.passData:
+        x = 0
+        for b in a:
+            if gameState.board.passData[y][x] != ' ':
+                drawModel('block.pmd', x, y, (0.4, 0.4, 0.4))
+            else:
+                drawModel(gameState.board.getObject(x, y).getModelName(), x, y,
+                          gameState.board.getObject(x, y).getColors())
+            x += 1
+        y += 1
+        
+def graphicsloop(gameState):
+
+    video_flags = OPENGL|DOUBLEBUF
+    
+    pygame.init()
+    pygame.display.set_mode((640,480), video_flags)
+
+    resize((640,480))
+    init()
+
+    frames = 0
+    ticks = pygame.time.get_ticks()
+    while 1:
+        event = pygame.event.poll()
+        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
+            break
+        if event.type == KEYDOWN:
+            manageKeys(event.key, gameState)
+        draw(gameState)
+        pygame.display.flip()
+        frames = frames+1
+
+    print "fps:  %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks))
+    
+def manageKeys(key, game):
+    if key in command_lookup:
+        command_lookup[key](game)
+    else:
+        print "Key not bound!"
+    
+def up(gameState):
+    gameState.move(Directions.UP)
+    
+def down(gameState):
+    gameState.move(Directions.DOWN)
+    
+def left(gameState):
+    gameState.move(Directions.LEFT)
+    
+def right(gameState):
+    gameState.move(Directions.RIGHT)
+    
+def iact(gameState):
+    gameState.interact()
+    
+def errorCommand(gameState):
+    print "Not a valid command!"
+    
+def quit(gameState):
+    print "End!"
+    sys.exit()
+
+command_lookup = {K_w: up,
+                  K_a: left,
+                  K_s: down,
+                  K_d: right,
+                  K_f: iact,
+                  K_q: quit}

+ 51 - 0
src/kelciste.py

@@ -0,0 +1,51 @@
+import error
+from common import Directions
+from game import *
+import sys
+import graphics
+
+def game_loop():
+	'''Reads user input, parses it and performs it'''
+	gameState = game()
+	print gameState
+	while 1:
+		graphics.graphicsloop(gameState)
+	
+def up(gameState):
+	gameState.move(Directions.UP)
+	
+def down(gameState):
+	gameState.move(Directions.DOWN)
+	
+def left(gameState):
+	gameState.move(Directions.LEFT)
+	
+def right(gameState):
+	gameState.move(Directions.RIGHT)
+	
+def iact(gameState):
+	gameState.interact()
+	
+def errorCommand(gameState):
+	print "Not a valid command!"
+	
+def quit(gameState):
+	print "End!"
+	sys.exit()
+
+def parse_command(raw):
+	if raw in command_lookup:
+		return command_lookup[raw]
+	else:
+		return errorCommand
+
+command_lookup = {'w': up,
+				  'a': left,
+				  's': down,
+				  'd': right,
+				  'f': iact,
+				  'q': quit}
+
+if __name__ == "__main__":
+	print "Beginning!"
+	game_loop()

+ 13 - 0
src/maps/house

@@ -0,0 +1,13 @@
+House
+#objects
+entry a 6 4
+door 6 5 b main
+door 7 5 b main
+sign 5 2 Welcome to a humble abode with nothing in it!
+#map
+11111111111111
+1            1
+1            1
+1            1
+1            1
+111111  111111

+ 36 - 0
src/maps/main

@@ -0,0 +1,36 @@
+Main
+#objects
+entry a 1 1
+entry b 15 6
+entry c 22 12
+sign 2 2 Welcome to the first tests!
+sign 4 2 Glad you can move around!
+sign 6 6 This is pretty impressive, huh?
+door 15 5 a house
+door 16 5 a house
+door 23 12 a store
+#map
+11111111111111111111111111111111
+1                              1
+1         111111111111         1
+1         111111111111         1
+1         111111111111         1
+1         11111  11111         1
+1                              1
+1                              1
+1                              1
+1                      11111   1
+1                      11111   1
+1                      11111   1
+1                       1111   1
+1                      11111   1
+1                              1
+1                              1
+1      wwwwwwww                1
+1    wwwwwwwwwwwwwwwww         1
+1    wwwwwwwwwwwwwwwwww        1
+1         wwwwwwwwww           1
+1              wwwwww          1
+1                              1
+1                              1
+11111111111111111111111111111111

+ 12 - 0
src/maps/store

@@ -0,0 +1,12 @@
+Store
+#objects
+entry a 6 4
+door 6 5 c main
+door 7 5 c main
+#map
+11111111111111
+1            1
+1            1
+1            1
+1            1
+111111  111111

+ 9 - 0
src/models/block.pmd

@@ -0,0 +1,9 @@
+#block.pmd
+0 0 0
+2 0 0
+2 2 0
+0 2 0
+0 0 3
+2 0 3
+2 2 3
+0 2 3

+ 5 - 0
src/models/door.pmd

@@ -0,0 +1,5 @@
+#door
+.1 .1 1
+1.9 .1 1
+1.9 1.9 1
+.1 1.9 1

+ 5 - 0
src/models/player.pmd

@@ -0,0 +1,5 @@
+#player
+.2 .2 2
+1.8 .2 2
+1.8 1.8 2
+.2 1.8 2

+ 18 - 0
src/models/sign.pmd

@@ -0,0 +1,18 @@
+#top bit
+0 0.8 2
+2 0.8 2
+2 0.8 3
+0 0.8 3
+0 1.2 2
+2 1.2 2
+2 1.2 3
+0 1.2 3
+#stem
+0.8 0.8 0
+1.2 0.8 0
+1.2 0.8 2
+0.8 0.8 2
+0.8 1.2 0
+1.2 1.2 0
+1.2 1.2 2
+0.8 1.2 2

+ 5 - 0
src/models/tile.pmd

@@ -0,0 +1,5 @@
+#tile
+0 0 0
+2 0 0
+2 2 0
+0 2 0