浏览代码

Added controller cursor in menu

jancoow 9 年之前
父节点
当前提交
e7163fc07c
共有 10 个文件被更改,包括 202 次插入30 次删除
  1. 1 1
      ControllerHandler.cpp
  2. 38 23
      CrystalPoint.cpp
  3. 4 1
      Enemy.cpp
  4. 1 0
      Main.cpp
  5. 89 0
      Matrix.cpp
  6. 26 0
      Matrix.h
  7. 1 0
      Vector.cpp
  8. 38 4
      Weapon.cpp
  9. 1 0
      Weapon.h
  10. 3 1
      World.cpp

+ 1 - 1
ControllerHandler.cpp

@@ -121,7 +121,7 @@ void ControllerHandler::commandControllerData(std::vector<std::string> data) {
             c->joystick.x = std::stoi(data[2])/2000.0f;
             c->joystick.x = std::stoi(data[2])/2000.0f;
             c->joystick.y = std::stoi(data[3])/2000.0f;
             c->joystick.y = std::stoi(data[3])/2000.0f;
             c->joystickButton = !(data[4] == "0");
             c->joystickButton = !(data[4] == "0");
-
+            c->button = !(data[8] == "0");
             c->magnetSwitch = !(data[9] == "0");
             c->magnetSwitch = !(data[9] == "0");
         }
         }
     }
     }

+ 38 - 23
CrystalPoint.cpp

@@ -65,7 +65,10 @@ void CrystalPoint::update()
 
 
 	if (keyboardState.keys[27] && !prevKeyboardState.keys[27])
 	if (keyboardState.keys[27] && !prevKeyboardState.keys[27])
 		state = !state;
 		state = !state;
