Load weapons from JSON
This commit is contained in:
+5
-1
@@ -144,9 +144,13 @@ void CrystalPoint::update()
|
||||
if (player->rotation.x < -90)
|
||||
player->rotation.x = -90;
|
||||
|
||||
player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
|
||||
|
||||
if (!worldhandler->isPlayerPositionValid())
|
||||
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);
|
||||
}
|
||||
else
|
||||
|
||||
+66
-2
@@ -3,6 +3,10 @@
|
||||
#include "Player.h"
|
||||
#include <GL/freeglut.h>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
Player* Player::instance = NULL;
|
||||
|
||||
Player::Player()
|
||||
@@ -15,10 +19,12 @@ Player::Player()
|
||||
level = 1;
|
||||
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));
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@@ -115,4 +121,62 @@ void Player::levelUp()
|
||||
maxXp += 50;
|
||||
maxHp += 10;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
#pragma once
|
||||
#include "Vector.h"
|
||||
#include "Weapon.h"
|
||||
#include "json.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Player
|
||||
{
|
||||
private:
|
||||
static Player* instance;
|
||||
void levelUp();
|
||||
|
||||
std::vector<Weapon*> leftweapons;
|
||||
std::vector<Weapon*> rightweapons;
|
||||
void loadWeapons(void);
|
||||
|
||||
public:
|
||||
Player();
|
||||
~Player();
|
||||
|
||||
+8
-2
@@ -9,18 +9,24 @@
|
||||
#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,
|
||||
Vec2f maxRotation, Vec2f minRotation){
|
||||
Vec2f maxRotation, Vec2f minRotation,
|
||||
Vec3f collision){
|
||||
weaponmodel = Model::load(modelFilename);
|
||||
rotate(rotation);
|
||||
move(location);
|
||||
this->scale = scale;
|
||||
|
||||
this->name = name;
|
||||
this->damage = damage;
|
||||
this->element = e;
|
||||
|
||||
this->offsetPlayer = offsetPlayer;
|
||||
this->ankerPoint = ankerPoint;
|
||||
this->maxRotation = maxRotation;
|
||||
this->minRotation = minRotation;
|
||||
this->collision = collision;
|
||||
};
|
||||
|
||||
Weapon::~Weapon(){
|
||||
|
||||
@@ -11,22 +11,28 @@
|
||||
|
||||
class Weapon {
|
||||
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,
|
||||
Vec2f maxRotation, Vec2f minXRotation);
|
||||
Vec2f maxRotation, Vec2f minXRotation,
|
||||
Vec3f collision);
|
||||
~Weapon();
|
||||
|
||||
void draw();
|
||||
void rotateWeapon(Vec3f rotation);
|
||||
void rotate(Vec2f rotation);
|
||||
void move(Vec3f location);
|
||||
|
||||
|
||||
std::string name;
|
||||
unsigned int damage;
|
||||
Element element;
|
||||
|
||||
Model* weaponmodel;
|
||||
|
||||
float scale;
|
||||
Vec3f position, rotation, rotationWeapon;
|
||||
Vec3f offsetPlayer, ankerPoint;
|
||||
Vec3f offsetPlayer, ankerPoint, collision;
|
||||
Vec2f maxRotation, minRotation;
|
||||
};
|
||||
|
||||
|
||||
@@ -64,8 +64,8 @@ World::World(const std::string &fileName)
|
||||
|
||||
//Set player starting position
|
||||
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.y = heightmap->GetHeight(player->position.x, player->position.z);
|
||||
|
||||
//Load and place objects into world
|
||||
for (auto object : v["objects"])
|
||||
|
||||
+1
-1
@@ -83,7 +83,7 @@
|
||||
]
|
||||
},
|
||||
"player": {
|
||||
"startposition": [ 1, 5, 20 ]
|
||||
"startposition": [ 20, 5, 20 ]
|
||||
},
|
||||
"objects": [ ],
|
||||
"portal": {
|
||||
|
||||
Reference in New Issue
Block a user