Skip to content

Commit

Permalink
resource events
Browse files Browse the repository at this point in the history
resource bars save-load in editor
  • Loading branch information
spidamoo committed May 29, 2014
1 parent 451974b commit d5ddcb0
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 166 deletions.
253 changes: 129 additions & 124 deletions SoDlib/SoDlib.layout

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion SoDlib/include/CharacterActionCause.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const int ACTIONCAUSE_TYPE_ONGROUND = 201;
const int ACTIONCAUSE_TYPE_ANIM_TIME_PASSED = 301;
const int ACTIONCAUSE_TYPE_ANIM_TIME_IS = 302;

const int ACTIONCAUSE_TYPE_HAVE_STATUS = 401;
const int ACTIONCAUSE_TYPE_HAVE_STATUS = 401;

const int ACTIONCAUSE_TYPE_RESORCE_LESS = 501;
const int ACTIONCAUSE_TYPE_RESORCE_MORE = 502;


class CharacterActionCause
Expand Down
12 changes: 11 additions & 1 deletion SoDlib/include/CharacterResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@
class CharacterResource
{
public:
CharacterResource(Character* owner, CharacterResourcePrototype* prototype);
CharacterResource(Game* game, Character* owner, CharacterResourcePrototype* prototype);
virtual ~CharacterResource();

float getFullValue(), getCurrentValue(), getCurrentBoost(), getNormalRegen(), getCurrentRegen();
void update(); void regen(float dt); void add(float value);

void setLessEvent(float value, CharacterAction* action); void setMoreEvent(float value, CharacterAction* action);
void dispatchLessEvent(); void dispatchMoreEvent();
void findNextLessEvent(), findPrevLessEvent(); void findNextMoreEvent(), findPrevMoreEvent();
void findCurrentLessEvents(); void findCurrentMoreEvents();
void checkEvents();
protected:
Game* game;
Character* owner;
CharacterResourcePrototype* prototype;
float fullValue, currentValue, currentBoost, normalRegen, currentRegen;
float* lessEventValues; CharacterAction** lessEventActions; int lessEventsCount;
float* moreEventValues; CharacterAction** moreEventActions; int moreEventsCount;
int nextLessEvent, prevLessEvent, nextMoreEvent, prevMoreEvent;
private:
};

Expand Down
3 changes: 2 additions & 1 deletion SoDlib/include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class Game
void drawArc(float x, float y, float r, float start, float end, DWORD color, DWORD bgcolor);
void drawCircle(float x, float y, float r, DWORD color);
void drawCircle(float x, float y, float r, DWORD color, DWORD bgcolor);
void drawPoly(b2PolygonShape* poly, b2Transform transform, b2Vec2 origin, float scale, DWORD color, DWORD bgcolor);
void drawPoly(b2PolygonShape* poly, b2Transform transform, b2Vec2 origin, float scale, DWORD color, DWORD bgcolor);
void drawText(float x, float y, char* text);