-		
+
+	Controller *rightcontroller = controller.getRightController();
+	Controller *leftcontroller = controller.getLeftController();
+
 	if (state)
 	if (state)
 	{
 	{
 		if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
 		if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
@@ -100,42 +103,43 @@ void CrystalPoint::update()
 		if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
 		if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
 		if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
 		if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
 
 
-		Controller *leftcontroller = controller.getLeftController();
 		if (leftcontroller != nullptr) {
 		if (leftcontroller != nullptr) {
 			Vec2f *leftControllerJoystick = &leftcontroller->joystick;
 			Vec2f *leftControllerJoystick = &leftcontroller->joystick;
 
 
-
 			if (leftcontroller->joystickButton) {
 			if (leftcontroller->joystickButton) {
 				controller.rumble(leftcontroller->controllerId, 100, 100);
 				controller.rumble(leftcontroller->controllerId, 100, 100);
 			}
 			}
 
 
-
-			if (leftControllerJoystick->y > 0.3) {
-				player->setPosition(270, leftControllerJoystick->y * deltaTime * 2.0f, false);
-			}
-			else if (leftControllerJoystick->y < -0.3) {
-				player->setPosition(90, leftControllerJoystick->y * -1 * deltaTime * 2.0f, false);
+			if (leftControllerJoystick->y > 0.3 || leftControllerJoystick->y < -0.3) {
+				player->rotation.x += leftControllerJoystick->y/4;
 			}
 			}
-			if (leftControllerJoystick->x > 0.3) {
-				player->setPosition(180, leftControllerJoystick->x * deltaTime * 2.0f, false);
-			}
-			else if (leftControllerJoystick->x < -0.3) {
-				player->setPosition(0, leftControllerJoystick->x * -1 * deltaTime * 2.0f, false);
+
+			if (leftControllerJoystick->x > 0.3 || leftControllerJoystick->x < -0.3) {
+				player->rotation.y += leftControllerJoystick->x/4;
 			}
 			}
 
 
 			player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
 			player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
 
 
+			if(leftcontroller->button && leftcontroller->joystickButton){
+				state = !state;
+			}
 		}
 		}
-		Controller *rightcontroller = controller.getRightController();
 		if(rightcontroller != nullptr){
 		if(rightcontroller != nullptr){
 			Vec2f *rightControllerJoystick = &rightcontroller->joystick;
 			Vec2f *rightControllerJoystick = &rightcontroller->joystick;
-			if (rightControllerJoystick->y > 0.3 || rightControllerJoystick->y < -0.3) {
-				player->rotation.x += rightcontroller->joystick.y/4;
-			}
 
 
-			if (rightControllerJoystick->x > 0.3 || rightControllerJoystick->x < -0.3) {
-				player->rotation.y += rightcontroller->joystick.x/4;
+			if (rightControllerJoystick->y > 0.3) {
+				player->setPosition(270, rightControllerJoystick->y * deltaTime * 2.0f, false);
+			}
+			else if (rightControllerJoystick->y < -0.3) {
+				player->setPosition(90, rightControllerJoystick->y * -1 * deltaTime * 2.0f, false);
 			}
 			}
+			if (rightControllerJoystick->x > 0.3) {
+				player->setPosition(180, rightControllerJoystick->x * deltaTime * 2.0f, false);
+			}
+			else if (rightControllerJoystick->x < -0.3) {
+				player->setPosition(0, rightControllerJoystick->x * -1 * deltaTime * 2.0f, false);
+			}
+
             player->rightWeapon->rotateWeapon(Vec3f(rightcontroller->ypr.y + 140, 0, -rightcontroller->ypr.z));
             player->rightWeapon->rotateWeapon(Vec3f(rightcontroller->ypr.y + 140, 0, -rightcontroller->ypr.z));
         }
         }
 
 
@@ -151,10 +155,21 @@ void CrystalPoint::update()
 	}	
 	}	
 	else
 	else
 	{
 	{
-        if (keyboardState.keys[27]){
-            glutExit();
-        }
 		menu->update();
 		menu->update();
+		if (leftcontroller != nullptr) {
+			Vec2f *leftControllerJoystick = &leftcontroller->joystick;
+			if (leftControllerJoystick->y > 0.3 || leftControllerJoystick->y < -0.3) {
+				cursor->update(Vec2f(cursor->mousePosition.x,cursor->mousePosition.y+leftControllerJoystick->y ));
+			}
+			if (leftControllerJoystick->x > 0.3 || leftControllerJoystick->x < -0.3) {
+				cursor->update(Vec2f(cursor->mousePosition.x+leftControllerJoystick->x ,cursor->mousePosition.y));
+			}
+			if(leftcontroller->button){
+				cursor->state = 137;
+			}else if(cursor->state == 137){
+				cursor->state = GLUT_UP;
+			}
+		}
 		cursor->update(cursor->mousePosition + mouseOffset);
 		cursor->update(cursor->mousePosition + mouseOffset);
 	}
 	}
 
 

+ 4 - 1
Enemy.cpp

@@ -133,16 +133,19 @@ void Enemy::update(float delta)
 		rotation.y = atan2f(dx, dz) * 180 / M_PI;		
 		rotation.y = atan2f(dx, dz) * 180 / M_PI;		
 	}
 	}
 	Player *player = Player::getInstance();
 	Player *player = Player::getInstance();
-	if(inObject(player->leftWeapon->collisionPoint)){
+
+	if(inObject(player->position + player->leftWeapon->collisionPoint)){
 		if(!isHit){
 		if(!isHit){
 			isHit = true;
 			isHit = true;
 			hit(player->leftWeapon->damage);
 			hit(player->leftWeapon->damage);
 		}
 		}
+		std::cout << "HIT1";
 	}else if(inObject(player->rightWeapon->collisionPoint)){
 	}else if(inObject(player->rightWeapon->collisionPoint)){
 		if(!isHit){
 		if(!isHit){
 			isHit = true;
 			isHit = true;
 			hit(player->rightWeapon->damage);
 			hit(player->rightWeapon->damage);
 		}
 		}
+		std::cout << "HIT2";
 	}else{
 	}else{
 		isHit = false;
 		isHit = false;
 	}
 	}

