Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
19 KB
Referenced Files
None
Subscribers
None
diff --git a/src/BaseCreatureEntity.cpp b/src/BaseCreatureEntity.cpp
index 5e91598..42df96d 100644
--- a/src/BaseCreatureEntity.cpp
+++ b/src/BaseCreatureEntity.cpp
@@ -1,368 +1,375 @@
#include "BaseCreatureEntity.h"
#include "sfml_game/ImageManager.h"
#include "Constants.h"
#include "WitchBlastGame.h"
BaseCreatureEntity::BaseCreatureEntity(sf::Texture* image, float x = 0.0f, float y = 0.0f, int spriteWidth = -1, int spriteHeight = -1)
: CollidingSpriteEntity (image, x, y, spriteWidth, spriteHeight )
{
hurting = false;
hurtingType = ShotTypeStandard;
shadowFrame = -1;
setMap(game().getCurrentMap(), TILE_WIDTH, TILE_HEIGHT, OFFSET_X, OFFSET_Y);
hpDisplay = 0;
movingStyle = movWalking;
for (int i = 0; i < NB_SPECIAL_STATES; i++)
{
specialState[i].type = (enumSpecialState)i;
specialState[i].active = false;
specialState[i].timer = 0.0f;
specialState[i].parameter = 0.0f;
}
for (int i = 0; i < NB_RESISTANCES; i++)
{
resistance[i] = ResistanceStandard;
}
recoil.active = false;
facingDirection = 2;
}
int BaseCreatureEntity::getHp()
{
return hp;
}
int BaseCreatureEntity::getHpMax()
{
return hpMax;
}
void BaseCreatureEntity::setHp(int hp)
{
this->hp = hp;
}
void BaseCreatureEntity::setHpMax(int hpMax)
{
this->hpMax = hpMax;
}
int BaseCreatureEntity::getHpDisplay()
{
return hpDisplay;
}
IntCoord BaseCreatureEntity::getCurrentTile()
{
int xMap = (x - OFFSET_X) / TILE_WIDTH;
int yMap = (y - OFFSET_Y) / TILE_HEIGHT;
return (IntCoord(xMap, yMap));
}
BaseCreatureEntity::enumMovingStyle BaseCreatureEntity::getMovingStyle()
{
return movingStyle;
}
float BaseCreatureEntity::animateStates(float delay)
{
for (int i = 0; i < NB_SPECIAL_STATES; i++)
{
if (specialState[i].active)
{
specialState[i].timer -= delay;
if (specialState[i].timer <= 0.0f) specialState[i].active = false;
}
}
if (specialState[SpecialStateIce].active) delay *= specialState[SpecialStateIce].parameter;
return delay;
}
void BaseCreatureEntity::animateColors(float delay)
{
// no color
sprite.setColor(sf::Color(255, 255, 255, 255 ));
if (hurting and hp > 0)
{
hurtingDelay -= delay;
if (hurtingDelay > 0.0f)
{
int fadeColor = (sf::Uint8)((HURTING_DELAY - hurtingDelay) * 255);
if (hurtingDelay > HURTING_DELAY) fadeColor = 0;
if (hurtingType == ShotTypeIce)
sprite.setColor(sf::Color(fadeColor, fadeColor, 255, 255 )); // blue
else
sprite.setColor(sf::Color(255, fadeColor, fadeColor, 255 )); // red
}
else
{
hurting = false;
sprite.setColor(sf::Color(255, 255, 255, 255 ));
}
}
if (specialState[SpecialStateIce].active) sprite.setColor(sf::Color(100, 100, 255, 255 ));
}
void BaseCreatureEntity::animateRecoil(float delay)
{
// recoil
if (recoil.active)
{
recoil.velocity.x *= 0.97f;
recoil.velocity.y *= 0.97f;
recoil.timer -= delay;
if (recoil.timer <= 0.0f)
{
recoil.active = false;
computeFacingDirection();
// TODO ?
}
}
}
void BaseCreatureEntity::animatePhysics(float delay)
{
velocity.x *= viscosity;
velocity.y *= viscosity;
float velx = velocity.x;
float vely = velocity.y;
if (recoil.active)
{
if (recoil.stun)
{
velx = 0.0f;
vely = 0.0f;
}
velx += recoil.velocity.x;
vely += recoil.velocity.y;
}
spin *= viscosity;
angle += spin * delay;
+ if (isCollidingWithMap())
+ {
+ stuck();
+ }
+ else
+ {
if ((int)velx > 0)
{
x += velx * delay;
if (collideWithMap(DIRECTION_LEFT))
{
x = (float)((int)x);
while (collideWithMap(DIRECTION_LEFT))
x--;
collideMapRight();
}
else if (x > map->getWidth() * tileWidth + offsetX)
{
exitMap(DIRECTION_RIGHT);
}
}
else if ((int)velx < 0)
{
x += velx * delay;
if (collideWithMap(DIRECTION_RIGHT))
{
x = (float)((int)x);
while (collideWithMap(DIRECTION_RIGHT))
x++;
collideMapLeft();
}
else if (x < offsetX)
{
exitMap(DIRECTION_LEFT);
}
}
+ }
vely += weight * delay;
if ( vely > maxY) vely = maxY;
if ((int)vely > 0)
{
y += vely * delay;
if (collideWithMap(DIRECTION_BOTTOM))
{
y = (float)((int)y);
while (collideWithMap(DIRECTION_BOTTOM))
y--;
collideMapBottom();
}
}
else if ((int)vely < 0)
{
y += vely * delay;
if (collideWithMap(DIRECTION_TOP))
{
y = (float)((int)y);
while (collideWithMap(DIRECTION_TOP))
y++;
collideMapTop();
}
}
if (lifetime > 0)
{
if (age >= lifetime) isDying = true;
}
age += delay;
}
void BaseCreatureEntity::animate(float delay)
{
if (hpDisplay > hp) hpDisplay--;
else if (hpDisplay < hp) hpDisplay++;
delay = animateStates(delay);
animateColors(delay);
animateRecoil(delay);
animatePhysics(delay);
z = y + height/2;
}
void BaseCreatureEntity::render(sf::RenderTarget* app)
{
if (!isDying && shadowFrame > -1)
{
// shadow
sprite.setTextureRect(sf::IntRect(shadowFrame * width, 0, width, height));
app->draw(sprite);
}
CollidingSpriteEntity::render(app);
#ifdef SHOW_BOUNDING_BOX
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(boundingBox.left, boundingBox.top)),
sf::Vertex(sf::Vector2f(boundingBox.left + boundingBox.width, boundingBox.top)),
sf::Vertex(sf::Vector2f(boundingBox.left + boundingBox.width, boundingBox.top)),
sf::Vertex(sf::Vector2f(boundingBox.left + boundingBox.width, boundingBox.top + boundingBox.height)),
sf::Vertex(sf::Vector2f(boundingBox.left + boundingBox.width, boundingBox.top + boundingBox.height)),
sf::Vertex(sf::Vector2f(boundingBox.left, boundingBox.top + boundingBox.height)),
sf::Vertex(sf::Vector2f(boundingBox.left, boundingBox.top + boundingBox.height)),
sf::Vertex(sf::Vector2f(boundingBox.left, boundingBox.top))
};
app->draw(line, 8, sf::Lines);
#endif
}
void BaseCreatureEntity::calculateBB()
{
}
bool BaseCreatureEntity::collideWithMap(int direction)
{
calculateBB();
int xTile0 = (boundingBox.left - offsetX) / tileWidth;
int xTilef = (boundingBox.left + boundingBox.width - offsetX) / tileWidth;
int yTile0 = (boundingBox.top - offsetY) / tileHeight;
int yTilef = (boundingBox.top + boundingBox.height - offsetY) / tileHeight;
if (boundingBox.top < 0) yTile0 = -1;
for (int xTile = xTile0; xTile <= xTilef; xTile++)
for (int yTile = yTile0; yTile <= yTilef; yTile++)
{
if (movingStyle == movWalking)
{
if ( dynamic_cast<DungeonMap*>(map)->isWalkable(xTile, yTile) == false ) return true;
}
else if (movingStyle == movFlying)
{
if ( dynamic_cast<DungeonMap*>(map)->isFlyable(xTile, yTile) == false ) return true;
}
}
return false;
}
bool BaseCreatureEntity::determineSatusChance(enumStateResistance resistance, int level)
{
bool hit = true;
switch (resistance)
{
case ResistanceVeryLow:
case ResistanceLow:
case ResistanceStandard: hit = true; break;
case ResistanceHigh: hit = rand() % 8 <= level * 2; break;
case ResistanceVeryHigh: hit = rand() % 10 <= level * 2; break;
case ResistanceImmune: hit = false; break;
}
return hit;
}
bool BaseCreatureEntity::hurt(int damages, enumShotType hurtingType, int level)
{
hurting = true;
hurtingDelay = HURTING_DELAY;
this->hurtingType = hurtingType;
if (hurtingType == ShotTypeIce
&& determineSatusChance(resistance[ResistanceFrozen], level)) // && specialState[SpecialStateIce].resistance > ResistanceImmune)
{
// frozen ?
specialState[SpecialStateIce].active = true;
specialState[SpecialStateIce].timer = STATUS_FROZEN_DELAY[level];
specialState[SpecialStateIce].parameter = STATUS_FROZEN_MULT[level];
// damages
}
hp -= damages;
if (hp <= 0)
{
hp = 0;
prepareDying();
}
return true;
}
void BaseCreatureEntity::prepareDying()
{
dying();
}
void BaseCreatureEntity::dying()
{
isDying = true;
}
void BaseCreatureEntity::computeFacingDirection()
{
if (abs((int)velocity.x) > 0 || abs((int)velocity.y) > 0)
{
if (abs((int)velocity.x) > abs((int)velocity.y))
{
if (velocity.x > 0.0f) facingDirection = 6;
else facingDirection = 4;
}
else
{
if (velocity.y > 0.0f) facingDirection = 2;
else facingDirection = 8;
}
}
}
void BaseCreatureEntity::giveRecoil(bool stun, Vector2D velocity, float timer)
{
if (!(recoil.active && recoil.stun))
{
recoil.active = true;
recoil.stun = stun;
recoil.velocity = velocity;
recoil.timer = timer;
}
}
void BaseCreatureEntity::inflictsRecoilTo(BaseCreatureEntity* targetEntity)
{
}
diff --git a/src/sfml_game/CollidingSpriteEntity.cpp b/src/sfml_game/CollidingSpriteEntity.cpp
index cd65a9d..8f75787 100644
--- a/src/sfml_game/CollidingSpriteEntity.cpp
+++ b/src/sfml_game/CollidingSpriteEntity.cpp
@@ -1,280 +1,298 @@
/** This file is part of Witch Blast.
*
* Witch Blast is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Witch Blast is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Witch Blast. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CollidingSpriteEntity.h"
CollidingSpriteEntity::CollidingSpriteEntity(sf::Texture* image, float x, float y, int width, int height)
: SpriteEntity(image, x, y, width, height)
{
maxY = 1000.0f;
}
CollidingSpriteEntity::~CollidingSpriteEntity()
{
}
GameMap* CollidingSpriteEntity::getMap()
{
return map;
}
void CollidingSpriteEntity::setMap(GameMap* map, int tileWidth, int tileHeight, int offsetX, int offsetY)
{
this->map = map;
setTileDimensions(tileWidth, tileHeight);
this->offsetX = offsetX;
this->offsetY = offsetY;
}
void CollidingSpriteEntity::setTileDimensions(int width, int height)
{
tileWidth = width;
tileHeight = height;
}
void CollidingSpriteEntity::render(sf::RenderTarget* app)
{
SpriteEntity::render(app);
}
void CollidingSpriteEntity::animate(float delay)
{
velocity.x *= viscosity;
velocity.y *= viscosity;
spin *= viscosity;
angle += spin * delay;
velocity.y += weight * delay;
if ((int)velocity.x > 0)
{
x += velocity.x * delay;
if (collideWithMap(DIRECTION_LEFT))
{
x = (float)((int)x);
while (collideWithMap(DIRECTION_LEFT))
x--;
collideMapRight();
}
else if (x > map->getWidth() * tileWidth + offsetX)
{
exitMap(DIRECTION_RIGHT);
}
}
else if ((int)velocity.x < 0)
{
x += velocity.x * delay;
if (collideWithMap(DIRECTION_RIGHT))
{
x = (float)((int)x);
while (collideWithMap(DIRECTION_RIGHT))
x++;
collideMapLeft();
}
else if (x < offsetX)
{
exitMap(DIRECTION_LEFT);
}
}
velocity.y += weight * delay;
if ( velocity.y > maxY) velocity.y = maxY;
- if ((int)velocity.y > 0)
+ if (isCollidingWithMap())
{
- y += velocity.y * delay;
-
- if (collideWithMap(DIRECTION_BOTTOM))
- {
- y = (float)((int)y);
- while (collideWithMap(DIRECTION_BOTTOM))
- y--;
- collideMapBottom();
- }
+ stuck();
}
- else if ((int)velocity.y < 0)
+ else
{
- y += velocity.y * delay;
- //calculateBB();
+ if ((int)velocity.y > 0)
+ {
+ y += velocity.y * delay;
- if (collideWithMap(DIRECTION_TOP))
+ if (collideWithMap(DIRECTION_BOTTOM))
+ {
+ y = (float)((int)y);
+ while (collideWithMap(DIRECTION_BOTTOM))
+ y--;
+ collideMapBottom();
+ }
+ }
+ else if ((int)velocity.y < 0)
{
- y = (float)((int)y);
- while (collideWithMap(DIRECTION_TOP))
- y++;
- collideMapTop();
+ y += velocity.y * delay;
+ //calculateBB();
+
+ if (collideWithMap(DIRECTION_TOP))
+ {
+ y = (float)((int)y);
+ while (collideWithMap(DIRECTION_TOP))
+ y++;
+ collideMapTop();
+ }
}
}
-
-
// for platforming
/* if ((int)weight == 0)
{
if (!(isOnGround()))
{
makeFall();
}
}*/
if (lifetime > 0)
{
if (age >= lifetime) isDying = true;
}
age += delay;
}
void CollidingSpriteEntity::calculateBB()
{
boundingBox.left = (int)x - width / 2;
boundingBox.width = width;
boundingBox.top = (int)y - height / 2;
boundingBox.height = height;
}
void CollidingSpriteEntity::makeFall()
{
weight = normalWeight;
}
bool CollidingSpriteEntity::isOnGround()
{
calculateBB();
int xTile0 = (boundingBox.left - offsetX) / tileWidth;
int xTilef = (boundingBox.left + boundingBox.width - offsetX) / tileWidth;
int yTile = (boundingBox.top + boundingBox.height + 1 - offsetY) / tileHeight;
for (int xTile = xTile0; xTile <= xTilef; xTile++)
{
if (map->isDownBlocking(xTile, yTile)) return true;
}
return false;
}
void CollidingSpriteEntity::exitMap(int direction)
{
if (direction == 0) {}
}
void CollidingSpriteEntity::collideMapRight()
{
velocity.x = 0.0f;
}
void CollidingSpriteEntity::collideMapLeft()
{
velocity.x = 0.0f;
}
void CollidingSpriteEntity::collideMapTop()
{
velocity.y = 0.0f;
}
void CollidingSpriteEntity::collideMapBottom()
{
velocity.y = 0.0f;
}
bool CollidingSpriteEntity::collideWithMap(int direction)
{
calculateBB();
int xTile0 = (boundingBox.left - offsetX) / tileWidth;
int xTilef = (boundingBox.left + boundingBox.width - offsetX) / tileWidth;
int yTile0 = (boundingBox.top - offsetY) / tileHeight;
int yTilef = (boundingBox.top + boundingBox.height - offsetY) / tileHeight;
if (boundingBox.top < 0) yTile0 = -1;
for (int xTile = xTile0; xTile <= xTilef; xTile++)
for (int yTile = yTile0; yTile <= yTilef; yTile++)
{
switch (direction)
{
case DIRECTION_LEFT:
if (map->isLeftBlocking(xTile, yTile)) return true;
break;
case DIRECTION_RIGHT:
if (map->isRightBlocking(xTile, yTile)) return true;
break;
case DIRECTION_TOP:
if (map->isUpBlocking(xTile, yTile)) return true;
break;
case DIRECTION_BOTTOM:
if (map->isDownBlocking(xTile, yTile)) return true;
break;
}
}
return false;
}
+bool CollidingSpriteEntity::isCollidingWithMap()
+{
+ return (collideWithMap(DIRECTION_BOTTOM)
+ || collideWithMap(DIRECTION_TOP)
+ || collideWithMap(DIRECTION_LEFT)
+ || collideWithMap(DIRECTION_RIGHT));
+}
+
+void CollidingSpriteEntity::stuck()
+{
+ onDying();
+}
+
bool CollidingSpriteEntity::collideWithEntity(CollidingSpriteEntity* entity)
{
calculateBB();
entity->calculateBB();
return boundingBox.intersects(entity->boundingBox);
/*if (boundingBox.left > entity->boundingBox.left + entity->boundingBox.width) return false;
if (boundingBox.Right < entity->boundingBox.Left) return false;
if (boundingBox.Top > entity->boundingBox.Bottom) return false;
if (boundingBox.Bottom < entity->boundingBox.Top) return false;*/
return true;
}
void CollidingSpriteEntity::testSpriteCollisions()
{
EntityManager::EntityList* entityList = EntityManager::getEntityManager()->getList();
EntityManager::EntityList::iterator it;
EntityManager::EntityList::iterator oldit = entityList->begin ();
for (it = entityList->begin (); it != entityList->end ();)
{
GameEntity* entity = *it;
CollidingSpriteEntity* collidingEntity = dynamic_cast<CollidingSpriteEntity*>(entity);
if (collidingEntity != NULL)
{
readCollidingEntity(collidingEntity);
}
oldit = it;
it++;
} // end for*/
}
void CollidingSpriteEntity::readCollidingEntity(CollidingSpriteEntity* entity)
{
if (entity == NULL) return;
}
void CollidingSpriteEntity::collideEntity(CollidingSpriteEntity* entity)
{
if (entity == NULL) return;
}
diff --git a/src/sfml_game/CollidingSpriteEntity.h b/src/sfml_game/CollidingSpriteEntity.h
index 1cb4f3a..c846d86 100644
--- a/src/sfml_game/CollidingSpriteEntity.h
+++ b/src/sfml_game/CollidingSpriteEntity.h
@@ -1,76 +1,78 @@
/** This file is part of Witch Blast.
*
* Witch Blast is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Witch Blast is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Witch Blast. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COLLIDINGSPRITEENTITY_H_INCLUDED
#define COLLIDINGSPRITEENTITY_H_INCLUDED
#include "SpriteEntity.h"
#include "GameMap.h"
// Basis class for Jouster
class CollidingSpriteEntity : public SpriteEntity
{
public:
CollidingSpriteEntity(sf::Texture* image, float x = 0.0f, float y = 0.0f, int width = -1, int height = -1);
~CollidingSpriteEntity();
GameMap* getMap();
void setMap(GameMap* map, int tileWidth, int tileHeight, int offsetX, int offsetY);
void setTileDimensions(int width, int height);
virtual void render(sf::RenderTarget* app);
virtual void animate(float delay);
virtual void calculateBB();
virtual void makeFall();
virtual bool collideWithMap(int direction);
+ virtual bool isCollidingWithMap();
+ virtual void stuck();
virtual bool collideWithEntity(CollidingSpriteEntity* entity);
enum directionEnum
{
DIRECTION_RIGHT,
DIRECTION_LEFT,
DIRECTION_TOP,
DIRECTION_BOTTOM
};
bool isOnGround();
protected:
GameMap* map; // map to test collisions with
sf::IntRect boundingBox; // BoundingBox
int tileWidth;
int tileHeight;
int offsetX;
int offsetY;
float normalWeight;
float maxY;
virtual void exitMap(int direction);
virtual void collideMapRight();
virtual void collideMapLeft();
virtual void collideMapTop();
virtual void collideMapBottom();
virtual void collideEntity(CollidingSpriteEntity* entity);
virtual void testSpriteCollisions();
virtual void readCollidingEntity(CollidingSpriteEntity* entity);
};
#endif // COLLIDINGSPRITEENTITY_H_INCLUDED

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:39 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68478
Default Alt Text
(19 KB)

Event Timeline