Example #1
0
 def layout_staggered(self):
     if len(self) > 0:
         if self.focus_idx == -1: self.focus_idx = len(self)-1
         cards = self.cards
         w, h = cards[0].width, cards[0].height
         radius = h*8#1.5
         incr_arc = math.pi/180*-0.75 #1
         extra_arc = math.pi/180*-4.5 #10 #-27
         Q = euclid.Quaternion.new_rotate_axis(incr_arc*(self.focus_idx-1)+extra_arc, euclid.Vector3(0,0,-1))
         i = 0.001
         y_incr = (self.focus_idx-1)*h*0.1*self.small_size
         size = self.small_size
         for card in cards[:self.focus_idx]:
             card.orientation = Q
             card.pos = Q*euclid.Vector3(0,radius,0) - euclid.Vector3(0,radius+y_incr,i)
             card.size = size
             Q.rotate_axis(incr_arc, euclid.Vector3(0,0,1))
             y_incr -= h*0.1*size
             #if size < self.small_size: size += 0.05
             i += 0.001
         card = cards[self.focus_idx]
         card.pos = euclid.Vector3(0,0,i)
         i += 0.001
         card.orientation = euclid.Quaternion()
         card.size = self.focus_size
         Q = euclid.Quaternion.new_rotate_axis(-extra_arc, euclid.Vector3(0,0,-1))
         y_incr += h*0.1*self.small_size
         for card in cards[self.focus_idx+1:]:
             card.orientation = Q
             card.pos = Q*euclid.Vector3(0,radius,0) - euclid.Vector3(0,radius+y_incr,i)
             card.size = self.small_size
             Q.rotate_axis(incr_arc, euclid.Vector3(0,0,1))
             y_incr += h*0.1*self.small_size
             i += 0.001
Example #2
0
 def unfocused_layout(self):
     numhand = len(self.cards)
     #self.hand_size.set_text(numhand)
     #if not self.is_opponent: self.hand_size.pos = euclid.Vector3(self.avail_width-10, 0, 0)
     #else: self.hand_size.pos = euclid.Vector3(10, 0, 0)
     #avail_width = self.avail_width - self.hand_size.width - 20
     avail_width = self.avail_width - 20
     size = self.unfocused_size[1]
     if numhand > 0:
         self.visible = 1.0
         cardwidth = self.cards[0].width
         # First lay out, then overlap, and then scale
         spacing = self.unfocused_spacing
         numhand += .5
         while (numhand*cardwidth*size*spacing) - avail_width > 0.005:
             # Figure out the spacing that will fit
             spacing = avail_width / (numhand*cardwidth*size)
             if spacing < 0.7:
                 spacing = self.unfocused_spacing
                 size -= 0.005
         x_incr = cardwidth*size*spacing*self.dir
         x, y = avail_width-cardwidth*size/2, 0
         #if not self.is_opponent: x, y = avail_width-cardwidth*size/2, 0
         #else: x, y = self.hand_size.width + cardwidth*size/2 + 20, 0
         z = 0
         for card in self.cards:
             card.size = size
             card.orientation = euclid.Quaternion()
             card._pos.set_transition(dt=0.8, method="sine")
             card.pos = euclid.Vector3(x,y,z)
             x -= x_incr
     else:
         self.visible = 0.0
     self.box = (0, -self.height/2, self.avail_width, self.height/2)
Example #3
0
 def layout_original(self):
     if len(self) > 0:
         if self.focus_idx == -1: self.focus_idx = len(self)-1
         cards = self.cards
         w, h = cards[0].width, cards[0].height
         radius = h*1.5
         incr_arc = math.pi/180*-2
         extra_arc = math.pi/180*-27
         Q = euclid.Quaternion.new_rotate_axis(incr_arc*(self.focus_idx-1)+extra_arc, euclid.Vector3(0,0,-1))
         i = 0.001
         for card in cards[:self.focus_idx]:
             card.orientation = Q
             card.pos = Q*euclid.Vector3(0,radius,0) - euclid.Vector3(0,radius,i)
             card.size = self.small_size
             Q.rotate_axis(incr_arc, euclid.Vector3(0,0,1))
             i += 0.001
         card = cards[self.focus_idx]
         card.pos = euclid.Vector3(0,0,i)
         i += 0.001
         card.orientation = euclid.Quaternion()
         card.size = self.focus_size
         Q = euclid.Quaternion.new_rotate_axis(-extra_arc, euclid.Vector3(0,0,-1))
         for card in cards[self.focus_idx+1:]:
             card.orientation = Q 
             card.pos = Q*euclid.Vector3(0,radius,0) - euclid.Vector3(0,radius,i)
             card.size = self.small_size
             Q.rotate_axis(incr_arc, euclid.Vector3(0,0,1))
             i += 0.001
