diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-01-21 13:31:36 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-01-21 13:31:36 +0000 |
commit | e0a956b4045ca09631d9a9a616ca675f2c1f4a78 (patch) | |
tree | b98a0bf6970c76a226436effed21f44611015e4f /src | |
parent | ad12a9189f33e65de827219eb573a6e63c7862c9 (diff) | |
download | mana-e0a956b4045ca09631d9a9a616ca675f2c1f4a78.tar.gz mana-e0a956b4045ca09631d9a9a616ca675f2c1f4a78.tar.bz2 mana-e0a956b4045ca09631d9a9a616ca675f2c1f4a78.tar.xz mana-e0a956b4045ca09631d9a9a616ca675f2c1f4a78.zip |
Some refactoring of Being class.
Diffstat (limited to 'src')
-rw-r--r-- | src/being.cpp | 113 | ||||
-rw-r--r-- | src/being.h | 40 | ||||
-rw-r--r-- | src/game.cpp | 54 | ||||
-rw-r--r-- | src/graphic/graphic.cpp | 42 |
4 files changed, 139 insertions, 110 deletions
diff --git a/src/being.cpp b/src/being.cpp index 18b78289..b9632424 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -24,6 +24,7 @@ #include <stdio.h> #include "astar.h" #include "being.h" +#include "game.h" Being *player_node = NULL; @@ -117,24 +118,11 @@ void sort() { beings.sort(BeingCompare()); } -void empty_path(Being *being) { - if (being) { - PATH_NODE *temp = being->path; - PATH_NODE *next; - while (temp) { - next = temp->next; - delete temp; - temp = next; - } - being->path = NULL; - } -} - Being::Being() { id = 0; job = 0; action = 0; frame = 0; path = NULL; speech = NULL; speech_time = 0; - tick_time = 0; speed = 150; + walk_time = 0; speed = 150; emotion = 0; emotion_time = 0; text_x = 0; text_y = 0; hair_style = 1; hair_color = 1; @@ -144,17 +132,94 @@ Being::Being() { } Being::~Being() { - if (path) { - PATH_NODE *temp = path; - PATH_NODE *next; - while (temp) { - next = temp->next; - delete temp; - temp = next; - } - path = NULL; - } + clearPath(); if (speech) { free(speech); } } + +void Being::clearPath() { + PATH_NODE *temp = path; + PATH_NODE *next; + while (temp) { + next = temp->next; + delete temp; + temp = next; + } + path = NULL; +} + +void Being::setPath(PATH_NODE *path) +{ + clearPath(); + this->path = path; + if (path != NULL) { + direction = 0; + if (path->next) { + if (path->next->x > path->x && path->next->y > path->y) + direction = SE; + else if (path->next->x < path->x && path->next->y > path->y) + direction = SW; + else if (path->next->x > path->x && path->next->y < path->y) + direction = NE; + else if (path->next->x < path->x && path->next->y < path->y) + direction = NW; + else if (path->next->x > path->x) + direction = EAST; + else if (path->next->x < path->x) + direction = WEST; + else if (path->next->y > path->y) + direction = SOUTH; + else if (path->next->y < path->y) + direction = NORTH; + } + PATH_NODE *pn = path; + this->path = path->next; + free(pn); + x = this->path->x; + y = this->path->y; + action = WALK; + walk_time = tick_time; + frame = 0; + } +} + +bool Being::hasPath() +{ + return path != NULL; +} + +void Being::nextStep() +{ + if (path->next) { + int old_x, old_y, new_x, new_y; + old_x = path->x; + old_y = path->y; + path = path->next; + new_x = path->x; + new_y = path->y; + direction = 0; + + if (new_x > old_x) { + if (new_y > old_y) direction = SE; + else if (new_y < old_y) direction = NE; + else direction = EAST; + } + else if (new_x < old_x) { + if (new_y > old_y) direction = SW; + else if (new_y < old_y) direction = NW; + else direction = WEST; + } + else { + if (new_y > old_y) direction = SOUTH; + else if (new_y < old_y) direction = NORTH; + } + + x = path->x; + y = path->y; + } else { + action = STAND; + } + frame = 0; + walk_time = tick_time; +} diff --git a/src/being.h b/src/being.h index 072cc9ae..ed6b4e31 100644 --- a/src/being.h +++ b/src/being.h @@ -38,6 +38,9 @@ struct PATH_NODE { }; class Being { + private: + PATH_NODE *path; + public: unsigned int id; unsigned short job; @@ -46,20 +49,46 @@ class Being { unsigned char type; unsigned char action; unsigned char frame; - PATH_NODE *path; char *speech; unsigned char speech_time; int speech_color; - unsigned short tick_time; + unsigned short walk_time; unsigned short speed; unsigned char emotion; unsigned char emotion_time; unsigned int text_x, text_y; // temp solution to fix speech position unsigned short hair_style, hair_color; unsigned short weapon; - + + /** + * Constructor. + */ Being(); - ~Being(); + + /** + * Destructor. + */ + ~Being(); + + /** + * Removes all path nodes from this being. + */ + void clearPath(); + + /** + * Sets the new path for this being. + */ + void setPath(PATH_NODE *path); + + /** + * Returns wether this being has a path to follow. + */ + bool hasPath(); + + /** + * Makes this being take the next step of his path. + */ + void nextStep(); }; /** Removes all beings from the list */ @@ -87,9 +116,6 @@ unsigned int find_monster(unsigned short x, unsigned short y); /** Sort beings in vertical order */ void sort(); -/** Remove all path nodes from a being */ -void empty_path(Being *being); - extern Being *player_node; extern std::list<Being*> beings; diff --git a/src/game.cpp b/src/game.cpp index 3498345c..9f9be942 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -283,7 +283,7 @@ void do_input() src_x = x; src_y = y; player_node->action = WALK; - player_node->tick_time = tick_time; + player_node->walk_time = tick_time; player_node->y = y - 1; player_node->direction = NORTH; } @@ -299,7 +299,7 @@ void do_input() src_x = x; src_y = y; player_node->action = WALK; - player_node->tick_time = tick_time; + player_node->walk_time = tick_time; player_node->y = y + 1; player_node->direction = SOUTH; } @@ -313,7 +313,7 @@ void do_input() src_x = x; src_y = y; player_node->action = WALK; - player_node->tick_time = tick_time; + player_node->walk_time = tick_time; player_node->x = x - 1; player_node->direction = WEST; } @@ -327,7 +327,7 @@ void do_input() src_x = x; src_y = y; player_node->action = WALK; - player_node->tick_time = tick_time; + player_node->walk_time = tick_time; player_node->x = x + 1; player_node->direction = EAST; } @@ -342,7 +342,7 @@ void do_input() attack(player_node->x, player_node->y, player_node->direction); - player_node->tick_time = tick_time; + player_node->walk_time = tick_time; } } @@ -440,7 +440,6 @@ void do_parse() { char *temp; char direction; Being *being = NULL; - PATH_NODE *pn; int len, n_items; // We need at least 2 bytes to identify a packet @@ -529,7 +528,7 @@ void do_parse() { being->speed = 150; // Else division by 0 when calculating frame } being->job = RFIFOW(14); - empty_path(being); + being->clearPath(); being->x = get_x(RFIFOP(46)); being->y = get_y(RFIFOP(46)); being->direction = get_direction(RFIFOP(46)); @@ -539,12 +538,12 @@ void do_parse() { } else { if (being) { - empty_path(being); + being->clearPath(); being->x = get_x(RFIFOP(46)); being->y = get_y(RFIFOP(46)); being->direction = get_direction(RFIFOP(46)); being->frame = 0; - being->tick_time = tick_time; + being->walk_time = tick_time; being->action = STAND; } } @@ -557,7 +556,7 @@ void do_parse() { if(being->job>110) { being->action = MONSTER_DEAD; being->frame = 0; - being->tick_time = tick_time; + being->walk_time = tick_time; } //else being->action = DEAD; //remove_node(RFIFOL(2)); @@ -577,7 +576,7 @@ void do_parse() { being->y = get_y(RFIFOP(46)); being->direction = get_direction(RFIFOP(46)); add_node(being); - being->tick_time = tick_time; + being->walk_time = tick_time; being->frame = 0; being->speed = RFIFOW(6); being->hair_color = RFIFOW(28); @@ -599,36 +598,9 @@ void do_parse() { being->job = RFIFOW(14); add_node(being); } - empty_path(being); - being->path = calculate_path(get_src_x(RFIFOP(50)), + being->setPath(calculate_path(get_src_x(RFIFOP(50)), get_src_y(RFIFOP(50)),get_dest_x(RFIFOP(50)), - get_dest_y(RFIFOP(50))); - if (being->path!=NULL) { - direction = 0; - if (being->path->next) { - if (being->path->next->x>being->path->x && being->path->next->y>being->path->y) direction = SE; - else if (being->path->next->x < being->path->x && being->path->next->y>being->path->y) direction = SW; - else if (being->path->next->x > being->path->x && being->path->next->y<being->path->y) direction = NE; - else if (being->path->next->x < being->path->x && being->path->next->y<being->path->y) direction = NW; - else if (being->path->next->x > being->path->x) - direction = EAST; - else if (being->path->next->x < being->path->x) - direction = WEST; - else if (being->path->next->y > being->path->y) - direction = SOUTH; - else if (being->path->next->y < being->path->y) - direction = NORTH; - } - pn = being->path; - being->path = being->path->next; - free(pn); - being->x = being->path->x; - being->y = being->path->y; - being->direction = direction; - being->action = WALK; - being->tick_time = tick_time; - being->frame = 0; - } + get_dest_y(RFIFOP(50)))); break; // Being moving case 0x01da: @@ -654,7 +626,7 @@ void do_parse() { direction = NORTH; else being->action = STAND; if (being->action == WALK) - being->tick_time = tick_time; + being->walk_time = tick_time; being->x = get_dest_x(RFIFOP(50)); being->y = get_dest_y(RFIFOP(50)); being->direction = direction; diff --git a/src/graphic/graphic.cpp b/src/graphic/graphic.cpp index c5fd63eb..c5bed789 100644 --- a/src/graphic/graphic.cpp +++ b/src/graphic/graphic.cpp @@ -398,7 +398,7 @@ void Engine::draw() if (being->action != STAND && being->action != SIT && being->action != DEAD) { being->frame = - (get_elapsed_time(being->tick_time) * 4) / (being->speed); + (get_elapsed_time(being->walk_time) * 4) / (being->speed); if (being->frame >= 4) { being->frame = 0; @@ -431,45 +431,11 @@ void Engine::draw() if (being->action != STAND) { being->frame = - (get_elapsed_time(being->tick_time) * 4) / (being->speed); + (get_elapsed_time(being->walk_time) * 4) / (being->speed); if (being->frame >= 4) { - if (being->action != MONSTER_DEAD && being->path) { - if (being->path->next) { - int old_x, old_y, new_x, new_y; - old_x = being->path->x; - old_y = being->path->y; - being->path = being->path->next; - new_x = being->path->x; - new_y = being->path->y; - direction = 0; - - if (new_x > old_x) { - if (new_y > old_y) direction = SE; - else if (new_y < old_y) direction = NE; - else direction = EAST; - } - else if (new_x < old_x) { - if (new_y > old_y) direction = SW; - else if (new_y < old_y) direction = NW; - else direction = WEST; - } - else { - if (new_y > old_y) direction = SOUTH; - else if (new_y < old_y) direction = NORTH; - } - - being->x = being->path->x; - being->y = being->path->y; - being->direction = direction; - } else { - being->action = STAND; - } - if (being->action != MONSTER_DEAD) { - being->frame = 0; - } - being->tick_time = tick_time; - //being->frame = 0; + if (being->action != MONSTER_DEAD && being->hasPath()) { + being->nextStep(); } } } |