00001 00002 // Copyright (C) 2004-2007 by The Allacrost Project 00003 // All Rights Reserved 00004 // 00005 // This code is licensed under the GNU GPL version 2. It is free software 00006 // and you may modify it and/or redistribute it under the terms of this license. 00007 // See http://www.gnu.org/copyleft/gpl.html for details. 00009 00016 #include <iostream> 00017 00018 #include "utils.h" 00019 00020 #include "system.h" 00021 00022 #include "map.h" 00023 #include "map_actions.h" 00024 #include "map_dialogue.h" 00025 #include "map_objects.h" 00026 #include "map_sprites.h" 00027 00028 using namespace std; 00029 00030 using namespace hoa_utils; 00031 using namespace hoa_system; 00032 00033 namespace hoa_map { 00034 00035 namespace private_map { 00036 00037 // ***************************************************************************** 00038 // **************************** ActionPathMove ********************************* 00039 // ***************************************************************************** 00040 00041 void ActionPathMove::Execute() { 00042 // TODO: Check if we already have a previously computed path and if it is still valid, use it. 00043 if (path.empty()) { 00044 MapMode::_current_map->_FindPath(_sprite, path, destination); 00045 } 00046 00047 if (!path.empty()) { 00048 _sprite->moving = true; 00049 if (_sprite->y_position > path[current_node].row) { // Need to move north 00050 if (_sprite->x_position > path[current_node].col) { // Need to move northwest 00051 _sprite->SetDirection(NORTHWEST); 00052 } 00053 else { 00054 if (_sprite->x_position < path[current_node].col) { // Need to move northeast 00055 _sprite->SetDirection(NORTHEAST); 00056 } 00057 else { // Just move north 00058 _sprite->SetDirection(NORTH); 00059 } 00060 } 00061 } 00062 else if (_sprite->y_position < path[current_node].row) { // Need to move south 00063 if (_sprite->x_position > path[current_node].col) // Need to move southwest 00064 _sprite->SetDirection(SOUTHWEST); 00065 else if (_sprite->x_position < path[current_node].col) // Need to move southeast 00066 _sprite->SetDirection(SOUTHEAST); 00067 else // Just move south 00068 _sprite->SetDirection(SOUTH); 00069 } 00070 else if (_sprite->x_position > path[current_node].col) { // Need to move west 00071 _sprite->SetDirection(WEST); 00072 } 00073 else if (_sprite->x_position < path[current_node].col) { // Need to move east 00074 _sprite->SetDirection(EAST); 00075 } 00076 else { // The x and y position have reached the node, update to the next node 00077 current_node++; 00078 if (current_node >= path.size()) { // Destination has been reached 00079 current_node = 0; 00080 _finished = true; 00081 _sprite->moving = false; 00082 } 00083 } 00084 } 00085 } // void ActionPathMove::Execute() 00086 00087 // ***************************************************************************** 00088 // **************************** ActionRandomMove ******************************* 00089 // ***************************************************************************** 00090 00091 void ActionRandomMove::Execute() { 00092 _sprite->moving = true; 00093 direction_timer += SystemManager->GetUpdateTime(); 00094 movement_timer += SystemManager->GetUpdateTime(); 00095 00096 // Check if we should change the sprite's direction 00097 if (direction_timer >= total_direction_time) { 00098 direction_timer -= total_direction_time; 00099 _sprite->SetRandomDirection(); 00100 } 00101 00102 if (movement_timer >= total_movement_time) { 00103 movement_timer = 0; 00104 _finished = true; 00105 _sprite->moving = false; 00106 } 00107 } 00108 00109 // ***************************************************************************** 00110 // ***************************** ActionAnimate ********************************* 00111 // ***************************************************************************** 00112 00113 void ActionAnimate::Execute() { 00114 timer += SystemManager->GetUpdateTime(); 00115 00116 if (timer > display_times[current_frame]) { 00117 timer = 0; 00118 current_frame++; 00119 00120 // If we've finished displaying the final frame... 00121 if (current_frame >= frames.size()) { 00122 current_frame = 0; 00123 00124 // If this animation is not infinitely looped, increment the loop counter 00125 if (loops >= 0) { 00126 loop_count++; 00127 if (loop_count > loops) { 00128 _finished = true; 00129 loop_count = 0; 00130 return; 00131 } 00132 } 00133 } 00134 00135 dynamic_cast<MapSprite*>(_sprite)->SetCurrentAnimation(frames[current_frame]); 00136 } 00137 00138 } // void ActionAnimate::Execute() 00139 00140 } // namespace private_map 00141 00142 } // namespace hoa_map
1.5.1