void DrawPolygon (const b2Vec2 *vertices, int32 vertexCount, DWORD color, DWORD bgcolor);
void DrawPolygonScreen (const b2Vec2 *vertices, int32 vertexCount, DWORD color, DWORD bgcolor);
Expand Down
Binary file modified SoDlib/libSoDlib.a
Binary file not shown.
21 changes: 20 additions & 1 deletion SoDlib/src/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Character::Character(TiXmlElement* xml, Game * game, b2Vec2 origin) {

resources = new CharacterResource*[game->getCharacterResourcePrototypesCount()];
for (int i = 0; i < game->getCharacterResourcePrototypesCount(); i++) {
resources[i] = new CharacterResource( this, game->getCharacterResourcePrototype(i) );
resources[i] = new CharacterResource( game, this, game->getCharacterResourcePrototype(i) );
}

statusActions = new CharacterAction*[game->getCharacterStatusPrototypesCount()];
Expand Down Expand Up @@ -312,6 +312,21 @@ void Character::draw(bool schematicMode) {
2,
2,
0, 0xFF00FF00, 0xAA00FF00
);
}
for (int i = 0; i < game->getCharacterResourcePrototypesCount(); i++) {
char buffer[64];
sprintf(
buffer,
"%s: %.2f/%.2f",
game->getCharacterResourcePrototype(i)->getName(),
getResource(i)->getCurrentValue(),
getResource(i)->getFullValue()
);
game->drawText(
game->screenX(position.x),
game->screenY(position.y - halfHeight) - 30.0f - 30.0f * i,
buffer
);
}
} else {
Expand Down Expand Up @@ -543,6 +558,10 @@ void Character::update(float dt) {

if (currentStatus > -1 && statusPriority > movePriorities[currentMove]) {
statusActions[currentStatus]->perform(game, this);
}

for (int i =0; i < game->getCharacterResourcePrototypesCount(); i++) {
resources[i]->regen(dt);
}

control(dt);
Expand Down
149 changes: 147 additions & 2 deletions SoDlib/src/CharacterResource.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CharacterResource.h"

CharacterResource::CharacterResource(Character* _owner, CharacterResourcePrototype* _prototype) {
CharacterResource::CharacterResource(Game* _game, Character* _owner, CharacterResourcePrototype* _prototype) {
game = _game;
owner = _owner;
prototype = _prototype;

Expand All @@ -10,6 +11,20 @@ CharacterResource::CharacterResource(Character* _owner, CharacterResourcePrototy
normalRegen = prototype->getDefaultRegen();
currentRegen = normalRegen;

lessEventsCount = 0;
moreEventsCount = 0;

lessEventActions = new CharacterAction*[16];
lessEventValues = new float[16];

moreEventActions = new CharacterAction*[16];
moreEventValues = new float[16];

nextLessEvent = -1;
prevLessEvent = -1;
nextMoreEvent = -1;
prevMoreEvent = -1;

update();
}

Expand Down Expand Up @@ -49,11 +64,141 @@ void CharacterResource::regen(float dt) {
currentValue += currentRegen * dt;
if (currentValue > fullValue)
currentValue = fullValue;

checkEvents();
}
void CharacterResource::add(float value) {
currentValue += value;
if (currentValue > fullValue) {
currentValue = fullValue;
}
/// TODO: ñîáûòèÿ
checkEvents();
}

void CharacterResource::setLessEvent(float value, CharacterAction* action) {
for (int i = 0; i < lessEventsCount; i++) {
if (lessEventValues[i] == value) {
lessEventActions[i] = action;
return;
}
}
lessEventValues[lessEventsCount] = value;
lessEventActions[lessEventsCount] = action;

findCurrentLessEvents();
}
void CharacterResource::setMoreEvent(float value, CharacterAction* action) {
for (int i = 0; i < moreEventsCount; i++) {
if (moreEventValues[i] == value) {
moreEventActions[i] = action;
return;
}
}
moreEventValues[lessEventsCount] = value;
moreEventActions[lessEventsCount] = action;
findCurrentMoreEvents();
}

void CharacterResource::dispatchLessEvent() {
lessEventActions[nextLessEvent]->perform(game, owner);
}
void CharacterResource::dispatchMoreEvent() {
moreEventActions[nextMoreEvent]->perform(game, owner);
}

void CharacterResource::findNextLessEvent() {
prevLessEvent = nextLessEvent;
nextLessEvent = -1;
float maxLessValue = -INFINITY;
for (int i = 0; i < lessEventsCount; i++) {
if (lessEventValues[i] < lessEventValues[nextLessEvent] && lessEventValues[i] > maxLessValue) {
maxLessValue = lessEventValues[i];
nextLessEvent = i;
}
}
prevLessEvent = nextLessEvent;
}
void CharacterResource::findPrevLessEvent() {
nextLessEvent = prevLessEvent;
prevLessEvent = -1;
float minLessValue = INFINITY;
for (int i = 0; i < lessEventsCount; i++) {
if (lessEventValues[i] > lessEventValues[nextLessEvent] && lessEventValues[i] < minLessValue) {
minLessValue = lessEventValues[i];
prevLessEvent = i;
}
}
}
void CharacterResource::findNextMoreEvent() {
prevMoreEvent = nextMoreEvent;
nextMoreEvent = -1;
float minMoreValue = INFINITY;
for (int i = 0; i < moreEventsCount; i++) {
if (moreEventValues[i] > moreEventValues[nextMoreEvent] && moreEventValues[i] < minMoreValue) {
minMoreValue = moreEventValues[i];
nextMoreEvent = i;
}
}
}
void CharacterResource::findPrevMoreEvent() {
nextMoreEvent = prevMoreEvent;
prevMoreEvent = -1;
float maxMoreValue = -INFINITY;
for (int i = 0; i < moreEventsCount; i++) {
if (moreEventValues[i] < moreEventValues[nextMoreEvent] && moreEventValues[i] > maxMoreValue) {
maxMoreValue = moreEventValues[i];
prevMoreEvent = i;
}
}
}

void CharacterResource::findCurrentLessEvents() {
float maxLessValue = -INFINITY;
for (int i = 0; i < lessEventsCount; i++) {
if (lessEventValues[i] < (currentValue + currentBoost) / (fullValue + currentBoost) && lessEventValues[i] > maxLessValue) {
maxLessValue = lessEventValues[i];
nextLessEvent = i;
}
}
float minLessValue = INFINITY;
for (int i = 0; i < lessEventsCount; i++) {
if (lessEventValues[i] > (currentValue + currentBoost) / (fullValue + currentBoost) && lessEventValues[i] < minLessValue) {
minLessValue = lessEventValues[i];
prevLessEvent = i;
}
}
}
void CharacterResource::findCurrentMoreEvents() {
float minMoreValue = INFINITY;
for (int i = 0; i < moreEventsCount; i++) {
if (moreEventValues[i] > (currentValue + currentBoost) / (fullValue + currentBoost) && moreEventValues[i] < minMoreValue) {
minMoreValue = moreEventValues[i];
nextMoreEvent = i;
}
}
float maxMoreValue = -INFINITY;
for (int i = 0; i < moreEventsCount; i++) {
if (moreEventValues[i] < (currentValue + currentBoost) / (fullValue + currentBoost) && moreEventValues[i] > maxMoreValue) {
maxMoreValue = moreEventValues[i];
prevMoreEvent = i;
}
}
}

void CharacterResource::checkEvents() {
if ( nextLessEvent != -1 && (currentValue + currentBoost) / (fullValue + currentBoost) < lessEventValues[nextLessEvent] ) {
dispatchLessEvent();
findNextLessEvent();
}
else if ( prevLessEvent != -1 && (currentValue + currentBoost) / (fullValue + currentBoost) > lessEventValues[prevLessEvent] ) {
findPrevLessEvent();
}

if ( nextMoreEvent != -1 && (currentValue + currentBoost) / (fullValue + currentBoost) > moreEventValues[nextMoreEvent] ) {
dispatchMoreEvent();
findNextMoreEvent();
}
else if ( prevMoreEvent != -1 && (currentValue + currentBoost) / (fullValue + currentBoost) < moreEventValues[prevMoreEvent] ) {
findPrevMoreEvent();
}
}
12 changes: 11 additions & 1 deletion SoDlib/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ void Game::drawGame() {
}
}

if (schematicDrawMode) {
for (int i = 0; i < groundLinesCount; i++) {
groundLines[i]->debugDraw();
}
}

et = clock();
updateCounter(COUNTER_DRAW_MAP, map_t + et - st);
st = clock();
Expand Down Expand Up @@ -447,7 +453,11 @@ void Game::drawPoly(b2PolygonShape* poly, b2Transform transform, b2Vec2 origin,
origin.y + first.y * scale,
color
);
}
}
void Game::drawText(float x, float y, char* text) {
defaultFont->Render(x, y, HGETEXT_CENTER, text);
}

void Game::DrawPolygon (const b2Vec2 *vertices, int32 vertexCount, DWORD color, DWORD bgcolor) {
b2Vec2 first = getPixelsPerMeter() * getScaleFactor() * (vertices[0] - cameraPos);
b2Vec2 prev = first;
Expand Down
41 changes: 41 additions & 0 deletions characterEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ bool*** frameHotSpotShow = new bool**[256];
char** moveNames = new char*[256];
char** bodyNames = new char*[256];

float* resourceBarWidths = new float[64];
float* resourceBarHeights = new float[64];
float* resourceBarPositionX = new float[64];
float* resourceBarPositionY = new float[64];
DWORD* resourceBarEmptyColors = new DWORD[64];
DWORD* resourceBarFullColors = new DWORD[64];
DWORD* resourceBarBoostColors = new DWORD[64];
DWORD* resourceBarFrameColors = new DWORD[64];

int currentTab = 0;
float currentTime = 0;
float frameProgress = 0;
Expand Down Expand Up @@ -957,6 +966,20 @@ bool saveCharacter(char* fn) {
characterElem->LinkEndChild( moveElem );
//cxn->SetDoubleAttribute("timeout", 123.456);
}

for (int i = 0; i < game->getCharacterResourcePrototypesCount(); i++) {
TiXmlElement* element = new TiXmlElement( "resource" );
characterElem->LinkEndChild( element );

element->SetDoubleAttribute("width", resourceBarWidths[i]);
element->SetDoubleAttribute("height", resourceBarHeights[i]);
element->SetDoubleAttribute("x", resourceBarPositionX[i]);
element->SetDoubleAttribute("y", resourceBarPositionY[i]);
element->SetAttribute("full_color", resourceBarFullColors[i]);
element->SetAttribute("empty_color", resourceBarEmptyColors[i]);
element->SetAttribute("boost_color", resourceBarBoostColors[i]);
element->SetAttribute("frame_color", resourceBarFrameColors[i]);
}
doc.LinkEndChild( characterElem );
doc.SaveFile(fn);
}
Expand Down Expand Up @@ -1157,6 +1180,24 @@ bool loadCharacter(char* fn) {
moveElem = moveElem->NextSiblingElement("move");
}
movesCount = i;

element = characterElem->FirstChildElement("resource");
i = 0;
while (element) {
resourceBarWidths[i] = atof( element->Attribute("width") );
resourceBarHeights[i] = atof( element->Attribute("height") );
resourceBarPositionX[i] = atof( element->Attribute("x") );
resourceBarPositionY[i] = atof( element->Attribute("y") );

resourceBarFullColors[i] = atoi( element->Attribute("full_color") );
resourceBarEmptyColors[i] = atoi( element->Attribute("empty_color") );
resourceBarBoostColors[i] = atoi( element->Attribute("boost_color") );
resourceBarFrameColors[i] = atoi( element->Attribute("frame_color") );

i++;
element = element->NextSiblingElement("resource");
}

printf("character loaded\n");
setMove(0);
}
Expand Down
12 changes: 6 additions & 6 deletions conditionEditor/conditionEditor.depend
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# depslib dependency file v1.0
1401097238 source:d:\work\my\sod\conditioneditor\main.cpp
1401174316 source:d:\work\my\sod\conditioneditor\main.cpp
<stdio.h>
<hge.h>
<hgefont.h>
Expand Down Expand Up @@ -344,7 +344,7 @@
<stdio.h>
<string.h>

