1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include "NPC.h"
int times = 0;
int random;
void NPC::Update(Map &map, Player &actor)
{
SDL_Color Black = {0, 0, 0, 0};
//MsgSurf = TTF_RenderText_Solid(Res.stdFONT, Name.c_str(), Black);
MsgSurf = TTF_RenderText_Solid(Res.stdFONT, Int2String::int2string(random).c_str(), Black);
if(Health > 0)
{
// If the player is moving, add one value to the delay
if(moving == true)
{
delay++;
}
else
{
// If he/she isn't moving, make sure the player
// doesn't get animated
delay = 0;
}
// If the delay reaches the point where it
// needs to add a frame, go to the next frame
// and reset the delay
if(delay == delaychange)
{
frame++;
delay = 0;
}
// If the frame is larger than the frames in the
// image, reset the frame to 0
if(frame > 3)
{
frame = 0;
}
// Make copies of the X and Y values
// incase we collide with something, so
// we can revert the position
int prevx = X;
int prevy = Y;
HandleMovement();
// Add the velocity to the
// proper coordinates
X += Xvel;
Y += Yvel;
if(Yvel != 0) {moving = true;} else {moving = false;}
if(Xvel != 0) {moving = true;} else {moving = false;}
// Update the collision box position
Box.x = X + 5;
Box.y = Y + 24;
if(moving == false) {Xvel = 0; Yvel = 0;}
if(Collision::ChkCollision(actor.getBox(), Box))
{
Collided = true;
actor.setCollided(true);
X = prevx;
Y = prevy;
}
Collided = map.TouchSolid(Box);
// If there is a X collision, revert the X coordinate
if((Box.x < 0) || (Box.x + Box.w > map.getMapW()) || Collided)
{
//std::cout << "Collision Detected.\n";
X = prevx;
moving = false;
}
// If there is a Y collision, revert the Y coordinate
if((Box.y < 0) || (Box.y + Box.h > map.getMapH()) || Collided)
{
//std::cout << "Collision Detected.\n";
Y = prevy;
moving = false;
}
}
}
void NPC::Render(SDL_Surface* Dest)
{
if(Health > 0)
{
switch(Position)
{
case LEFT:
Draw::DrawGFX(Graphic, LClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
//LeftAnim.RenderObj(Dest);
break;
case RIGHT:
Draw::DrawGFX(Graphic, RClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
//RightAnim.RenderObj(Dest);
break;
case UP:
Draw::DrawGFX(Graphic, UClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
break;
case DOWN:
Draw::DrawGFX(Graphic, DClips[frame], Dest, X - monitor.getCamera().x, Y - monitor.getCamera().y);
break;
}
}
}
void NPC::DrawTextBox(SDL_Surface* Dest)
{
if(Talking == true)
{
SDL_FillRect(Dest, &Window[0], SDL_MapRGB(Dest->format, 0, 0, 0));
SDL_FillRect(Dest, &Window[1], SDL_MapRGB(Dest->format, 6, 61, 121));
// Draw the message
//Draw::DrawGFX(MsgSurf, Dest, Window[1].x + 10, Window[1].y + 5);
Draw::DrawGFX(MsgSurf, Dest, 200, 10);
}
Draw::DrawGFX(MsgSurf, Dest, 200, 10);
}
void NPC::HandleMovement()
{
bool temp = false;
times++;
if(moving)
{
if(Position == LEFT && moving == true) {Xvel = -Vel;}
if(Position == RIGHT && moving == true) {Xvel = Vel;}
if(Position == UP && moving == true) {Yvel = -Vel;}
if(Position == DOWN && moving == true) {Yvel = Vel;}
}
if(times == WaitTime)
{
times = 0;
// This should be a while loop
while(temp == false)
{
// Generate a random number :)
srand(SDL_GetTicks());
random = rand();
random = random % 4;
if(random < 4 || random < 0)
{
if(random == LEFT)
{
Position = LEFT;
Xvel = -Vel;
moving = true;
}
if(random == RIGHT)
{
Position = RIGHT;
Xvel = Vel;
moving = true;
}
if(random == UP)
{
Position = UP;
Yvel = -Vel;
moving = true;
}
if(random == DOWN)
{
Position = DOWN;
Yvel = Vel;
moving = true;
}
if(random == 4)
{
Xvel = 0;
Yvel = 0;
temp = false;
}
temp = true;
}
}
}
}
|