123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import pyglet
- from pyglet.gl import *
- from pyglet.window import key
- import math
- import os
- from PIL import Image
- from anarchmap import AnarchMap
- assets_folder = '../anarch/assets/'
- class LevelModel:
- def get_tex(self,file):
- tex = pyglet.image.load(file).get_texture()
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
- return pyglet.graphics.TextureGroup(tex)
-
- def get_texture(self, textureIndex):
- tex = pyglet.image.load()
- def add_block(self, x, y, z, wall_tex, ceil_color=0, floor_color=0):
- X, Y, Z = x+1, y+1, z+1
- tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1))
- self.batch.add(4, GL_QUADS, self.wall_textures[wall_tex], ('v3f', (X, y, z, x, y, z, x, Y, z, X, Y, z)), tex_coords) # back
- self.batch.add(4, GL_QUADS, self.wall_textures[wall_tex], ('v3f', (x, y, Z, X, y, Z, X, Y, Z, x, Y, Z)), tex_coords) # front
- self.batch.add(4, GL_QUADS, self.wall_textures[wall_tex], ('v3f', (x, y, z, x, y, Z, x, Y, Z, x, Y, z)), tex_coords) # left
- self.batch.add(4, GL_QUADS, self.wall_textures[wall_tex], ('v3f', (X, y, Z, X, y, z, X, Y, z, X, Y, Z)), tex_coords) # right
- self.batch.add(4, GL_QUADS, self.bottom, ('v3f', (x, y, z, X, y, z, X, y, Z, x, y, Z)), tex_coords) # bottom
- self.batch.add(4, GL_QUADS, self.top, ('v3f', (x, Y, Z, X, Y, Z, X, Y, z, x, Y, z)), tex_coords) # top
- def __init__(self, basepath):
- self.base_path = basepath
- self.top = self.get_tex(os.path.join(self.base_path, 'wall_texture0.png'))
- self.bottom = self.get_tex(os.path.join(self.base_path, 'wall_texture0.png'))
- self.batch = pyglet.graphics.Batch()
- # build list of wall textures
- idxs = range(16)
- filename_template = os.path.join(self.base_path, 'wall_texture{}.png')
- self.wall_textures = list(map(lambda x: self.get_tex(filename_template.format(x)), idxs))
- # self.add_block(0, 0, -1, 0)
- # self.add_block(0, 2, -1, 0)
- def load_level_image(self, file):
- self.ceiling_color = 0xff0000
- self.floor_color = 0x0000ff
- self.map = AnarchMap(file)
- for x in range(64):
- for y in range(64):
- item = self.map.levelMap[x][y]
- if not item[0]:
- continue
- else:
- tile_index = self.map.defineName(item[0])
- self.add_block(x, y, -1, 0)
- def draw(self):
- self.batch.draw()
- class Camera:
- def __init__(self, pos=(0, 0, 0), rot=(0, 0)):
- self.pos = list(pos)
- self.rot = list(rot)
- def mouse_motion(self, dx, dy):
- dx/= 8
- dy/= 8
- self.rot[0] += dy
- self.rot[1] -= dx
- if self.rot[0]>90:
- self.rot[0] = 90
- elif self.rot[0] < -90:
- self.rot[0] = -90
- def update(self,dt,keys):
- sens = 0.1
- s = dt*10
- rotY = -self.rot[1]/180*math.pi
- dx, dz = s*math.sin(rotY), math.cos(rotY)
- if keys[key.W]:
- self.pos[0] += dx*sens
- self.pos[2] -= dz*sens
- if keys[key.S]:
- self.pos[0] -= dx*sens
- self.pos[2] += dz*sens
- if keys[key.A]:
- self.pos[0] -= dz*sens
- self.pos[2] -= dx*sens
- if keys[key.D]:
- self.pos[0] += dz*sens
- self.pos[2] += dx*sens
- if keys[key.SPACE]:
- self.pos[1] += s
- if keys[key.LSHIFT]:
- self.pos[1] -= s
- class Window(pyglet.window.Window):
- def push(self,pos,rot):
- glPushMatrix()
- rot = self.player.rot
- pos = self.player.pos
- glRotatef(-rot[0],1,0,0)
- glRotatef(-rot[1],0,1,0)
- glTranslatef(-pos[0], -pos[1], -pos[2])
- def Projection(self):
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- def Model(self):
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- def set2d(self):
- self.Projection()
- gluPerspective(0, self.width, 0, self.height)
- self.Model()
- def set3d(self):
- self.Projection()
- gluPerspective(70, self.width/self.height, 0.05, 1000)
- self.Model()
- def setLock(self, state):
- self.lock = state
- self.set_exclusive_mouse(state)
- lock = False
- mouse_lock = property(lambda self:self.lock, setLock)
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.set_minimum_size(300,200)
- self.keys = key.KeyStateHandler()
- self.push_handlers(self.keys)
- pyglet.clock.schedule(self.update)
- self.model = LevelModel(assets_folder)
- self.player = Camera((0.5,1.5,1.5),(-30,0))
- self.model.load_level_image('level0.gif')
- def on_mouse_motion(self,x,y,dx,dy):
- if self.mouse_lock: self.player.mouse_motion(dx,dy)
- def on_key_press(self, KEY, _MOD):
- if KEY == key.ESCAPE:
- self.close()
- elif KEY == key.E:
- self.mouse_lock = not self.mouse_lock
- def update(self, dt):
- self.player.update(dt, self.keys)
- def on_draw(self):
- self.clear()
- self.set3d()
- self.push(self.player.pos,self.player.rot)
- self.model.draw()
- glPopMatrix()
- if __name__ == '__main__':
- window = Window(width=400, height=300, caption='AnarchMap', resizable=True)
- glClearColor(0.5,0.7,1,1)
- glEnable(GL_DEPTH_TEST)
- #glEnable(GL_CULL_FACE)
- pyglet.app.run()
|