+ 1 - 0
Main.cpp

@@ -61,6 +61,7 @@ int main(int argc, char* argv[])
 		if (button == GLUT_LEFT_BUTTON)
 		if (button == GLUT_LEFT_BUTTON)
 			Cursor::getInstance()->state = state;
 			Cursor::getInstance()->state = state;
 
 
+
 			//std::cout << "Left button is down" << std::endl;
 			//std::cout << "Left button is down" << std::endl;
 	};
 	};
 
 

+ 89 - 0
Matrix.cpp

@@ -0,0 +1,89 @@
+//
+// Created by janco on 6/21/16.
+//
+
+#include "Matrix.h"
+
+    Matrix::Matrix(int size = 4)
+    {
+        data = new float[size][size];
+    }
+
+    Matrix Matrix::identity()
+    {
+        Matrix m = new Matrix(4);
+        m.data[0,0] = 1;
+        m.data[1,1] = 1;
+        m.data[2,2] = 1;
+        m.data[3,3] = 1;
+        return m;
+    }
+
+    Matrix Matrix::rotation(float angle, Vector3 axis)
+    {
+        Matrix m = Matrix.identity();
+
+        float c = (float) Math.Cos((double) angle);
+        float s = (float) Math.Sin((double) angle);
+
+        m.data[0, 0] = (float) Math.Pow(axis.x, 2) * (1 - c) + c;
+        m.data[0, 1] = axis.x * axis.y * (1 - c) - axis.z * s;
+        m.data[0, 2] = axis.x * axis.z * (1 - c) - axis.y * s;
+        m.data[1, 0] = axis.x * axis.y * (1 - c) + axis.z * s;
+        m.data[1, 1] = (float)Math.Pow(axis.y, 2) * (1 - c) + c;
+        m.data[1, 2] = axis.y * axis.z * (1 - c) + axis.x * s;
+        m.data[2, 0] = axis.x * axis.z * (1 - c) + axis.y * s;
+        m.data[2, 1] = axis.y * axis.z * (1 - c) - axis.x * s;
+        m.data[2, 2] = (float)Math.Pow(axis.z, 2) * (1 - c) + c;
+
+        return m;
+    }
+    Matrix Matrix::translate(Vector3 offset)
+    {
+        Matrix m = Matrix.identity();
+
+        for(int i = 0; i < 3; i++)
+        {
+            m.data[i, 3] = offset.data[i];
+        }
+
+        return m;
+    }
+
+    Vec3f Matrix::operator * (Matrix mat, Vec3f vec)
+    {
+        Vec3f v = Vec3f(0,0,0);
+
+        for(int i = 0; i < 4; i++)
+        {
+            v.data[i] = 0;
+
+            for(int p = 0; p < 4; p++)
+            {
+                v.data[i] += mat.data[i, p] * vec.data[p];
+            }
+        }
+
+        return v;
+    }
+
+    Matrix Matrix::operator * (Matrix mat1, Matrix mat2)
+    {
+        Matrix m = Matrix.identity();
+
+        for (int i = 0; i < 4; i++)
+        {
+            for (int p = 0; p < 4; p++)
+            {
+                m.data[i, p] = 0;
+
+                for (int q = 0; q < 4; q++)
+                {
+                    m.data[i, p] += mat1.data[i, q] * mat2.data[q, p];
+                }
+            }
+        }
+
+        return m;
+    }
+};

+ 26 - 0
Matrix.h

@@ -0,0 +1,26 @@
+//
+// Created by janco on 6/21/16.
+//
+
+#ifndef CRYSTALPOINT_MATRIX_H
+#define CRYSTALPOINT_MATRIX_H
+
+
+class Matrix {
+private:
+public:
+    float *data;
+
+    Matrix(int i = 4);
+    ~Matrix();
+
+    static Matrix rotation(int angle, Vec3f axis);
+    static Matrix identity(void);
+    static Matrix translate(Vec3f offset);
+
+    Matrix operator * (Matrix m1, Matrix m2);
+    Vec3f operator * (Matrix m, Vec3f v);
+}
+
+
+#endif //CRYSTALPOINT_MATRIX_H