Example #4
0
 def __init__(self, pos):
     self._pos = AnimatedVector3(pos)
     self._orientation = AnimatedQuaternion()
     #self.viewangle = -7*math.pi/16
     #self.viewangle = -15*math.pi/32
     self.viewangle = -127 * math.pi / 256
     self._orientation.set(euclid.Quaternion().rotate_axis(
         self.viewangle, euclid.Vector3(1, 0, 0)))
     self._orientation.set_transition(dt=0.5, method="sine")
     self.view_switched = False
     self.vis_distance = 6.5
     self.x_limit = (-20, 20)
     self.y_limit = (15, 40)
     self.z_limit = (-20, 20)
Example #5
0
 def __init__(self,
              screenDim=(4, 4),
              imageDim=(64, 64),
              translation=euclid.Point3(0., 0., 0.),
              rotation=euclid.Quaternion(),
              focallength=1.0):
     self.translation = translation
     self.rotation = rotation
     self.focallength = focallength  # distance from camera to screen
     # Screen width is the real-world width/height of the screen
     self.screenw = screenDim[0]
     self.screenh = screenDim[1]
     # Image width is the number of pixels in the screen
     self.imagew = imageDim[0]
     self.imageh = imageDim[1]
 def open(self):
     self.closed = False
     self.origin = self.request.headers.get("origin", "")
     self.userAgent = self.request.headers.get("user-agent")
     print "connection", self.request.remote_ip, self.origin, self.userAgent
     if self.origin != options.origin and not \
         self.origin.startswith("http://31.192.226.244:") and not \
         self.origin.startswith("http://localhost:"):
         print "kicking out bad origin"
         self.write_message(
             '{"chat":[{"Will":"if you fork the code, you need to run your own server!"}]}'
         )
         self.close()
     chat = [
         dict([[
             "%d fps" % options.fps,
             "(lock step frame-rate with all players)"
         ]])
     ]
     if options.welcome:
         chat.append({"welcome": options.welcome})
     self.write_message(json.dumps({"chat": chat}))
     self.lastMessage = time.time()
     self.keys = set()
     self.pos = euclid.Vector3(random.uniform(-.5, .5),
                               random.uniform(-.5, .5),
                               random.uniform(-.5, .5))
     self.rot = euclid.Quaternion().rotate_euler(random.uniform(-.5, .5),
                                                 random.uniform(-.5, .5),
                                                 random.uniform(
                                                     -.5, .5)).normalized()
     self.speed = random.random() * max_speed
     self.roll_speed = self.pitch_speed = self.yaw_speed = 0
     self.firing = 0
     self.killed_by = None
     self.game.add_client(self)
     print self.name, "joined;", len(self.game.clients), "players"
 def run(self):
     # time out old clients
     stale = time.time() - 3  # 3 secs
     for client in self.clients.copy():
         if client.lastMessage < stale:
             print "timing out", client.name, client.lastMessage - time.time(
             )
             self.remove_client(client, "ping timeout")
     # move simulation onwards?
     while self.tick + self.tick_length <= self.now():
         updates, deaths = [], set()
         for client in self.clients:
             # roll
             if 37 in client.keys: client.roll_speed += roll_speed
             if 39 in client.keys: client.roll_speed -= roll_speed
             client.roll_speed = max(-max_roll_speed,
                                     min(max_roll_speed, client.roll_speed))
             if 37 not in client.keys and 39 not in client.keys:
                 client.roll_speed *= .9
             if math.fabs(client.roll_speed) < epslion:
                 client.roll_speed = 0
             # pitch
             if 38 in client.keys: client.pitch_speed -= pitch_speed
             if 40 in client.keys: client.pitch_speed += pitch_speed
             client.pitch_speed = max(
                 -max_pitch_speed, min(max_pitch_speed, client.pitch_speed))
             if 38 not in client.keys and 40 not in client.keys:
                 client.pitch_speed *= .9
             if math.fabs(client.pitch_speed) < epslion:
                 client.pitch_speed = 0
             # yaw
             if 65 in client.keys: client.yaw_speed += yaw_speed
             if 68 in client.keys: client.yaw_speed -= yaw_speed
             client.yaw_speed = max(-max_yaw_speed,
                                    min(max_yaw_speed, client.yaw_speed))
             if 65 not in client.keys and 68 not in client.keys:
                 client.yaw_speed *= .9
             if math.fabs(client.yaw_speed) < epslion: client.yaw_speed = 0
             # apply
             client.rot *= euclid.Quaternion().rotate_euler(
                 client.yaw_speed, client.roll_speed, client.pitch_speed)
             client.rot.normalize()
             # speed
             if 83 in client.keys: client.speed -= speed
             if 87 in client.keys: client.speed += speed
             client.speed = max(0., min(max_speed,
                                        client.speed))  # no going backwards
             if 83 not in client.keys and 87 not in client.keys:
                 client.speed *= .9
             if math.fabs(client.speed) < epslion: client.speed = 0
             move = ((client.rot * forward) * client.rot.conjugated())
             move = euclid.Vector3(move.x, move.y,
                                   move.z).normalized() * client.speed
             client.pos += move
             # are we out-of-bounds?
             ### ideally bounce etc, but for now we'll just slide along it
             extreme = .85
             if client.pos.x < -extreme: client.pos.x = -extreme
             if client.pos.x > extreme: client.pos.x = extreme
             if client.pos.y < -extreme: client.pos.y = -extreme
             if client.pos.y > extreme: client.pos.y = extreme
             if client.pos.z < -extreme: client.pos.z = -extreme
             if client.pos.z > extreme: client.pos.z = extreme
             # shooting
             if 32 in client.keys:
                 client.firing += 1
                 if client.firing < 5:
                     self.shots.append(Shot(client))
             else:
                 client.firing = 0
             #print client.name, client.keys, client.roll_speed, client.pitch_speed, client.speed, client.rot, client.pos, move
         for shot in self.shots[:]:
             hit, distance = shot.tick(self.clients)
             if hit:
                 deaths.add(hit)
                 hit.killed_by = shot.client.name
             if hit or not shot.age:
                 self.shots.remove(shot)
         for client in self.clients:
             updates.append({
                 "name":
                 client.name,
                 "pos": (client.pos.x, client.pos.y, client.pos.z),
                 "rot":
                 (client.rot.x, client.rot.y, client.rot.z, client.rot.w),
                 "speed":
                 client.speed,
             })
         for client in self.clients:
             client.write_message(
                 json.dumps({
                     "tick":
                     self.tick,
                     "updates":
                     updates,
                     "shots": [{
                         "pos": (shot.pos.x, shot.pos.y, shot.pos.z),
                         "vec": (shot.vec.x, shot.vec.y, shot.vec.z),
                     } for shot in self.shots],
                 }))
         for dead in deaths:
             self.remove_client(dead, "died, shot by %s" % dead.killed_by)
         self.tick += self.tick_length
from tornado.options import define, options, parse_command_line
import euclid

define("port", default=8888, type=int)
define("branch", default="master")
define("access", type=str, multiple=True)
define("local", type=bool)

define("welcome", type=str)
define("fps", default=8, type=int)
define("origin", default="http://williame.github.com", type=str)

num_models = 8
player_size = 0.025
epslion = 0.0001
forward = euclid.Quaternion(0., 0., 0., -1.)


def set_fps(fps):
    global ticks_per_sec
    global roll_speed, max_roll_speed
    global pitch_speed, max_pitch_speed
    global yaw_speed, max_yaw_speed
    global speed, max_speed
    global shot_length, max_shot_age
    ticks_per_sec = fps
    base_speed = (.0015 * 8) / ticks_per_sec
    base_max = (.05 * 8) / ticks_per_sec
    roll_speed = base_speed
    max_roll_speed = base_max
    pitch_speed = base_speed