00001 #include <SDL/SDL.h>
00002 #include <string.h>
00003 #include <iostream>
00004 #include <math.h>
00005
00006 #include "objmgr.h"
00007 #include "bear.h"
00008 #include "util.h"
00009 #include "sound.h"
00010
00011
00012 Bear::Bear(ObjectManager &om) : GameObject(om, "Bear")
00013 {
00014 vector <Surface *> v = Util_parseImage(BEAR_IMG_FILE);
00015
00016
00017 sprite = new Sprite(v, "BEAR");
00018 SetCollidable(true);
00019
00020 SetDamagable(true);
00021 SetDamage (2);
00022
00023
00024 movingDir = EAST;
00025 walking = true;
00026
00027
00028 walk_velocity.dx = walk_velocity.dy = 0;
00029 walk_velocity.pertime.msecs = 25;
00030 walk_velocity.pertime.secs = 0;
00031
00032
00033 acceleration = 0.1;
00034
00035 xMinVel = -4.0;
00036 xMaxVel = 4.0;
00037 yMinVel = -4.0;
00038 yMaxVel = 4.0;
00039
00040
00041 sprite->SetFrame(0);
00042 sprite->ReverseAnimation(false);
00043 sprite->SetAnimateTime(150);
00044 sprite->SetAnimationStartFrame(0);
00045 sprite->SetAnimationEndFrame(4);
00046 sprite->EnableAnimation(true);
00047
00048 lastwalk = Util_GetTime();
00049 }
00050
00051 Bear::~Bear()
00052 {
00053 }
00054
00055 void Bear::think()
00056 {
00057 float tempX, tempY;
00058 double nx = 0, ny = 0;
00059 bool e = false, s = false, w = false, n = false;
00060 int count = Util_TimePassed(&lastwalk, walk_velocity.pertime.msecs);
00061
00062
00063 tempX = (float)(OM.GetMainChar())->GetX();
00064 tempY = (float)(OM.GetMainChar())->GetY();
00065
00066 if(Xpos < tempX) {
00067 walk_velocity.dx += count * acceleration;
00068 if(walk_velocity.dx > xMaxVel) walk_velocity.dx = xMaxVel;
00069 nx = count * walk_velocity.dx;
00070 }
00071 if(Xpos > tempX) {
00072 walk_velocity.dx -= count * acceleration;
00073 if(walk_velocity.dx < xMinVel) walk_velocity.dx = xMinVel;
00074
00075 nx =count *walk_velocity.dx;
00076 }
00077 if(Ypos < tempY) {
00078 walk_velocity.dy += count * acceleration;
00079 if(walk_velocity.dy > yMaxVel) walk_velocity.dy = yMaxVel;
00080 ny = count * walk_velocity.dy;
00081 }
00082 if(Ypos > tempY) {
00083 walk_velocity.dy -= count * acceleration;
00084 if(walk_velocity.dy < yMinVel) walk_velocity.dy = yMinVel;
00085 ny = count * walk_velocity.dy;
00086 }
00087
00088
00089 OM.GetSubWorld()->SubWorldToScreen (tempX, tempY);
00090 setWalk(tempX, tempY);
00091 SetPosition(GetX() + nx, GetY() + ny);
00092 }
00093
00094 void Bear::setWalk(double tempX, double tempY)
00095 {
00096
00097
00098
00099
00100
00101
00102 double den = (tempX - (sprite->GetX() + sprite->GetWidth() / 2));
00103 double angle;
00104 if (den == 0) {
00105 if (tempY < (GetY() + sprite->GetHeight() / 2))
00106 angle = 270;
00107 else
00108 angle = 90;
00109 } else {
00110 double radians =
00111 atan((tempY - (sprite->GetY() + sprite->GetHeight() / 2)) / (den));
00112
00113 angle = radians * 180.0 / NNCMath::PI;
00114
00115
00116 }
00117
00118 if (angle < 0 && (tempX > (sprite->GetX() + (sprite->GetWidth() / 2)))) {
00119 angle = 360 - -angle;
00120 } else if (angle >= 0 && (tempX > (sprite->GetX() + (sprite->GetWidth() / 2)))) {
00121
00122 } else if (angle >= 0 && (tempX < (sprite->GetX() + (sprite->GetWidth() / 2)))) {
00123 angle = 180.0 + angle;
00124 } else if (angle < 0 && (tempX < (sprite->GetX() + (sprite->GetWidth() / 2)))) {
00125 angle = 180 - -angle;
00126 }
00127
00128 if (angle < 22.5) {
00129
00130 if(movingDir != EAST) {
00131 movingDir = EAST;
00132 sprite->SetFrame(0);
00133 sprite->ReverseAnimation(false);
00134 sprite->SetAnimateTime(150);
00135 sprite->SetAnimationStartFrame(0);
00136 sprite->SetAnimationEndFrame(4);
00137 sprite->EnableAnimation(true);
00138 }
00139 } else if (angle < 67.5) {
00140
00141 if(movingDir != SOUTHEAST) {
00142 movingDir = SOUTHEAST;
00143 sprite->SetFrame(4);
00144 sprite->ReverseAnimation(false);
00145 sprite->SetAnimateTime(150);
00146 sprite->SetAnimationStartFrame(4);
00147 sprite->SetAnimationEndFrame(8);
00148 sprite->EnableAnimation(true);
00149 }
00150 } else if (angle < 112.5) {
00151
00152
00153 if(movingDir != SOUTH) {
00154 movingDir = SOUTH;
00155 sprite->SetFrame(8);
00156 sprite->ReverseAnimation(false);
00157 sprite->SetAnimateTime(150);
00158 sprite->SetAnimationStartFrame(8);
00159 sprite->SetAnimationEndFrame(12);
00160 sprite->EnableAnimation(true);
00161 }
00162
00163 } else if (angle < 157.5) {
00164
00165 if(movingDir != SOUTHWEST) {
00166 movingDir = SOUTHWEST;
00167 sprite->SetFrame(12);
00168 sprite->ReverseAnimation(false);
00169 sprite->SetAnimateTime(150);
00170 sprite->SetAnimationStartFrame(12);
00171 sprite->SetAnimationEndFrame(16);
00172 sprite->EnableAnimation(true);
00173 }
00174
00175 } else if (angle < 202.5) {
00176
00177 if(movingDir != WEST) {
00178 movingDir = WEST;
00179 sprite->SetFrame(16);
00180 sprite->ReverseAnimation(false);
00181 sprite->SetAnimateTime(150);
00182 sprite->SetAnimationStartFrame(16);
00183 sprite->SetAnimationEndFrame(20);
00184 sprite->EnableAnimation(true);
00185 }
00186
00187 } else if (angle < 247.5) {
00188
00189
00190 if(movingDir != NORTHWEST) {
00191 movingDir = NORTHWEST;
00192 sprite->SetFrame(20);
00193 sprite->ReverseAnimation(false);
00194 sprite->SetAnimateTime(150);
00195 sprite->SetAnimationStartFrame(20);
00196 sprite->SetAnimationEndFrame(24);
00197 sprite->EnableAnimation(true);
00198 }
00199
00200
00201 } else if (angle < 292.5) {
00202
00203
00204 if(movingDir != NORTH) {
00205 movingDir = NORTH;
00206 sprite->SetFrame(24);
00207 sprite->ReverseAnimation(false);
00208 sprite->SetAnimateTime(150);
00209 sprite->SetAnimationStartFrame(24);
00210 sprite->SetAnimationEndFrame(28);
00211 sprite->EnableAnimation(true);
00212 }
00213 } else if (angle < 337.5) {
00214
00215
00216 if(movingDir != NORTHEAST) {
00217 movingDir = NORTHEAST;
00218 sprite->SetFrame(28);
00219 sprite->ReverseAnimation(false);
00220 sprite->SetAnimateTime(150);
00221 sprite->SetAnimationStartFrame(28);
00222 sprite->SetAnimationEndFrame(32);
00223 sprite->EnableAnimation(true);
00224
00225 }
00226
00227 } else {
00228
00229 if(movingDir != EAST) {
00230 movingDir = EAST;
00231 sprite->SetFrame(0);
00232 sprite->ReverseAnimation(false);
00233 sprite->SetAnimateTime(150);
00234 sprite->SetAnimationStartFrame(0);
00235 sprite->SetAnimationEndFrame(4);
00236 sprite->EnableAnimation(true);
00237 }
00238 }
00239 }
00240
00241 bool Bear::isWalking() {
00242 return walking;
00243 }
00244