aboutsummaryrefslogtreecommitdiffstats
path: root/src
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
parentc61cbf691bee6dec791c3a161145ae16b448ac2b (diff)
Sword
Diffstat (limited to 'src')
-rw-r--r--src/entities.cpp34
-rw-r--r--src/inventory.cpp35
-rw-r--r--src/items.cpp87
-rw-r--r--src/mob.cpp2
-rw-r--r--src/ui.cpp8
-rw-r--r--src/world.cpp16
6 files changed, 155 insertions, 27 deletions
diff --git a/src/entities.cpp b/src/entities.cpp
index d5d9af2..58c1651 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -111,18 +111,38 @@ void Entity::spawn(float x, float y)
void Entity::takeHit(unsigned int _health, unsigned int cooldown)
{
- // modify variables
- health = fmax(health - _health, 0);
- forcedMove = true;
- hitCooldown = cooldown;
+ if (hitCooldown <= 1) {
+ // modify variables
+ health = fmax(health - _health, 0);
+ forcedMove = true;
+ hitCooldown = cooldown;
+
+ // pushback
+ vel.x = player->left ? -0.5f : 0.5f;
+ vel.y = 0.2f;
+ }
+}
+
+unsigned int Entity::coolDown()
+{
+ return hitCooldown;
+}
- // pushback
- vel.x = player->left ? -0.5f : 0.5f;
- vel.y = 0.2f;
+void Entity::setCooldown(unsigned int c)
+{
+ hitCooldown = c;
}
+
+
void Entity::handleHits(void)
{
+ int c = hitCooldown - game::time::getDeltaTime();
+ if (c >= 0)
+ hitCooldown = c;
+ else
+ hitCooldown = 0;
+
if (!forcedMove)
return;
diff --git a/src/inventory.cpp b/src/inventory.cpp
index ff7284f..b617d09 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -41,6 +41,8 @@ void items(void)
XMLElement *exml = xml.FirstChildElement("item");
XMLElement *cxml = xml.FirstChildElement("currency");
+ Sword *tmpSword = new Sword();
+
while (cxml) {
// NEWEWEWEWEWEWEWEW
@@ -60,7 +62,8 @@ void items(void)
// if the type is a sword
} else if (strCaseCmp(name, "sword")) {
- ItemMap.push_back(new Sword());
+ tmpSword->setDamage(exml->FloatAttribute("damage"));
+ ItemMap.push_back(tmpSword->clone());
// if the type is a bow
} else if (strCaseCmp(name, "bow")) {
@@ -68,7 +71,7 @@ void items(void)
ItemMap.push_back(new Bow());
// uncooked / raw food
- } else if (strCaseCmp(name, "rawfood")) {
+ } else if (strCaseCmp(name, "raw food")) {
ItemMap.push_back(new RawFood());
@@ -587,14 +590,20 @@ void Inventory::draw(void) {
void itemDraw(Player *p, Item *d) {
itemLoc.y = p->loc.y+(p->height/3);
- itemLoc.x = p->left?p->loc.x:p->loc.x+p->width;
+ itemLoc.x = p->left?p->loc.x-d->dim.x/2:p->loc.x+p->width-d->dim.x/2;
glPushMatrix();
glUseProgram(shaderProgram);
glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
- glTranslatef(itemLoc.x,itemLoc.y,0);
- glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
- glTranslatef(-itemLoc.x,-itemLoc.y,0);
+ if (p->left) {
+ glTranslatef(itemLoc.x+d->dim.x/2,itemLoc.y,0);
+ glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
+ glTranslatef(-itemLoc.x-d->dim.x/2,-itemLoc.y,0);
+ } else {
+ glTranslatef(itemLoc.x+d->dim.x/2,itemLoc.y,0);
+ glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
+ glTranslatef(-itemLoc.x-d->dim.x/2,-itemLoc.y,0);
+ }
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,d->tex->image[0]);
glColor4ub(255,255,255,255);
@@ -620,11 +629,23 @@ int Inventory::useItem(void)
int Inventory::useCurrent()
{
- if(Items[sel].second)
+ if (Items[sel].second)
return Items[sel].first->useItem();
return -1;
}
+void Inventory::currentAddInteract(Entity* e)
+{
+ if (Items[sel].second)
+ Items[sel].first->addInteract(e);
+}
+
+void Inventory::currentAddInteract(std::vector<Entity*> e)
+{
+ if (Items[sel].second)
+ Items[sel].first->addInteract(e);
+}
+
bool Inventory::detectCollision(vec2 one, vec2 two) {
(void)one;
(void)two;
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 *
**************************************************/
diff --git a/src/mob.cpp b/src/mob.cpp
index e98a648..f5c1af2 100644
--- a/src/mob.cpp
+++ b/src/mob.cpp
@@ -54,7 +54,7 @@ Door::Door(void) : Mob()
maxHealth = health = 50;
width = HLINES(12);
height = HLINES(20);
- tex = TextureIterator({"assets/door.png"});
+ tex = TextureIterator({"assets/style/classic/door.png"});
}
void Door::act(void)
diff --git a/src/ui.cpp b/src/ui.cpp
index be6c3f3..5f61f6d 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -964,9 +964,11 @@ EXIT:
dialogAdvance();
} else {
// left click uses item
- if (e.button.button & SDL_BUTTON_LEFT)
- if(!player->inv->usingi)
- player->inv->useCurrent();
+ if (e.button.button & SDL_BUTTON_LEFT) {
+ player->inv->currentAddInteract(currentWorld->getMobs());
+ player->inv->useCurrent();
+ }
+
}
if(mouse.x > player->loc.x && mouse.x < player->loc.x + player->width &&
diff --git a/src/world.cpp b/src/world.cpp
index a7c2fdf..0e4fd84 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -637,11 +637,13 @@ detect(Player *p)
int l;
// handle the player
- std::thread(&World::singleDetect, this, p).detach();
+ singleDetect(p);
+ //std::thread(&World::singleDetect, this, p).detach();
// handle other entities
for (auto &e : entity)
- std::thread(&World::singleDetect, this, e).detach();
+ singleDetect(e);
+ //std::thread(&World::singleDetect, this, e).detach();
// handle particles
for (auto &part : particles) {
@@ -786,6 +788,16 @@ getLastMob(void)
return mob.back();
}
+std::vector<Entity*> World::
+getMobs(void)
+{
+ std::vector<Entity*> meme;
+ for (auto &m : mob) {
+ meme.push_back(m);
+ }
+ return meme;
+}
+
/**
* Get the interactable entity that is closest to the entity provided.
*/