1400929315 d:\work\my\sod\sodlib\include\game.h
1401216357 d:\work\my\sod\sodlib\include\game.h
<SoDlib.h>

1400947304 d:\work\my\sod\sodlib\include\conditionprototype.h
Expand All @@ -362,19 +362,19 @@
1400533750 d:\work\my\sod\sodlib\include\effect.h
<SoDlib.h>

1401002091 d:\work\my\sod\sodlib\include\characteractioncause.h
1401221948 d:\work\my\sod\sodlib\include\characteractioncause.h
<SoDlib.h>

1397805132 d:\work\my\sod\sodlib\include\characteractioneffect.h
<SoDlib.h>

1400438363 d:\work\my\sod\sodlib\include\characteraction.h
1401176577 d:\work\my\sod\sodlib\include\characteraction.h
<SoDlib.h>

1400535025 d:\work\my\sod\sodlib\include\characterhotspot.h
<SoDlib.h>

1401021838 d:\work\my\sod\sodlib\include\character.h
1401178013 d:\work\my\sod\sodlib\include\character.h
<SoDlib.h>

1396800264 d:\work\my\sod\sodlib\include\nonplayercharacter.h
Expand Down Expand Up @@ -420,6 +420,6 @@
1400947378 d:\work\my\sod\sodlib\include\characterparam.h
<SoDlib.h>

1400946674 d:\work\my\sod\sodlib\include\characterresource.h
1401315643 d:\work\my\sod\sodlib\include\characterresource.h
<SoDlib.h>

Loading

0 comments on commit d5ddcb0

Please sign in to comment.