Skip to content

Commit

Permalink
Merge pull request #45 from xFrednet/mouse-movement
Browse files Browse the repository at this point in the history
Mouse movement
  • Loading branch information
rwxzig committed Oct 12, 2020
2 parents b78327b + 6c561d3 commit 315143a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 2 additions & 0 deletions a03_3d-maze/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
| Return to Home | `[H]` |
| Debug information | `[P]` |

You can also use the mouse to rotate the view :)

## Installing and running the game
This game uses:
* PyOpenGL and pygame for rendering
Expand Down
25 changes: 14 additions & 11 deletions a03_3d-maze/src/control_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,28 @@ def _wasd_movement(self, entity_id, speed, vertical_movement, vertical_speed):
print(f"Transformation(Position: {tra.position}, Rotation: {tra.rotation})")

def _mouse_control(self, entity_id, enable_pitch=True):
controls: res.GameControlState = self.world.controls
screen_center = self.world.resolution / 2.0

# If python breaks on this try updating pygame :D
(is_pressed, _, _, _, _) = pygame.mouse.get_pressed()
transformation = self.world.component_for_entity(entity_id, com.Transformation)
(rel_x, rel_y) = pygame.mouse.get_rel()
if is_pressed:
transformation = self.world.component_for_entity(entity_id, com.Transformation)
#(rel_x, rel_y) = pygame.mouse.get_rel()
(pos_x, pos_y) = pygame.mouse.get_pos()
rel_x = screen_center.x - pos_x
rel_y = screen_center.y - pos_y

if enable_pitch:
pitch_change = 0.0
if rel_y > 0:
pitch_change += 0.01 # I think it is too fast with change 0.1
if rel_y < 0:
pitch_change -= 0.01
pitch_change = rel_y * controls.mouse_sensitivity
transformation.rotation.y = clamp(
transformation.rotation.y + pitch_change,
(math.pi - 0.2) / -2,
(math.pi - 0.2) / 2)

if rel_x < 0:
transformation.rotation.x += 0.1
if rel_x > 0:
transformation.rotation.x -= 0.1
transformation.rotation.x += rel_x * controls.mouse_sensitivity

pygame.mouse.set_pos([screen_center.x, screen_center.y])

def _arrow_key_rotation(self, entity_id, enable_pitch=True):
transformation = self.world.component_for_entity(entity_id, com.Transformation)
Expand Down
4 changes: 3 additions & 1 deletion a03_3d-maze/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import glm
import pygame
import pygame.display
from world import World

RESOLUTION = 1024, 720
Expand Down Expand Up @@ -41,7 +42,8 @@ def main():
pygame.init()
pygame.display.init()
pygame.display.set_mode(RESOLUTION, pygame.DOUBLEBUF | pygame.OPENGL)
pygame.display.set_caption("Le 3D maze of time")
pygame.display.set_caption("Le maze: 3D-Packman - just a bit worse")
pygame.mouse.set_visible(False)
running = True
world = World(glm.vec2(RESOLUTION), LEVEL)

Expand Down
2 changes: 2 additions & 0 deletions a03_3d-maze/src/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(self):
self.key_return_to_home = pygame.locals.K_h
self.key_return_to_home_state = False

self.mouse_sensitivity = 0.0025

self.player_speed = 10.0
self.player_jump_height = 15.0
self.free_camera_speed = 10.0
Expand Down

0 comments on commit 315143a

Please sign in to comment.