]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Things flash red when hit
authordrumsetmonkey <abelleisle@roadrunner.com>
Fri, 13 May 2016 16:48:11 +0000 (12:48 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Fri, 13 May 2016 16:48:11 +0000 (12:48 -0400)
include/common.hpp
include/entities.hpp
main.cpp
shaders/world.frag
shaders/world.vert
src/entities.cpp
src/ui.cpp

index 2442f5c0a47c1763b4e71f56d17631ad05199d40..240cd01cd8037ab2fd22b15d4d0ff739a8cc4a1e 100644 (file)
@@ -229,6 +229,7 @@ extern GLuint worldShader;
 extern GLint worldShader_attribute_coord;
 extern GLint worldShader_attribute_tex;
 extern GLint worldShader_uniform_texture;
+extern GLint worldShader_uniform_color;
 
 /**
  *     Prints a formatted debug message to the console, along with the callee's file and line
index 7487bbebf3da45e468e9f882317323132a2509d8..1c3b6ff86a9f663cfca5570cb2d2672a3ebe69ff 100644 (file)
@@ -181,6 +181,11 @@ protected:
        // TODO
        float targetx;
 
+       // the cooldown display (red overlay)
+       float hitDuration;
+
+       // the max cooldown display
+       float maxHitDuration;
 public:
        // contains the entity's coordinates, in pixels
        vec2 loc;
index a9138c787f17405abb8d025e0bf70568068226e9..9ff36891a6b2ed7335e949e4f9fbeb889120ea8e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -76,6 +76,7 @@ GLint worldShader_attribute_coord;
 GLint worldShader_attribute_tex;
 GLint worldShader_uniform_texture;
 GLint worldShader_uniform_transform;
+GLint worldShader_uniform_color;
 
 // keeps a simple palette of colors for single-color draws
 GLuint colorIndex;
@@ -214,21 +215,22 @@ int main(int argc, char *argv[]){
        /**
         *      Creating the text shader and its attributes/uniforms
         */
-       textShader = create_program("shaders/new.vert", "shaders/new.frag");
-       textShader_attribute_coord = get_attrib(textShader, "coord2d");
-       textShader_attribute_tex = get_attrib(textShader, "tex_coord");
-       textShader_uniform_texture = get_uniform(textShader, "sampler");
-       textShader_uniform_transform = get_uniform(textShader, "ortho");
-    textShader_uniform_color = get_uniform(textShader, "tex_color");
+       textShader =                                    create_program("shaders/new.vert", "shaders/new.frag");
+       textShader_attribute_coord =    get_attrib(textShader, "coord2d");
+       textShader_attribute_tex =              get_attrib(textShader, "tex_coord");
+       textShader_uniform_texture =    get_uniform(textShader, "sampler");
+       textShader_uniform_transform =  get_uniform(textShader, "ortho");
+    textShader_uniform_color =                 get_uniform(textShader, "tex_color");
 
        /**
         *      Creating the world's shader and its attributes/uniforms
         */
-       worldShader = create_program("shaders/world.vert", "shaders/world.frag");
-       worldShader_attribute_coord = get_attrib(worldShader, "coord2d");
-       worldShader_attribute_tex = get_attrib(worldShader, "tex_coord");
-       worldShader_uniform_texture = get_uniform(worldShader, "sampler");
+       worldShader =                                   create_program("shaders/world.vert", "shaders/world.frag");
+       worldShader_attribute_coord =   get_attrib(worldShader, "coord2d");
+       worldShader_attribute_tex =     get_attrib(worldShader, "tex_coord");
+       worldShader_uniform_texture =   get_uniform(worldShader, "sampler");
        worldShader_uniform_transform = get_uniform(worldShader, "ortho");
+       worldShader_uniform_color =     get_uniform(worldShader, "tex_color");
 
        //glEnable(GL_MULTISAMPLE);
 
@@ -387,7 +389,7 @@ void render() {
     glUniform4f(textShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
     glUseProgram(worldShader);
        glUniformMatrix4fv(worldShader_uniform_transform, 1, GL_FALSE, glm::value_ptr(ortho));
-
+       glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
        /**************************
        **** RENDER STUFF HERE ****
        **************************/
index c45b4a0548f7f7eaf7c2b86f52db1850779e368a..80af175c856488d1018f377695022b0cf3ff1b42 100644 (file)
@@ -1,10 +1,11 @@
 uniform sampler2D sampler;
 
 varying vec2 texCoord;
+varying vec4 color;
 
 void main(){
-    vec4 color = texture2D(sampler, vec2(texCoord.x, 1-texCoord.y));
-    if(color.a <= .1)
+    vec4 pixTex = texture2D(sampler, vec2(texCoord.x, 1-texCoord.y));
+    if(pixTex.a <= .1)
         discard;
-    gl_FragColor = color;
+    gl_FragColor = pixTex * color;
 }
index 1bedfd3feb3b631419702dfc5190abe54c97b550..ce7fa5aa3df6e17078a964f33c1b7e72151c6f68 100644 (file)
@@ -1,11 +1,14 @@
 attribute vec3 coord2d;
 attribute vec2 tex_coord;
 
+uniform vec4 tex_color;
 uniform mat4 ortho;
 
 varying vec2 texCoord;
+varying vec4 color;
 
 void main(){
+       color = tex_color;
     texCoord = tex_coord;
     gl_Position = ortho * vec4(coord2d.xyz, 1.0);
 }
index 6b5e1d3f0b1a7e2dcbeee06b781b38ff87a43f2e..205e848a0fe4e13105bd733061090a7464cf0bd0 100644 (file)
@@ -99,6 +99,8 @@ Entity::Entity(void)
        ticksToUse = 0;
        hitCooldown = 0;
 
+       hitDuration = maxHitDuration = 0;
+
        inv = nullptr;
        name = nullptr;
 }
@@ -118,16 +120,21 @@ void Entity::spawn(float x, float y)
                name[0] = '\0';
        else
                randGetomName(this);
+
+       setCooldown(0);
 }
 
 void Entity::takeHit(unsigned int _health, unsigned int cooldown)
 {
        if (hitCooldown <= 1) {
+               std::cout << "Taking hit " << std::endl;
                // modify variables
                health = fmax(health - _health, 0);
                forcedMove = true;
                hitCooldown = cooldown;
 
+               hitDuration = maxHitDuration = 350.0f;
+
                // pushback
                vel.x = player->left ? -0.5f : 0.5f;
                vel.y = 0.2f;
@@ -146,7 +153,8 @@ void Entity::setCooldown(unsigned int c)
 
 void Entity::handleHits(void)
 {
-       hitCooldown = fmax(hitCooldown - game::time::getDeltaTime(), 0);
+       hitCooldown = fmax(static_cast<int>(hitCooldown - game::time::getDeltaTime()), 0);
+       hitDuration = fmax(hitDuration - game::time::getDeltaTime(), 0);
 
        if (!forcedMove)
                return;
@@ -418,23 +426,32 @@ void Entity::draw(void)
        else
                glColor3ub(255,255,255);*/
 
-               glUseProgram(worldShader);
-               glUniform1i(worldShader_uniform_texture, 0);
-               glEnableVertexAttribArray(worldShader_attribute_coord);
-               glEnableVertexAttribArray(worldShader_attribute_tex);
+       glUseProgram(worldShader);
+       // make the entity hit flash red
+       if (maxHitDuration-hitDuration) {
+               float flashAmt = 1-(hitDuration/maxHitDuration);
+               glUniform4f(worldShader_uniform_color, 1.0, flashAmt, flashAmt, 1.0);
+       }
 
-               glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
-               if (left)
-                       glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL);
-               else
-                       glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord);
-               glDrawArrays(GL_TRIANGLES, 0, 6);
+       glUniform1i(worldShader_uniform_texture, 0);
+       glEnableVertexAttribArray(worldShader_attribute_coord);
+       glEnableVertexAttribArray(worldShader_attribute_tex);
+
+       glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
+       if (left)
+               glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL);
+       else
+               glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord);
+       glDrawArrays(GL_TRIANGLES, 0, 6);
+
+       glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
 NOPE:
 if (near && type != MOBT)
        ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-game::HLINE/2,name);
 if (health != maxHealth) {
 
-       glBindTexture(GL_TEXTURE_2D,colorIndex);
+       static GLuint frontH = Texture::genColor(Color(255,0,0));
+       static GLuint backH =  Texture::genColor(Color(150,0,0));
        glUniform1i(worldShader_uniform_texture, 0);
 
        GLfloat coord_back[] = {
@@ -456,33 +473,23 @@ if (health != maxHealth) {
                loc.x,                              loc.y + height + game::HLINE * 2, z,
                loc.x,                              loc.y + height,                   z,
        };
-
-       static const vec2 index1 = Texture::getIndex(Color(0,0,0));
-       GLfloat back_tex[] = {
-               float(.25*index1.x), float(.125*index1.y),
-               float(.25*index1.x), float(.125*index1.y),
-               float(.25*index1.x), float(.125*index1.y),
-
-               float(.25*index1.x), float(.125*index1.y),
-               float(.25*index1.x), float(.125*index1.y),
-               float(.25*index1.x), float(.125*index1.y),
+       
+       glBindTexture(GL_TEXTURE_2D, backH);
+       GLfloat tex[] = { 0.0, 0.0,
+                                                  1.0, 0.0,
+                                                  1.0, 1.0,
+                                                  
+                                                  1.0, 1.0,
+                                                  0.0, 1.0,
+                                                  0.0, 0.0,
        };
        glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coord_back);
-       glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, back_tex);
+       glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
        glDrawArrays(GL_TRIANGLES, 0, 6);
 
-       static const vec2 index2 = Texture::getIndex(Color(255,0,0));
-       GLfloat front_tex[] = {
-               float(.25*index2.x), float(.125*index2.y),
-               float(.25*index2.x), float(.125*index2.y),
-               float(.25*index2.x), float(.125*index2.y),
-
-               float(.25*index2.x), float(.125*index2.y),
-               float(.25*index2.x), float(.125*index2.y),
-               float(.25*index2.x), float(.125*index2.y),
-       };
+       glBindTexture(GL_TEXTURE_2D, frontH);
        glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, coord_front);
