1 /*
2 Copyright (c) 2017-2018 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.graphics.fpview;
29 
30 import dlib.core.memory;
31 import dlib.math.vector;
32 import dlib.math.matrix;
33 
34 import dagon.core.libs;
35 import dagon.core.ownership;
36 import dagon.core.event;
37 import dagon.graphics.fpcamera;
38 import dagon.graphics.view;
39 import dagon.logics.entity;
40 import dagon.logics.controller;
41 
42 class FirstPersonView: EventListener, View
43 {
44     FirstPersonCamera camera;
45     int oldMouseX = 0;
46     int oldMouseY = 0;
47     bool _active = false;
48 
49     float mouseSensibility = 0.2f;
50 
51     this(EventManager emngr, Entity camEntity, Owner owner)
52     {
53         super(emngr, owner);
54 
55         camera = New!FirstPersonCamera(camEntity);
56         camEntity.controller = camera;
57     }
58 
59     void update(double dt)
60     {
61         processEvents();
62 
63         if (_active)
64         {
65             float turn_m =  (eventManager.mouseRelX) * mouseSensibility;
66             float pitch_m = (eventManager.mouseRelY) * mouseSensibility;
67 
68             camera.pitch += pitch_m;
69             camera.turn += turn_m;
70 
71             camera.weaponPitch += pitch_m * camera.weaponPitchCoef;
72 
73             float pitchLimitMax = 60.0f;
74             float pitchLimitMin = -60.0f;
75             if (camera.pitch > pitchLimitMax)
76             {
77                 camera.pitch = pitchLimitMax;
78                 camera.weaponPitch = pitchLimitMax * camera.weaponPitchCoef;
79             }
80             else if (camera.pitch < pitchLimitMin)
81             {
82                 camera.pitch = pitchLimitMin;
83                 camera.weaponPitch = pitchLimitMin * camera.weaponPitchCoef;
84             }
85         }
86 
87         camera.update(dt);
88     }
89 
90     void active(bool v)
91     {
92         if (v)
93         {
94             oldMouseX = eventManager.mouseX;
95             oldMouseY = eventManager.mouseY;
96             SDL_SetRelativeMouseMode(SDL_TRUE);
97         }
98         else
99         {
100             SDL_SetRelativeMouseMode(SDL_FALSE);
101             eventManager.setMouse(oldMouseX, oldMouseY);
102         }
103 
104         _active = v;
105     }
106 
107     bool active()
108     {
109         return _active;
110     }
111 
112     Matrix4x4f viewMatrix()
113     {
114         return camera.viewMatrix();
115     }
116 
117     Matrix4x4f invViewMatrix()
118     {
119         return camera.invViewMatrix();
120     }
121 
122     Vector3f cameraPosition()
123     {
124         return camera.position;
125     }
126 }