Added mouse input support.

This commit is contained in:
Quantum 2013-11-01 12:30:10 -04:00
parent e0e1a619f0
commit 617d4913d7

View file

@ -17,7 +17,8 @@ except ImportError:
from punyverse.model import model_list, load_model from punyverse.model import model_list, load_model
from pyglet.gl import * from pyglet.gl import *
from pyglet.window import key from pyglet.window import key, mouse
import pyglet import pyglet
@ -45,7 +46,8 @@ class Applet(pyglet.window.Window):
cam.roll += 4 cam.roll += 4
if key.S in self.keys: if key.S in self.keys:
cam.roll -= 4 cam.roll -= 4
cam.move(self.speed) if self.moving:
cam.move(self.speed)
if self.running: if self.running:
self.world.tick += self.tick self.world.tick += self.tick
@ -63,6 +65,7 @@ class Applet(pyglet.window.Window):
self.debug = False self.debug = False
self.orbit = True self.orbit = True
self.running = True self.running = True
self.moving = True
self.tick = self.world.tick_length self.tick = self.world.tick_length
# On standard world: 10x is one day per second, 100x is 10 days, 300x is a month # On standard world: 10x is one day per second, 100x is 10 days, 300x is a month
@ -146,9 +149,24 @@ class Applet(pyglet.window.Window):
super(Applet, self).set_exclusive_mouse(exclusive) # Pass to parent super(Applet, self).set_exclusive_mouse(exclusive) # Pass to parent
self.exclusive = exclusive # Toggle flag self.exclusive = exclusive # Toggle flag
def launch_meteor(self):
c = self.cam
dx, dy, dz = c.direction()
speed = abs(self.speed) * 1.1 + 5
dx *= speed
dy *= speed
dz *= speed
self.world.tracker.append(
Asteroid(random.choice(self.asteroid_ids), (c.x, c.y - 3, c.z + 5), direction=(dx, dy, dz)))
def on_mouse_press(self, x, y, button, modifiers): def on_mouse_press(self, x, y, button, modifiers):
if not self.exclusive: if not self.exclusive:
self.set_exclusive_mouse(True) self.set_exclusive_mouse(True)
else:
if button == mouse.LEFT:
self.launch_meteor()
elif button == mouse.RIGHT:
self.moving = not self.moving
def on_mouse_motion(self, x, y, dx, dy): def on_mouse_motion(self, x, y, dx, dy):
if self.exclusive: # Only handle camera movement if mouse is grabbed if self.exclusive: # Only handle camera movement if mouse is grabbed
@ -195,14 +213,7 @@ class Applet(pyglet.window.Window):
if index >= 0: if index >= 0:
self.tick = self.ticks[index] self.tick = self.ticks[index]
elif symbol == key.SPACE: elif symbol == key.SPACE:
c = self.cam self.launch_meteor()
dx, dy, dz = c.direction()
speed = abs(self.speed) * 1.1 + 5
dx *= speed
dy *= speed
dz *= speed
self.world.tracker.append(
Asteroid(random.choice(self.asteroid_ids), (c.x, c.y - 3, c.z + 5), direction=(dx, dy, dz)))
else: else:
self.keys.add(symbol) self.keys.add(symbol)
@ -220,6 +231,9 @@ class Applet(pyglet.window.Window):
gluPerspective(45.0, width / float(height), 0.1, 50000000.0) gluPerspective(45.0, width / float(height), 0.1, 50000000.0)
glMatrixMode(GL_MODELVIEW) glMatrixMode(GL_MODELVIEW)
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
self.speed += scroll_y * 50 + scroll_x * 500
def get_time_per_second(self): def get_time_per_second(self):
if self.__time_per_second_cache == self.tick: if self.__time_per_second_cache == self.tick:
return self.__time_per_second_value return self.__time_per_second_value