Skip to content
Permalink
Browse files

Mouse controls

  • Loading branch information
rdb committed Mar 26, 2020
1 parent 6d76589 commit 063bf180a78f873302ed7b707c514455c269106a
Showing with 31 additions and 11 deletions.
  1. +3 −3 game/__init__.py
  2. +28 −8 game/controls.py
@@ -45,7 +45,7 @@ def __init__(self):
},
),
Controls(),
Speed(min=0.0, max=5.0),
Speed(min=3.0, max=5.0),
Collider(solid=core.CollisionSphere((0, 0, 0), 1.0), from_mask=1, into_mask=0),
name="player",
)
@@ -82,7 +82,7 @@ def __init__(self):
},
),
SfxPlayer(sounds=['flower-open-a', 'flower-open-b', 'thorns'], volume=1000),
Collider(solid=core.CollisionSphere((0, 0, 1.25), 0.25)),
Collider(solid=core.CollisionSphere((0, 0, 1.25), 1)),
name="flower",
)
self.flowers.append(flower)
@@ -133,7 +133,7 @@ def handle_collision(self, entry):
flower[Character].state = 'open'
flower[SfxPlayer].play(choice(['flower-open-a', 'flower-open-b']))

self.player[Speed].current = 0.0
#self.player[Speed].current = 0.0
#self.player[Controls].enabled = False

#self.player[TerrainObject]._root.hprInterval(4, (360, 0, 0), blendType='easeInOut').start()
@@ -6,13 +6,14 @@

from .terrain import TerrainObject
from .general import Speed
from .animation import Character

import math


@Component()
class Controls:
forward: str = 'raw-w'
forward: str = 'mouse1'
left: str = 'raw-a'
backward: str = 'raw-s'
right: str = 'raw-d'
@@ -21,7 +22,7 @@ class Controls:
deceleration: float = 2.0

# degrees per second
turn_speed: float = 45.0
turn_speed: float = 90

enabled: bool = True

@@ -34,6 +35,10 @@ class PlayerController(System, DirectObject):
def __init__(self):
System.__init__(self)

self.last_ptr = None

base.win.request_properties(core.WindowProperties(mouse_mode=core.WindowProperties.M_confined))

def init_entity(self, filter_name, entity):
controls = entity[Controls]
controls._states = {}
@@ -61,13 +66,28 @@ def update(self, entities_by_filter):
controls = entity[Controls]
speed = entity[Speed]

if controls.enabled:
if controls._states['forward'] or controls._states['backward']:
speed.accelerate((controls._states['forward'] - controls._states['backward']) * controls.acceleration)
else:
speed.accelerate(-controls.deceleration)

dt = globalClock.dt

ptr = base.win.get_pointer(0)
if ptr.in_window:
if self.last_ptr:
lean = ptr.x - base.win.get_x_size() / 2
lean_norm = lean / base.win.get_x_size()
obj.direction -= lean_norm * controls.turn_speed * dt
base.cam.set_x(lean_norm * 3)

elevate = (ptr.y / base.win.get_y_size()) - 0.5
base.cam.set_z(2.5 - elevate)

self.last_ptr = ptr
else:
self.last_ptr = None

if controls.enabled and (controls._states['forward'] or controls._states['backward']):
speed.accelerate((controls._states['forward'] - controls._states['backward']) * controls.acceleration)
else:
speed.accelerate(-controls.deceleration)

turn = controls._states['right'] - controls._states['left']
obj.direction -= turn * controls.turn_speed * dt

0 comments on commit 063bf18

Please sign in to comment.
You can’t perform that action at this time.