Przeglądaj źródła

Added controller code

Kenneth van Ewijk 9 lat temu
rodzic
commit
83557c7ba7
8 zmienionych plików z 172 dodań i 4 usunięć
  1. 27 0
      CrystalPoint.cpp
  2. 2 0
      CrystalPoint.h
  3. 2 0
      CrystalPoint.vcxproj
  4. 6 0
      CrystalPoint.vcxproj.filters
  5. 11 0
      Player.cpp
  6. 4 4
      Player.h
  7. 86 0
      Weapon.cpp
  8. 34 0
      Weapon.h

+ 27 - 0
CrystalPoint.cpp

@@ -40,6 +40,7 @@ void CrystalPoint::draw()
 	glLoadIdentity();
 
 	worldhandler->draw();
+	player->draw();
 
 	//cursor->draw();
 
@@ -79,6 +80,32 @@ void CrystalPoint::update()
 	if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
 	if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
 
+	Controller *leftcontroller = controller.getLeftController();
+	if (leftcontroller != nullptr) {
+		Vec2f *leftControllerJoystick = &leftcontroller->joystick;
+
+		if (leftcontroller->joystickButton) {
+			controller.rumble(leftcontroller->controllerId, 100, 100);
+		}
+
+		if (leftControllerJoystick->y > 0.3) {
+			player->setPosition(270, leftControllerJoystick->y*deltaTime, false);
+		}
+		else if (leftControllerJoystick->y < -0.3) {
+			player->setPosition(90, leftControllerJoystick->y*-1 * deltaTime, false);
+		}
+		if (leftControllerJoystick->x > 0.3) {
+			player->setPosition(180, leftControllerJoystick->x*deltaTime, false);
+		}
+		else if (leftControllerJoystick->x < -0.3) {
+			player->setPosition(0, leftControllerJoystick->x*-1 * deltaTime, false);
+		}
+
+		player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
+
+	}
+
+
 	if (!worldhandler->isPlayerPositionValid())
 		player->position = oldPosition;
 

+ 2 - 0
CrystalPoint.h

@@ -5,6 +5,7 @@ class SoundSystem;
 class Player;
 #include "Vector.h"
 #include "SoundSystem.h"
+#include "ControllerHandler.h"
 
 class KeyboardState
 {
@@ -25,6 +26,7 @@ public:
 
 	WorldHandler* worldhandler;
 	Player* player;
+	ControllerHandler controller;
 
 	static int width, height;
 	KeyboardState keyboardState;

+ 2 - 0
CrystalPoint.vcxproj

@@ -178,6 +178,7 @@
     <ClCompile Include="Skybox.cpp" />
     <ClCompile Include="Vector.cpp" />
     <ClCompile Include="Vertex.cpp" />
+    <ClCompile Include="Weapon.cpp" />
     <ClCompile Include="World.cpp" />
     <ClCompile Include="WorldHandler.cpp" />
   </ItemGroup>
@@ -206,6 +207,7 @@
     <ClInclude Include="stb_image.h" />
     <ClInclude Include="vector.h" />
     <ClInclude Include="Vertex.h" />
+    <ClInclude Include="Weapon.h" />
     <ClInclude Include="World.h" />
     <ClInclude Include="WorldHandler.h" />
   </ItemGroup>

+ 6 - 0
CrystalPoint.vcxproj.filters

@@ -105,6 +105,9 @@
     <ClCompile Include="lib\serial\src\serial.cc">
       <Filter>Source Files\Serial</Filter>
     </ClCompile>
+    <ClCompile Include="Weapon.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="World.h">
@@ -185,6 +188,9 @@
     <ClInclude Include="lib\serial\include\impl\win.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Weapon.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="worlds\worlds.json">

+ 11 - 0
Player.cpp

@@ -12,6 +12,9 @@ Player::Player()
 	xp = 75;
 	level = 10;
 	crystals = 0;
+
+	leftWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(4.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec2f(170, 70), Vec2f(20, -80));
+	leftWeapon->rotateWeapon(Vec3f(150, 0, 60));
 }
 
 Player* Player::getInstance()
@@ -42,6 +45,8 @@ void Player::setCamera()
 	glRotatef(rotation.y, 0, 1, 0);
 	glTranslatef(-position.x, -position.y, -position.z);
 
+	leftWeapon->rotate(rotation);
+
 }
 
 void Player::setPosition(float angle, float fac, bool height)
@@ -53,4 +58,10 @@ void Player::setPosition(float angle, float fac, bool height)
 		position.x -= (float)cos((rotation.y + angle) / 180 * M_PI) * fac;
 		position.z -= (float)sin((rotation.y + angle) / 180 * M_PI) * fac;
 	}
