Load weapons from JSON

This commit is contained in:
2016-06-21 18:25:16 +02:00
parent 4437acb9f2
commit 3025fecf94
7 changed files with 99 additions and 11 deletions
+5 -1
View File
@@ -144,9 +144,13 @@ void CrystalPoint::update()
if (player->rotation.x < -90) if (player->rotation.x < -90)
player->rotation.x = -90; player->rotation.x = -90;
player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
if (!worldhandler->isPlayerPositionValid()) if (!worldhandler->isPlayerPositionValid())
player->position = oldPosition; player->position = oldPosition;
player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f; /*else if (player->position.y > oldPosition.y + 1.2)
player->position = oldPosition;
*/
worldhandler->update(deltaTime); worldhandler->update(deltaTime);
} }
else else
+66 -2
View File
@@ -3,6 +3,10 @@
#include "Player.h" #include "Player.h"
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include <string>
#include <iostream>
#include <fstream>
Player* Player::instance = NULL; Player* Player::instance = NULL;
Player::Player() Player::Player()
@@ -15,10 +19,12 @@ Player::Player()
level = 1; level = 1;
crystals = 0; 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)); loadWeapons();
leftWeapon = leftweapons[0];
leftWeapon->rotateWeapon(Vec3f(150, 0, 60)); leftWeapon->rotateWeapon(Vec3f(150, 0, 60));
rightWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(0.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec2f(170, 70), Vec2f(20, -80)); rightWeapon = rightweapons[0];
rightWeapon->rotateWeapon(Vec3f(150, 0, 60)); rightWeapon->rotateWeapon(Vec3f(150, 0, 60));
} }
@@ -116,3 +122,61 @@ void Player::levelUp()
maxHp += 10; maxHp += 10;
health = maxHp; health = maxHp;
} }
void Player::loadWeapons()
{
std::string fileName = "weapons.json";
//Open world json file
std::ifstream file(fileName);
if (!file.is_open())
std::cout << "Error, can't open world file - " << fileName << "\n";
json::Value v = json::readJson(file);
file.close();
//Check file
if (v["weapons"].isNull())
std::cout << "Invalid weapons file: " << fileName << "\n";
//Load object templates
for (auto w : v["weapons"])
{
Weapon* lweapon;
Weapon* rweapon;
std::string name = w["name"].asString();
std::string fileN = w["file"].asString();
float damage = w["damage"].asFloat();
Weapon::Element e = Weapon::FIRE;
if(w["element"].asString() == "fire")
e = Weapon::FIRE;
else if (w["element"].asString() == "water")
e = Weapon::WATER;
else if (w["element"].asString() == "earth")
e = Weapon::EARTH;
else if (w["element"].asString() == "air")
e = Weapon::AIR;
else
e = Weapon::FIRE;
Vec3f leftoffset = Vec3f(w["left"]["offset"][0].asFloat(), w["left"]["offset"][1].asFloat(), w["left"]["offset"][2].asFloat());
Vec3f rightoffset = Vec3f(w["right"]["offset"][0].asFloat(), w["right"]["offset"][1].asFloat(), w["right"]["offset"][2].asFloat());
Vec3f anchor = Vec3f(w["anchor"][0].asFloat(), w["anchor"][1].asFloat(), w["anchor"][2].asFloat());
Vec3f collision = Vec3f(w["collision"][0].asFloat(), w["collision"][1].asFloat(), w["collision"][2].asFloat());
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);
leftweapons.push_back(lweapon);
rightweapons.push_back(rweapon);
}
}
+8
View File
@@ -1,12 +1,20 @@
#pragma once #pragma once
#include "Vector.h" #include "Vector.h"
#include "Weapon.h" #include "Weapon.h"
#include "json.h"
#include <vector>
class Player class Player
{ {
private: private:
static Player* instance; static Player* instance;
void levelUp(); void levelUp();
std::vector<Weapon*> leftweapons;
std::vector<Weapon*> rightweapons;
void loadWeapons(void);
public: public:
Player(); Player();
~Player(); ~Player();
+8 -2
View File
@@ -9,18 +9,24 @@
#include <cmath> #include <cmath>
Weapon::Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation, Weapon::Weapon(std::string name, int damage, Element e, std::string modelFilename, float scale, Vec3f location, Vec2f rotation,
Vec3f offsetPlayer, Vec3f ankerPoint, Vec3f offsetPlayer, Vec3f ankerPoint,
Vec2f maxRotation, Vec2f minRotation){ Vec2f maxRotation, Vec2f minRotation,
Vec3f collision){
weaponmodel = Model::load(modelFilename); weaponmodel = Model::load(modelFilename);
rotate(rotation); rotate(rotation);
move(location); move(location);
this->scale = scale; this->scale = scale;
this->name = name;
this->damage = damage;
this->element = e;
this->offsetPlayer = offsetPlayer; this->offsetPlayer = offsetPlayer;
this->ankerPoint = ankerPoint; this->ankerPoint = ankerPoint;
this->maxRotation = maxRotation; this->maxRotation = maxRotation;
this->minRotation = minRotation; this->minRotation = minRotation;
this->collision = collision;
}; };
Weapon::~Weapon(){ Weapon::~Weapon(){
+9 -3
View File
@@ -11,9 +11,12 @@
class Weapon { class Weapon {
public: public:
Weapon(std::string modelFilename, float scale, Vec3f location, Vec2f rotation, enum Element {FIRE, WATER, EARTH, AIR};
Weapon(std::string name, int damage, Element element, std::string modelFilename, float scale, Vec3f location, Vec2f rotation,
Vec3f offsetPlayer, Vec3f ankerPoint, Vec3f offsetPlayer, Vec3f ankerPoint,
Vec2f maxRotation, Vec2f minXRotation); Vec2f maxRotation, Vec2f minXRotation,
Vec3f collision);
~Weapon(); ~Weapon();
void draw(); void draw();
@@ -21,12 +24,15 @@ public:
void rotate(Vec2f rotation); void rotate(Vec2f rotation);
void move(Vec3f location); void move(Vec3f location);
std::string name;
unsigned int damage; unsigned int damage;
Element element;
Model* weaponmodel; Model* weaponmodel;
float scale; float scale;
Vec3f position, rotation, rotationWeapon; Vec3f position, rotation, rotationWeapon;
Vec3f offsetPlayer, ankerPoint; Vec3f offsetPlayer, ankerPoint, collision;
Vec2f maxRotation, minRotation; Vec2f maxRotation, minRotation;
}; };
+1 -1
View File
@@ -64,8 +64,8 @@ World::World(const std::string &fileName)
//Set player starting position //Set player starting position
player->position.x = v["player"]["startposition"][0].asFloat(); player->position.x = v["player"]["startposition"][0].asFloat();
player->position.y = v["player"]["startposition"][1].asFloat();
player->position.z = v["player"]["startposition"][2].asFloat(); player->position.z = v["player"]["startposition"][2].asFloat();
player->position.y = heightmap->GetHeight(player->position.x, player->position.z);
//Load and place objects into world //Load and place objects into world
for (auto object : v["objects"]) for (auto object : v["objects"])
+1 -1
View File
@@ -83,7 +83,7 @@
] ]
}, },
"player": { "player": {
"startposition": [ 1, 5, 20 ] "startposition": [ 20, 5, 20 ]
}, },
"objects": [ ], "objects": [ ],
"portal": { "portal": {