diff --git a/Controller.h b/Controller.h index 6d85ad8..29b693e 100644 --- a/Controller.h +++ b/Controller.h @@ -9,6 +9,7 @@ public: Vec3f ypr; Vec2f joystick; bool button, joystickButton, magnetSwitch; + bool lastButton, lastJoystickButton, lastMagetSwitch; int controllerId; void setConnected(bool connected); diff --git a/ControllerHandler.cpp b/ControllerHandler.cpp index 5e12a15..900585e 100644 --- a/ControllerHandler.cpp +++ b/ControllerHandler.cpp @@ -120,8 +120,13 @@ void ControllerHandler::commandControllerData(std::vector data) { c->joystick.x = std::stoi(data[2])/2000.0f; c->joystick.y = std::stoi(data[3])/2000.0f; - c->joystickButton = !(data[4] == "0"); + c->lastButton = c->button; + c->lastJoystickButton = c->joystickButton; + c->lastMagetSwitch = c->magnetSwitch; + + c->joystickButton = !(data[4] == "0"); + c->button = !(data[8] == "0"); c->magnetSwitch = !(data[9] == "0"); } } diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 6b59afd..17f2cf7 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -64,8 +64,11 @@ void CrystalPoint::update() lastFrameTime = frameTime; if (keyboardState.keys[27] && !prevKeyboardState.keys[27]) - state = !state; - + state = !state; + + Controller *rightcontroller = controller.getRightController(); + Controller *leftcontroller = controller.getLeftController(); + if (state) { Player* player = Player::getInstance(); @@ -75,7 +78,7 @@ void CrystalPoint::update() if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT]) worldhandler->NextWorld(); if (keyboardState.special[GLUT_KEY_UP] && !prevKeyboardState.special[GLUT_KEY_UP]) - player->NextLeftWeapon(); + player->NextRightWeapon(); if (keyboardState.special[GLUT_KEY_DOWN] && !prevKeyboardState.special[GLUT_KEY_DOWN]) player->PreviousLeftWeapon(); if (keyboardState.keys[27]) @@ -103,42 +106,53 @@ 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 || leftControllerJoystick->y < -0.3) { + player->rotation.x += leftControllerJoystick->y/4; } - - 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->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)); + if(leftcontroller->button && leftcontroller->joystickButton){ + state = !state; + }else if(!leftcontroller->lastButton && leftcontroller->button){ + leftcontroller->lastButton = leftcontroller->button; + controller.rumble(leftcontroller->controllerId, 100, 200); + player->NextLeftWeapon(); + }else if(!leftcontroller->lastJoystickButton && leftcontroller->joystickButton){ + leftcontroller->lastJoystickButton = leftcontroller->joystickButton; + player->hit = true; + } + } - Controller *rightcontroller = controller.getRightController(); if(rightcontroller != nullptr){ Vec2f *rightControllerJoystick = &rightcontroller->joystick; - if (rightControllerJoystick->y > 0.3 || rightControllerJoystick->y < -0.3) { - player->rotation.x += rightcontroller->joystick.y/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); } - if (rightControllerJoystick->x > 0.3 || rightControllerJoystick->x < -0.3) { - player->rotation.y += rightcontroller->joystick.x/4; - } + if(!rightcontroller->lastButton && rightcontroller->button){ + rightcontroller->lastButton = rightcontroller->button; + controller.rumble(rightcontroller->controllerId, 100, 200); + player->NextRightWeapon(); + } + player->rightWeapon->rotateWeapon(Vec3f(rightcontroller->ypr.y + 140, 0, -rightcontroller->ypr.z)); } @@ -151,14 +165,25 @@ void CrystalPoint::update() if (!worldhandler->isPlayerPositionValid()) player->position = oldPosition; - /*else if (player->position.y > oldPosition.y + 1.2) - player->position = oldPosition; - */ worldhandler->update(deltaTime); } else { 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); } diff --git a/Enemy.cpp b/Enemy.cpp index d46e4fb..05932ce 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -2,6 +2,7 @@ #include #include "Enemy.h" #include "Model.h" +#include "Player.h" #include Enemy::Enemy(const std::string &fileName, @@ -80,6 +81,14 @@ void Enemy::collide(const Entity * entity) position.z = difference.z + entity->position.z; } +void Enemy::hit(int damage){ + health -= damage; +} + +bool Enemy::isDead(){ + return health < 0; +} + void Enemy::update(float delta) { music->SetPos(position, Vec3f()); @@ -116,4 +125,21 @@ void Enemy::update(float delta) } rotation.y = atan2f(dx, dz) * 180 / M_PI; } + Player *player = Player::getInstance(); + + if(inObject(player->position + player->leftWeapon->collisionPoint)){ + if(!isHit){ + isHit = true; + hit(player->leftWeapon->damage); + } + std::cout << "HIT1"; + }else if(inObject(player->rightWeapon->collisionPoint)){ + if(!isHit){ + isHit = true; + hit(player->rightWeapon->damage); + } + std::cout << "HIT2"; + }else{ + isHit = false; + } } \ No newline at end of file diff --git a/Enemy.h b/Enemy.h index eb503b4..0be19e1 100644 --- a/Enemy.h +++ b/Enemy.h @@ -21,11 +21,15 @@ public: int xp; bool attack; + bool isHit; + void update(float); void draw(); void inEyeSight(Vec3f &); void collide(const Entity *entity); + void hit(int damage); + bool isDead(void); private: int hit_sound_id; }; diff --git a/Main.cpp b/Main.cpp index c119e64..e9abf25 100644 --- a/Main.cpp +++ b/Main.cpp @@ -61,6 +61,7 @@ int main(int argc, char* argv[]) if (button == GLUT_LEFT_BUTTON) Cursor::getInstance()->state = state; + //std::cout << "Left button is down" << std::endl; }; @@ -79,7 +80,7 @@ void configureOpenGL() { //Init window and glut display mode glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); - glutInitWindowSize(800, 600); + glutInitWindowSize(1440, 900); //glutInitWindowPosition(glutGet(GLUT_WINDOW_WIDTH) / 2 - 800/2, glutGet(GLUT_WINDOW_HEIGHT) / 2 - 600/2); glutCreateWindow("Crystal Point"); //glutFullScreen(); diff --git a/Player.cpp b/Player.cpp index 54888ad..d650a02 100644 --- a/Player.cpp +++ b/Player.cpp @@ -18,6 +18,7 @@ Player::Player() maxXp = 100; level = 5; crystals = 0; + hit = false; loadWeapons(); @@ -150,6 +151,7 @@ void Player::loadWeapons() std::string name = w["name"].asString(); std::string fileN = w["file"].asString(); float damage = w["damage"].asFloat(); + float scale = w["scale"].asFloat(); Weapon::Element e = Weapon::FIRE; @@ -174,8 +176,8 @@ void Player::loadWeapons() Vec2f maxRot = Vec2f(w["maxRotation"][0].asFloat(), w["maxRotation"][1].asFloat()); Vec2f minRot = Vec2f(w["minRotation"][0].asFloat(), w["minRotation"][1].asFloat()); - lweapon = new Weapon(name, damage, e, fileN, 1, position, rotation, leftoffset, anchor, maxRot, minRot, collision); - rweapon = new Weapon(name, damage, e, fileN, 1, position, rotation, rightoffset, anchor, maxRot, minRot, collision); + lweapon = new Weapon(name, damage, e, fileN, scale, position, rotation, leftoffset, anchor, maxRot, minRot, collision); + rweapon = new Weapon(name, damage, e, fileN, scale, position, rotation, rightoffset, anchor, maxRot, minRot, collision); leftweapons.push_back(lweapon); rightweapons.push_back(rweapon); @@ -191,6 +193,8 @@ void Player::PreviousRightWeapon() currentrightweapon = (rightweapons.size() > level ? level - 1 : rightweapons.size() - 1); rightWeapon = rightweapons[currentrightweapon]; + rightWeapon->move(position); + rightWeapon->rotate(rotation); } void Player::NextRightWeapon(void) { @@ -200,6 +204,8 @@ void Player::NextRightWeapon(void) currentrightweapon = 0; rightWeapon = rightweapons[currentrightweapon]; + rightWeapon->move(position); + rightWeapon->rotate(rotation); } void Player::PreviousLeftWeapon(void) { @@ -209,6 +215,8 @@ void Player::PreviousLeftWeapon(void) currentleftweapon = (leftweapons.size() > level ? level - 1 : leftweapons.size() - 1); leftWeapon = leftweapons[currentleftweapon]; + leftWeapon->move(position); + leftWeapon->rotate(rotation); } void Player::NextLeftWeapon(void) { @@ -218,4 +226,6 @@ void Player::NextLeftWeapon(void) currentleftweapon = 0; leftWeapon = leftweapons[currentleftweapon]; + leftWeapon->move(position); + leftWeapon->rotate(rotation); } \ No newline at end of file diff --git a/Player.h b/Player.h index b843153..17bbe49 100644 --- a/Player.h +++ b/Player.h @@ -40,6 +40,8 @@ public: int level; int crystals; + bool hit; + float speed; void HpUp(int); diff --git a/Vector.cpp b/Vector.cpp index 35a6d41..c7ea763 100644 --- a/Vector.cpp +++ b/Vector.cpp @@ -2,6 +2,7 @@ #include #include "Vector.h" + Vec3f::Vec3f(float x, float y, float z) { this->x = x; diff --git a/WAVE/ape.wav b/WAVE/ape.wav new file mode 100644 index 0000000..29aecf6 Binary files /dev/null and b/WAVE/ape.wav differ diff --git a/WAVE/hedge.wav b/WAVE/hedge.wav new file mode 100644 index 0000000..c92201d Binary files /dev/null and b/WAVE/hedge.wav differ diff --git a/WAVE/letsRock.wav b/WAVE/letsRock.wav new file mode 100644 index 0000000..976eb2e Binary files /dev/null and b/WAVE/letsRock.wav differ diff --git a/Weapon.cpp b/Weapon.cpp index 878433b..5c78e92 100644 --- a/Weapon.cpp +++ b/Weapon.cpp @@ -27,7 +27,8 @@ Weapon::Weapon(std::string name, int damage, Element e, std::string modelFilenam this->ankerPoint = ankerPoint; this->maxRotation = maxRotation; this->minRotation = minRotation; - this->collision = collision; + this->collisionPoint = collisionPoint; + }; Weapon::~Weapon(){ @@ -52,11 +53,28 @@ void Weapon::move(Vec3f 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(){ if (weaponmodel != nullptr) { glPushMatrix(); + //Player position and rotation glTranslatef(position.x, position.y, position.z); glRotatef(rotation.x, 1, 0, 0); @@ -66,28 +84,44 @@ void Weapon::draw(){ //offset from player glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z); + glScalef(scale, scale, scale); + //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(); + glEnd();*/ + + glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z); + + weaponmodel->draw(); + + //Test code for finding anchor point + glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z); + + float matrix[16]; + glGetFloatv(GL_MODELVIEW_MATRIX, matrix); + + Vec3f point = multiply(matrix, Vec3f(1,1,1)); + glPopMatrix(); + glPushMatrix(); + + + + glPopMatrix(); } } + + + diff --git a/Weapon.h b/Weapon.h index 187b5b0..059aefd 100644 --- a/Weapon.h +++ b/Weapon.h @@ -16,7 +16,7 @@ public: Weapon(std::string name, int damage, Element element, std::string modelFilename, float scale, Vec3f location, Vec2f rotation, Vec3f offsetPlayer, Vec3f ankerPoint, Vec2f maxRotation, Vec2f minXRotation, - Vec3f collision); + Vec3f collisionPoint); ~Weapon(); void draw(); @@ -32,8 +32,9 @@ public: float scale; Vec3f position, rotation, rotationWeapon; - Vec3f offsetPlayer, ankerPoint, collision; + Vec3f offsetPlayer, ankerPoint, collisionPoint; Vec2f maxRotation, minRotation; + }; diff --git a/World.cpp b/World.cpp index b706b13..42d4179 100644 --- a/World.cpp +++ b/World.cpp @@ -249,6 +249,7 @@ float World::getHeight(float x, float y) void World::draw() { + player->setCamera(); float lightPosition[4] = { 0, 2, 1, 0 }; glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); @@ -260,7 +261,6 @@ void World::draw() skybox->draw(); - player->setCamera(); player->draw(); heightmap->Draw(); @@ -304,6 +304,9 @@ void World::update(float elapsedTime) enemy->update(elapsedTime); if (enemy->hasTarget) { + if(player->hit) + enemy->hit(player->leftWeapon->damage); + for (auto e : entities) { if (e->canCollide && e->inObject(enemy->position)) @@ -315,12 +318,13 @@ void World::update(float elapsedTime) if (enemy->attack) { - remove = true; - continue; + player->HpDown(enemy->damage / 4); } } - enemy->position.y = getHeight(enemy->position.x, enemy->position.z); - + enemy->position.y = getHeight(enemy->position.x, enemy->position.z) + 2.0f; + if(enemy->isDead()){ + remove = true; + } if(!remove) count++; } @@ -328,7 +332,7 @@ void World::update(float elapsedTime) if (remove) { delete enemies[count]; - player->XpUp(enemies[count]->xp); + player->XpUp(enemies[count]->xp*2); enemies.erase(enemies.begin() + count); player->HpUp(10); } @@ -340,6 +344,8 @@ void World::update(float elapsedTime) if (portal->enter(elapsedTime)) nextworld = true; } + + player->hit = false; } @@ -360,3 +366,4 @@ bool World::isPlayerPositionValid() } return true; } + diff --git a/weapons.json b/weapons.json index 77177a9..264dd33 100644 --- a/weapons.json +++ b/weapons.json @@ -10,12 +10,13 @@ "collision": [ 0, 0, 0 ], "maxRotation": [ 170, 70 ], "minRotation": [ 20, -80 ], + "scale": 0.3, "left": { - "offset": [ 4.5, -8, -1 ] + "offset": [ 1.5, -2.6, 0 ] }, "right": { - "offset": [ 0.5, -8, -1 ] + "offset": [ -0.1, -2.6, 0 ] } }, { @@ -28,12 +29,13 @@ "collision": [ 0, 0, 0 ], "maxRotation": [ 170, 70 ], "minRotation": [ 20, -80 ], + "scale": 0.3, "left": { - "offset": [ 4.5, -8, -1 ] + "offset": [ 1.5, -2.6, 0 ] }, "right": { - "offset": [ 0.5, -8, -1 ] + "offset": [ -0.1, -2.6, 0 ] } } ] diff --git a/worlds/rock.json b/worlds/rock.json index 362d6d0..cf3f2e8 100644 --- a/worlds/rock.json +++ b/worlds/rock.json @@ -85,56 +85,56 @@ "file": "models/Enemies/Monkey.obj", "pos": [ 80, 0, 670 ], "scale": 1, - "music": "WAVE/ghostEnemy.wav", + "music": "WAVE/ape.wav", "health": 10, - "damage": 1 + "damage": 2 }, { "file": "models/Enemies/Monkey.obj", "pos": [ 213, 0, 348 ], "scale": 1.25, - "music": "WAVE/ghostEnemy.wav", + "music": "WAVE/ape.wav", "health": 10, - "damage": 1 + "damage": 2 }, { "file": "models/Bob/BobRock.obj", "pos": [437,0,600], "scale": 0.75, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/letsRock.wav", + "health": 5, "damage": 1 }, { "file": "models/Bob/BobRock.obj", "pos": [540,0,681], "scale": 0.75, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/letsRock.wav", + "health": 5, "damage": 1 }, { "file": "models/Bob/BobRock.obj", "pos": [594,0,575], "scale": 0.75, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/letsRock.wav", + "health": 5, "damage": 1 }, { "file": "models/Bob/BobRock.obj", "pos": [666,0,631], "scale": 0.75, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/letsRock.wav", + "health": 5, "damage": 1 }, { "file": "models/Bob/BobRock.obj", "pos": [ 603, 0, 562 ], "scale": 0.75, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/letsRock.wav", + "health": 5, "damage": 1 }, { @@ -189,72 +189,72 @@ "file": "models/Enemies/VincentRock.obj", "pos": [363,0,343], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [333,0,263], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [243,0,218], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [289,0,123], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [628,0,397], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [618,0,416], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [660,0,432], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [639,0,442], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 }, { "file": "models/Enemies/VincentRock.obj", "pos": [613,0,447], "scale": 1, - "music": "WAVE/ghostEnemy.wav", - "health": 10, + "music": "WAVE/hedge.wav", + "health": 3, "damage": 1 } diff --git a/worlds/small.json b/worlds/small.json index 3bcbb48..62d5ddf 100644 --- a/worlds/small.json +++ b/worlds/small.json @@ -12,7 +12,7 @@ "music": "WAVE/world1.wav" }, "player": { - "startposition": [ 20, 0, 20 ] + "startposition": [ -10, -10, -10 ] }, "objects": [ ], "portal": {