+
+	leftWeapon->move(position);
+}
+
+void Player::draw() {
+	leftWeapon->draw();
 }

+ 4 - 4
Player.h

@@ -1,7 +1,6 @@
 #pragma once
 #include "Vector.h"
-
-class Model;
+#include "Weapon.h"
 
 class Player
 {
@@ -13,6 +12,7 @@ public:
 
 	void setCamera();
 	void setPosition(float angle, float fac, bool height);
+	void draw(void);
 
 	static Player* getInstance(void);
 	static void init(void);
@@ -20,8 +20,8 @@ public:
 	Vec3f position;
 	Vec2f rotation;
 
-	Model* leftWeapon;
-	Model* rightWeapon;
+	Weapon* leftWeapon;
+	Weapon* rightWeapon;
 
 	float health;
 	float xp;

+ 86 - 0
Weapon.cpp

@@ -0,0 +1,86 @@
+//
+// Created by janco on 25-5-16.
+//
+
+#include "Weapon.h"
+#include "Model.h"
+#include <iostream>
+#include <string>
+#include <cmath>
+
+
+Weapon::Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation,
+               Vec3f offsetPlayer, Vec3f ankerPoint,
+               Vec2f maxRotation, Vec2f minRotation){
+    weaponmodel = Model::load(modelFilename);
+    rotate(rotation);
+    move(location);
+    this->scale = scale;
+
+    this->offsetPlayer = offsetPlayer;
+    this->ankerPoint = ankerPoint;
+    this->maxRotation = maxRotation;
+    this->minRotation = minRotation;
+};
+
+Weapon::~Weapon(){
+
+}
+
+void Weapon::rotateWeapon(Vec3f rotation){
+    if(rotation.x < maxRotation.x && rotation.x > minRotation.x){
+        rotationWeapon.x = rotation.x;
+    }
+    if(rotation.z < maxRotation.y && rotation.z > minRotation.y){
+        rotationWeapon.z = rotation.z;
+    }
+    rotationWeapon.y = rotation.y;
+}
+
+void Weapon::rotate(Vec2f rotation){
+    this->rotation.y = -rotation.y;
+}
+
+void Weapon::move(Vec3f location){
+    position = location;
+}
+
+void Weapon::draw(){
+    if (weaponmodel != nullptr)
+    {
+        glPushMatrix();
+
+        //Player position and rotation
+        glTranslatef(position.x, position.y, position.z);
+        glRotatef(rotation.x, 1, 0, 0);
+        glRotatef(rotation.y, 0, 1, 0);
+        glRotatef(rotation.z, 0, 0, 1);
+
+        //offset from player
+        glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
+
+        //Rotate weapon itself, from specific anker point
+        glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
+        glRotatef(rotationWeapon.z, 0, 0, 1);
+        glRotatef(rotationWeapon.y, 0, 1, 0);
+        glRotatef(rotationWeapon.x, 1, 0, 0);
+        glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
+
+        glScalef(scale, scale, scale);
+
+        weaponmodel->draw();
+
+        //Test code for finding anker point
+/*        glColor3ub(255, 255, 0);
+        glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
+        glBegin(GL_LINES);
+        glVertex2f(0, 4);
+        glVertex2f(0, -4);
+        glVertex2f(4, 0);
+        glVertex2f(-4, 0);
+        glEnd();*/
+
+        glPopMatrix();
+
+    }
+}

+ 34 - 0
Weapon.h

@@ -0,0 +1,34 @@
+//
+// Created by janco on 25-5-16.
+//
+
+#ifndef CRYSTALPOINT_WEAPON_H
+#define CRYSTALPOINT_WEAPON_H
+
+#include "Vector.h"
+#include "Model.h"
+#include <string>
+
+class Weapon {
+public:
+    Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation,
+           Vec3f offsetPlayer, Vec3f ankerPoint,
+           Vec2f maxRotation, Vec2f minXRotation);
+    ~Weapon();
+
+    void draw();
+    void rotateWeapon(Vec3f rotation);
+    void rotate(Vec2f rotation);
+    void move(Vec3f location);
+
+    unsigned int damage;
+    Model* weaponmodel;
+
+    float scale;
+    Vec3f position, rotation, rotationWeapon;
+    Vec3f offsetPlayer, ankerPoint;
+    Vec2f maxRotation, minRotation;
+};
+
+
+#endif //CRYSTALPOINT_WEAPON_H