aboutsummaryrefslogtreecommitdiffstats
path: root/src/items.cpp
diff options
context:
space:
mode:
authordrumsetmonkey <abelleisle@roadrunner.com>2016-05-02 08:46:48 -0400
committerdrumsetmonkey <abelleisle@roadrunner.com>2016-05-02 08:46:48 -0400
commit13cd55136ffd09afd9f4828a00716ed9f94f0e0b (patch)
treed7841da0e6c1b1e4c2c5dc0dd2b492e4fcaeee18 /src/items.cpp
parentc61cbf691bee6dec791c3a161145ae16b448ac2b (diff)
Sword
Diffstat (limited to 'src/items.cpp')
-rw-r--r--src/items.cpp87
1 files changed, 80 insertions, 7 deletions
diff --git a/src/items.cpp b/src/items.cpp
index b86abf7..f88186a 100644
--- a/src/items.cpp
+++ b/src/items.cpp
@@ -18,20 +18,66 @@ int BaseItem::useItem()
int Sword::useItem()
{
- std::cout << "Swing!" << std::endl;
+ if (inUse())
+ return -1;
+
std::thread([this]{
- player->inv->usingi = true;
- bool swing = true;
+ setUse(true);
+ volatile bool swing = true;
+ bool back = false;
float coef = 0.0f;
while (swing) {
- coef += .01f;
+
+ // handle swinging
+ if (!back)
+ coef += .8f;
+ else
+ coef -= .4f;
+
if (player->left)
rotation = coef;
else
rotation = -coef;
+
+ if (coef > 80 && !back)
+ back = true;
+
+ if (coef <= 0 && back) {
+ swing = false;
+ coef = 0.0f;
+ rotation = 0.0f;
+ }
+
+ if (!back) {
+ // handle collision with interaction
+ hitbox.start.y = player->loc.y+(player->height/3);
+ hitbox.start.x = player->left ? player->loc.x : player->loc.x + player->width;
+
+ for (auto &e : interact) {
+ float dist = 0.0f;
+ while (dist < dim.y) {
+ hitbox.end = hitbox.start;
+ hitbox.end.x += dist * cos(rotation*PI/180);
+ hitbox.end.y += dist * sin(rotation*PI/180);
+
+ if (hitbox.end.x > e->loc.x && hitbox.end.x < e->loc.x + e->width) {
+ if (hitbox.end.y > e->loc.y && hitbox.end.y < e->loc.y + e->height) {
+ e->takeHit(damage, 600);
+ }
+ }
+
+ dist += HLINES(1);
+ }
+ }
+ }
+
+ // add a slight delay
+ SDL_Delay(1);
}
- player->inv->usingi = false;
+ for (auto &e : interact)
+ e->setCooldown(0);
+ setUse(false);
}).detach();
return 0;
@@ -92,9 +138,26 @@ RawFood* RawFood::clone()
* ITEM *
**************************************************/
-Item::~Item()
+bool Item::inUse()
{
- delete tex;
+ return beingUsed;
+}
+
+void Item::setUse(bool use)
+{
+ beingUsed = use;
+}
+
+void Item::addInteract(Entity* e)
+{
+ interact.push_back(e);
+}
+
+void Item::addInteract(std::vector<Entity*> e)
+{
+ for (auto &v : e) {
+ interact.push_back(v);
+ }
}
GLuint Item::rtex()
@@ -107,6 +170,11 @@ GLuint Item::rtex(int n)
return tex->image[n];
}
+Item::~Item()
+{
+ delete tex;
+}
+
/**************************************************
* SWORD *
**************************************************/
@@ -116,6 +184,11 @@ float Sword::getDamage()
return damage;
}
+void Sword::setDamage(float d)
+{
+ damage = d;
+}
+
/**************************************************
* BOW *
**************************************************/