rendered paste body#twisted
from twisted.internet import reactor, protocol
#pyglet
import pyglet
from pyglet.gl import *
from pyglet.window import key
from random import randint as rint
#builtin
import math, cPickle
#window size
wd, h = 640, 480
#debug
class Client(protocol.Protocol):
buffer = ""
def connectionMade(self):
self.transport.write("MAP")
def dataReceived(self, data):
if data.count("\0temp")>0:
data = data.split("\0temp")
last = data[0]
self.buffer += last
self.update()
self.transport.loseConnection()
else:
self.buffer += data
def update(self):
global a
print 'Found nulltemp, initing'
a.m.map = cPickle.loads(self.buffer)
a.init_map()
self.buffer = ""
#self.transport.loseConnection()
def connectionLost(self, reason):
print "connection lost"
class Factory(protocol.ClientFactory):
protocol = Client
# def startedConnecting(self,connector):
# reactor.stop()
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop()
#map class
class Map:
def __init__(self):
self.map = [ [0 for i in range(256)] for j in range(256)]
def loadMap(self):
height = pyglet.image.load('height.png')
for i in range(256):
for j in range(256):
pix = height.get_region(0,0,256,256).get_region(i,j,1,1).get_image_data().get_data("RGB", 3)
r = ord(pix[0])
g = ord(pix[1])
self.map[i][j] = r*0.5 + g*2
def get(self,i,j):
return [i,self.map[i][j],j]
class App:
def __init__(self,m):
global wd, h
if m != 0:
#print m
self.m = cPickle.loads(m)
else:
self.m = Map()
#messages
self.chat_msg = ""
self.chatting = 0
self.chat_label = pyglet.text.Label('',
font_name='Times New Roman',
font_size=14,
x=20,y=20)
#graphics
self.window = pyglet.window.Window()
self.tile = pyglet.image.load('tile.png')
self.tile = self.tile.get_texture()
glMatrixMode(GL_MODELVIEW)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glShadeModel(GL_FLAT)
self.tile_domain = pyglet.graphics.vertexdomain.create_domain('v3f','t2f')
self.init_map()
def init_map(self):
glBindTexture(self.tile.target, self.tile.id)
for i in range(254):
for j in range(254):
v = []
for c in self.m.get(i,j):
v.append(c)
for c in self.m.get(i,j+1):
v.append(c)
for c in self.m.get(i+1,j+1):
v.append(c)
for c in self.m.get(i+1,j):
v.append(c)
t = [0,0, 0,1, 1,1, 1,0]
l = self.tile_domain.create(4)
l.vertices = v
l.tex_coords = t
self.pl = Player(1,1,1,0,0,0)
def set2d(self):
global wd, h
glDisable(GL_DEPTH_TEST)
# store the projection matrix to restore later
glMatrixMode(GL_PROJECTION)
glPushMatrix()
# load orthographic projection matrix
glLoadIdentity()
far = -8192
glOrtho(-wd / 2., wd / 2., -h / 2., h / 2., 0, far)
# reset modelview
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def unSet2d(self):
# load back the projection matrix saved before
glMatrixMode(GL_PROJECTION)
glPopMatrix()
# self.cam.x -= float(math.sin(self.cam.ry/180*3.141592654))
# self.cam.z += float(math.cos(self.cam.ry/180*3.141592654))
def findHeight(self,x,z):
x = -x
z = -z
tx = int(x)
tz = int(z)
xt = x-tx
zt = z-tz
a = self.m.get(tx,tz)[1]
b = self.m.get(tx+1,tz)[1]
c = self.m.get(tx,tz+1)[1]
d = self.m.get(tx+1,tz+1)[1]
y = (a*(1-xt)+b*xt)*(1-zt) + (c*(1-xt) + d*xt)*zt
return -y
def draw(self):
self.window.clear()
self.tile_domain.draw(GL_QUADS)
self.set2d()
self.chat_label.draw()
self.unSet2d()
def update(self,dt):
self.chat_label.text = self.chat_msg
self.pl.cam.placeKeepRot(self.pl.cam.x,self.findHeight(self.pl.cam.x,self.pl.cam.z)-1,self.pl.cam.z)
self.pl.update()
# self.cam.placeKeepRot(self.cam.x,self.findHeight(self.cam.x,self.cam.z)-1,self.cam.z)
# self.cam.relMove(0,0,0.2,1)
class Player:
def __init__(self,x,y,z,rx,ry,rz):
self.x,self.y,self.z = x,y,z
self.rx,self.ry,self.rz = rx,ry,rz
self.height = 10
self.cam = Camera()
self.dx = 0
self.dy = 0
self.dz = 0
def update(self):
reactor.iterate()
self.relMove(self.dx,self.dy,self.dz)
#self.relMove(0,0,0.2)
if self.cam.x != self.x or self.cam.y != self.y or self.cam.z != self.z \
or self.cam.rx != self.rx or self.cam.ry != self.ry or self.cam.rz != self.rz:
self.x = self.cam.x
self.y = self.cam.y
self.z = self.cam.z
self.rx = self.cam.rx
self.ry = self.cam.ry
self.rz = self.cam.rz
#print 'synced pl'
def relMove(self,x,y,z):
self.cam.sideMove(x)
self.cam.forwardMove(z)
class Camera:
def __init__(self):
self.x = 0
self.y = 0
self.z = 0
self.rx = 0
self.ry = 0
self.rz = 0
self.reset()
def reset(self):
global wd, h
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45,wd/float(h),.1,1000)
self.x = 0
self.y = 0
self.z = 0
self.rx = 0
self.ry = 0
self.rz = 0
def translate(self,x,y,z):
glMatrixMode(GL_PROJECTION)
glTranslated(x,y,z)
self.x += x
self.y += y
self.z += z
def rotate(self,rx,ry,rz):
glMatrixMode(GL_PROJECTION)
glRotated(rx,1,0,0)
glRotated(ry,0,1,0)
glRotated(rz,0,0,1)
self.rx += rx
self.ry += ry
self.rz += rz
def placeKeepRot(self,x,y,z):
rtx,rty,rtz = self.rx, self.ry, self.rz
self.reset()
self.rotate(rtx,rty,rtz)
self.translate(x,y,z)
def place(self,x,y,z,rx,ry,rz):
tx,ty,tz = self.x, self.y, self.z
rtx,rty,rtz = self.rx, self.ry, self.rz
self.reset()
self.rotate(rtx+rx,rty+ry,rtz+rz)
self.translate(tx+x,ty+y,tz+z)
def forwardMove(self,t):
self.place(-float(math.sin(self.ry/180*3.141592654))*t,0,float(math.cos(self.ry/180*3.141592654))*t,0,0,0)
def sideMove(self,t):
self.place(-float(math.cos((self.ry)/180*3.141592654))*t,0,-float(math.sin((self.ry)/180*3.141592654))*t,0,0,0)
a = App(0)
@a.window.event
def on_draw():
# print 'drawn'
a.draw()
@a.window.event
def on_resize(width, height):
global wd, h
wd = width
h = height
glViewport(0, 0, width, height)
a.pl.cam.reset()
a.pl.cam.place(-5,-5,-5,0,180,0)
glMatrixMode(GL_MODELVIEW)
return pyglet.event.EVENT_HANDLED
@a.window.event
def on_key_press(symbol, modifiers):
if a.chatting == 0:
if symbol == pyglet.window.key.W:
a.pl.dz = 0.2
elif symbol == pyglet.window.key.S:
a.pl.dz = -0.2
if symbol == pyglet.window.key.A:
a.pl.dx = -0.2
elif symbol == pyglet.window.key.D:
a.pl.dx = 0.2
if symbol == pyglet.window.key.ENTER:
a.chatting = 1
else:
if symbol == pyglet.window.key.ENTER:
#a.showMsg()
a.chatting = 0
else:
pass
#a.chat_msg += symbol
@a.window.event
def on_key_release(symbol, modifiers):
if symbol == pyglet.window.key.W:
a.pl.dz = 0
elif symbol == pyglet.window.key.S:
a.pl.dz = 0
if symbol == pyglet.window.key.A:
a.pl.dx = 0
elif symbol == pyglet.window.key.D:
a.pl.dx = 0
@a.window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
a.pl.cam.place(0,0,0,-dy*0.2,dx*0.2,0)
@a.window.event
def on_text(text):
print text
if a.chatting:
a.chat_msg += text
#init map
print 'connecting'
f = Factory()
reactor.connectTCP("localhost", 8000, f)
reactor.run()
reactor.iterate()
print 'connected'
#pyglet running
pyglet.clock.schedule_interval(a.update,0.1)
pyglet.app.run()