main.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import pyglet
  2. from pyglet.gl import *
  3. from pyglet.window import key
  4. import math
  5. import os
  6. from PIL import Image
  7. from anarchmap import AnarchMap
  8. assets_folder = '../anarch/assets/'
  9. class LevelModel:
  10. def get_tex(self,file):
  11. tex = pyglet.image.load(file).get_texture()
  12. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  13. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  14. return pyglet.graphics.TextureGroup(tex)
  15. def get_texture(self, textureIndex):
  16. tex = pyglet.image.load()
  17. def add_block(self, x, y, z, wall_tex, ceil_color=0, floor_color=0):
  18. X, Y, Z = x+1, y+1, z+1
  19. tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1))
  20. 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
  21. 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
  22. 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
  23. 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
  24. self.batch.add(4, GL_QUADS, self.bottom, ('v3f', (x, y, z, X, y, z, X, y, Z, x, y, Z)), tex_coords) # bottom
  25. self.batch.add(4, GL_QUADS, self.top, ('v3f', (x, Y, Z, X, Y, Z, X, Y, z, x, Y, z)), tex_coords) # top
  26. def __init__(self, basepath):
  27. self.base_path = basepath
  28. self.top = self.get_tex(os.path.join(self.base_path, 'wall_texture0.png'))
  29. self.bottom = self.get_tex(os.path.join(self.base_path, 'wall_texture0.png'))
  30. self.batch = pyglet.graphics.Batch()
  31. # build list of wall textures
  32. idxs = range(16)
  33. filename_template = os.path.join(self.base_path, 'wall_texture{}.png')
  34. self.wall_textures = list(map(lambda x: self.get_tex(filename_template.format(x)), idxs))
  35. # self.add_block(0, 0, -1, 0)
  36. # self.add_block(0, 2, -1, 0)
  37. def load_level_image(self, file):
  38. self.ceiling_color = 0xff0000
  39. self.floor_color = 0x0000ff
  40. self.map = AnarchMap(file)
  41. for x in range(64):
  42. for y in range(64):
  43. item = self.map.levelMap[x][y]
  44. if not item[0]:
  45. continue
  46. else:
  47. tile_index = self.map.defineName(item[0])
  48. self.add_block(x, y, -1, 0)
  49. def draw(self):
  50. self.batch.draw()
  51. class Camera:
  52. def __init__(self, pos=(0, 0, 0), rot=(0, 0)):
  53. self.pos = list(pos)
  54. self.rot = list(rot)
  55. def mouse_motion(self, dx, dy):
  56. dx/= 8
  57. dy/= 8
  58. self.rot[0] += dy
  59. self.rot[1] -= dx
  60. if self.rot[0]>90:
  61. self.rot[0] = 90
  62. elif self.rot[0] < -90:
  63. self.rot[0] = -90
  64. def update(self,dt,keys):
  65. sens = 0.1
  66. s = dt*10
  67. rotY = -self.rot[1]/180*math.pi
  68. dx, dz = s*math.sin(rotY), math.cos(rotY)
  69. if keys[key.W]:
  70. self.pos[0] += dx*sens
  71. self.pos[2] -= dz*sens
  72. if keys[key.S]:
  73. self.pos[0] -= dx*sens
  74. self.pos[2] += dz*sens
  75. if keys[key.A]:
  76. self.pos[0] -= dz*sens
  77. self.pos[2] -= dx*sens
  78. if keys[key.D]:
  79. self.pos[0] += dz*sens
  80. self.pos[2] += dx*sens
  81. if keys[key.SPACE]:
  82. self.pos[1] += s
  83. if keys[key.LSHIFT]:
  84. self.pos[1] -= s
  85. class Window(pyglet.window.Window):
  86. def push(self,pos,rot):
  87. glPushMatrix()
  88. rot = self.player.rot
  89. pos = self.player.pos
  90. glRotatef(-rot[0],1,0,0)
  91. glRotatef(-rot[1],0,1,0)
  92. glTranslatef(-pos[0], -pos[1], -pos[2])
  93. def Projection(self):
  94. glMatrixMode(GL_PROJECTION)
  95. glLoadIdentity()
  96. def Model(self):
  97. glMatrixMode(GL_MODELVIEW)
  98. glLoadIdentity()
  99. def set2d(self):
  100. self.Projection()
  101. gluPerspective(0, self.width, 0, self.height)
  102. self.Model()
  103. def set3d(self):
  104. self.Projection()
  105. gluPerspective(70, self.width/self.height, 0.05, 1000)
  106. self.Model()
  107. def setLock(self, state):
  108. self.lock = state
  109. self.set_exclusive_mouse(state)
  110. lock = False
  111. mouse_lock = property(lambda self:self.lock, setLock)
  112. def __init__(self, *args, **kwargs):
  113. super().__init__(*args, **kwargs)
  114. self.set_minimum_size(300,200)
  115. self.keys = key.KeyStateHandler()
  116. self.push_handlers(self.keys)
  117. pyglet.clock.schedule(self.update)
  118. self.model = LevelModel(assets_folder)
  119. self.player = Camera((0.5,1.5,1.5),(-30,0))
  120. self.model.load_level_image('level0.gif')
  121. def on_mouse_motion(self,x,y,dx,dy):
  122. if self.mouse_lock: self.player.mouse_motion(dx,dy)
  123. def on_key_press(self, KEY, _MOD):
  124. if KEY == key.ESCAPE:
  125. self.close()
  126. elif KEY == key.E:
  127. self.mouse_lock = not self.mouse_lock
  128. def update(self, dt):
  129. self.player.update(dt, self.keys)
  130. def on_draw(self):
  131. self.clear()
  132. self.set3d()
  133. self.push(self.player.pos,self.player.rot)
  134. self.model.draw()
  135. glPopMatrix()
  136. if __name__ == '__main__':
  137. window = Window(width=400, height=300, caption='AnarchMap', resizable=True)
  138. glClearColor(0.5,0.7,1,1)
  139. glEnable(GL_DEPTH_TEST)
  140. #glEnable(GL_CULL_FACE)
  141. pyglet.app.run()