+ 1 - 0
Vector.cpp

@@ -2,6 +2,7 @@
 #include <cmath>
 #include <cmath>
 #include "Vector.h"
 #include "Vector.h"
 
 
+
 Vec3f::Vec3f(float x, float y, float z)
 Vec3f::Vec3f(float x, float y, float z)
 {
 {
 	this->x = x;
 	this->x = x;

+ 38 - 4
Weapon.cpp

@@ -22,6 +22,8 @@ Weapon::Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rot
     this->maxRotation = maxRotation;
     this->maxRotation = maxRotation;
     this->minRotation = minRotation;
     this->minRotation = minRotation;
     this->collisionPoint = collisionPoint;
     this->collisionPoint = collisionPoint;
+
+    this->damage = 1;
 };
 };
 
 
 Weapon::~Weapon(){
 Weapon::~Weapon(){
@@ -46,6 +48,22 @@ void Weapon::move(Vec3f location){
     position = location;
     position = location;
 }
 }
 
 
+Vec3f multiply(float matrix[16], Vec3f vec)
+{
+    Vec3f result;
+
+    for(int i = 0; i < 4; i++)
+    {
+        for(int p = 0; p < 4; p++)
+        {
+            result[i] += matrix[i * p] * vec[p];
+        }
+    }
+
+    return result;
+}
+
+
 void Weapon::draw(){
 void Weapon::draw(){
     if (weaponmodel != nullptr)
     if (weaponmodel != nullptr)
     {
     {
@@ -67,13 +85,27 @@ void Weapon::draw(){
         glRotatef(rotationWeapon.x, 1, 0, 0);
         glRotatef(rotationWeapon.x, 1, 0, 0);
         glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
         glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
 
 
-        glScalef(scale, scale, scale);
 
 
         weaponmodel->draw();
         weaponmodel->draw();
 
 
-        //Test code for finding anker point
-        glColor3ub(255, 255, 0);
+        //Test code for finding anchor point
         glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z);
         glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z);
+
+        float matrix[16];
+        glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
+
+        Vec3f point = multiply(matrix, Vec3f(1,1,1));
+
+
+        glScalef(scale, scale, scale);
+
+
+        glPopMatrix();
+
+        glPushMatrix();
+
+        glTranslatef(point.x, point.y, point.z);
+        glColor3ub(255, 255, 0);
         glBegin(GL_LINES);
         glBegin(GL_LINES);
         glVertex2f(0, 4);
         glVertex2f(0, 4);
         glVertex2f(0, -4);
         glVertex2f(0, -4);
@@ -82,6 +114,8 @@ void Weapon::draw(){
         glEnd();
         glEnd();
 
 
         glPopMatrix();
         glPopMatrix();
-
     }
     }
 }
 }
+
+
+

+ 1 - 0
Weapon.h

@@ -28,6 +28,7 @@ public:
     Vec3f position, rotation, rotationWeapon;
     Vec3f position, rotation, rotationWeapon;
     Vec3f offsetPlayer, ankerPoint, collisionPoint;
     Vec3f offsetPlayer, ankerPoint, collisionPoint;
     Vec2f maxRotation, minRotation;
     Vec2f maxRotation, minRotation;
+
 };
 };
 
 
 
 

+ 3 - 1
World.cpp

@@ -320,7 +320,9 @@ void World::update(float elapsedTime)
 			}
 			}
 		}
 		}
 		enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
 		enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f;
-		
+		if(enemy->isDead()){
+			remove = true;
+		}
 		if(!remove)
 		if(!remove)
 			count++;
 			count++;
 	}
 	}