-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfooditem.cpp
108 lines (83 loc) · 2.86 KB
/
fooditem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
TIE-02201 Ohjelmointi 2: Perusteet, K2019
Assignment 12.4: Matopelin paluu
3D Snake game made with OpenGL 2.1 immediate mode.
See 'instructions.txt' for further information.
fooditem.cpp
Defines a class for 3D food item objects on the
game board. Configurations are loaded from a
JSON file.
@author Joona Perasto, 272725, [email protected]
*/
#include "fooditem.hh"
#include <QJsonArray>
#include <cmath>
FoodItem::FoodItem(Scene* scene, const QString& itemName): GameObject(scene),
isEaten_(false), timeEaten_(0.0f), floatAngle_(0.0f)
{
ResourceManager& resourceManager = ResourceManager::getInstance();
QJsonObject foodData = resourceManager.getFoodData();
QJsonObject propertiesJson = foodData[itemName].toObject();
if (propertiesJson.isEmpty())
{
qWarning() << "FoodItem" << itemName << "does not exist.";
propertiesJson = foodData["null"].toObject();
}
QJsonObject effectJson = propertiesJson["effect"].toObject();
// Apply JSON data to the item
shaderProgram_ = resourceManager.loadProgram(propertiesJson["shaderProgram"].toString());
mesh_ = resourceManager.loadMesh(propertiesJson["mesh"].toString());
texture_ = resourceManager.loadTexture(propertiesJson["texture"].toString());
effect_ = FoodEffect(effectJson);
transform_->setScale(FOOD_SCALE);
}
void FoodItem::update(float deltaTime)
{
floatAngle_ += 180.0f * deltaTime;
// Float the food item
transform_->setPosition(position_ + QVector3D(0.0f, 0.04f * (1 + sinf(floatAngle_ * 0.02f)), 0.0f));
transform_->setRotation(QVector3D(0.0f, floatAngle_, 0.0f));
if (isEaten_)
{
timeEaten_ += deltaTime;
float scale = 1 - timeEaten_/ANIM_TIME;
transform_->setScale(0.1f * scale);
if (scale <= 0.0f)
scene_->removeGameObject(this);
}
}
void FoodItem::render(QOpenGLFunctions *gl)
{
shaderProgram_->enableAttributeArray("aVertex");
shaderProgram_->enableAttributeArray("aTexcoord");
shaderProgram_->enableAttributeArray("aNormal");
shaderProgram_->setAttributeArray("aVertex", mesh_->vertexData.constData());
shaderProgram_->setAttributeArray("aTexcoord", mesh_->texcoordData.constData());
shaderProgram_->setAttributeArray("aNormal", mesh_->normalData.constData());
texture_->bind();
// Draw the food mesh as triangles
gl->glDrawElements(GL_TRIANGLES, mesh_->indexData.count(),
GL_UNSIGNED_INT, mesh_->indexData.constData());
}
void FoodItem::consume()
{
isEaten_ = true;
timeEaten_ = 0.0f;
}
bool FoodItem::canBeEaten() const
{
return !isEaten_;
}
QVector3D FoodItem::getPosition() const
{
return position_;
}
void FoodItem::setPosition(QVector3D position)
{
position_ = position;
transform_->setPosition(position);
}
FoodEffect FoodItem::getEffect() const
{
return effect_;
}