-       glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, front_tex);
+       glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
        glDrawArrays(GL_TRIANGLES, 0, 6);
 }
 
index 51edb8653b60cd08f9d5fb54078ec13a14cb1a89..838ac253638dc216c5d2602a0c3459faafd78519 100644 (file)
@@ -820,7 +820,7 @@ namespace ui {
                                         offset.x + (SCREEN_WIDTH / 10) - 40,    offset.y + (SCREEN_HEIGHT / 5),     1.0};
 
                 glActiveTexture(GL_TEXTURE0); 
-                glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[0]));
+                glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[1]));
                 glUniform1i(textShader_uniform_texture, 0);
                 glUseProgram(textShader);
 
@@ -831,7 +831,7 @@ namespace ui {
                 glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex);
                 glDrawArrays(GL_TRIANGLES, 0 ,6);
                
-                glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[1]));
+                glBindTexture(GL_TEXTURE_2D, getItemTexture(merchTrade.item[0]));
 
                 glVertexAttribPointer(textShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, right_item);
                 glVertexAttribPointer(textShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, item_tex);
@@ -1103,6 +1103,7 @@ EXIT:
 
                auto indoor = dynamic_cast<IndoorWorld *>(currentWorld);
                Mob *m; // ;lkjfdsa
+               Entity *en; // used for interaction
 
                SDL_Event e;
 
@@ -1153,15 +1154,15 @@ EXIT:
                                if ((action::make = e.button.button & SDL_BUTTON_RIGHT))
                                        /*player->inv->invHover =*/ edown = false;
 
-                               if (dialogBoxExists || pageTexReady) {
+                       if (dialogBoxExists || pageTexReady) {
                                        // right click advances dialog
                                        if ((e.button.button & SDL_BUTTON_RIGHT))
                                                dialogAdvance();
                                } else {
                                        // left click uses item
                                        if (e.button.button & SDL_BUTTON_LEFT) {
-                                               if ((m = currentWorld->getNearMob(*player)) != nullptr) {
-                                                       player->inv->currentAddInteract(m);
+                                               if ((en = currentWorld->getNearMob(*player)) != nullptr) {
+                                                       player->inv->currentAddInteract(en);
                                                        player->inv->useCurrent();
                                                }
                                        }