You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
3.7 KiB

#include "mover.h"
Mover::Mover()
{
x = 0; y = 0;
px = 0; py = 0;
speed = 0;
dir = 0; pdir = 0;
move_status = 0;
move_status_max = 0;
move_status_buf = 0;
move_status_buf_max = 0;
floor = NULL;
map = NULL;
}
Mover::Mover(IMAGE* floor, Map* map) :floor(floor), map(map)
{
x = BLOCK_SIZE * 1 + BLOCK_SIZE / 2;
y = BLOCK_SIZE * 1 + BLOCK_SIZE / 2;
px = 0;
py = 0;
speed = 2;
dir = DIR_NONE;
pdir = DIR_NONE;
move_status = 0;
move_status_max = 2;
move_status_buf = 0;
move_status_buf_max = 10;
ImportImage("image/Pinky.png");
}
bool Mover::Move()
{
if (dir == DIR_NONE)
return false;
bool ss = true;
int nx = x;
int ny = y;
int dx = (nx % BLOCK_SIZE) - BLOCK_SIZE / 2;
int dy = (ny % BLOCK_SIZE) - BLOCK_SIZE / 2;
switch (dir)
{
case DIR_UP:
{
if (abs(dx) < MOVE_TOLERENCE)
{
ny -= speed;
nx -= dx;
}
else
ss = false;
break;
}
case DIR_DOWN:
{
if (abs(dx) < MOVE_TOLERENCE)
{
ny += speed;
nx -= dx;
}
else
ss = false;
break;
}
case DIR_LEFT:
{
if (abs(dy) < MOVE_TOLERENCE)
{
nx -= speed;
ny -= dy;
}
else
ss = false;
break;
}
case DIR_RIGHT:
{
if (abs(dy) < MOVE_TOLERENCE)
{
nx += speed;
ny -= dy;
}
else
ss = false;
break;
}
}
if (ss == false)
{
if (dir != pdir)
{
dir = pdir;
return Move();
}
else
{
return false;
}
}
if (nx < 0)
nx = GAME_WIDTH;
else if (nx > GAME_WIDTH)
nx = 0;
if (ny < 0)
ny = GAME_HEIGHT;
else if (ny > GAME_HEIGHT)
ny = 0;
// ÅжÏÊÇ·ñײǽ
int ni = (nx - BLOCK_SIZE / 2) / BLOCK_SIZE;
int nj = (ny - BLOCK_SIZE / 2) / BLOCK_SIZE;
switch (dir)
{
case DIR_UP:
case DIR_LEFT:
{
if (map->GetMap(ni, nj)==3)
ss = false;
break;
}
case DIR_DOWN:
{
if (map->GetMap(ni, nj + 1)==3)
ss = false;
break;
}
case DIR_RIGHT:
{
if (nx == 0)
{
if(map->GetMap(ni, nj)==3)
ss = false;
}
else if (map->GetMap(ni + 1, nj)==3)
ss = false;
break;
}
}
if (ss == false)
{
if (dir != pdir)
{
dir = pdir;
return Move();
}
else
{
return false;
}
}
move_status_buf++;
if (move_status_buf >= move_status_buf_max)
{
move_status_buf = 0;
move_status++;
if (move_status >= move_status_max)
move_status = 0;
}
x = nx;
y = ny;
pdir = dir;
return true;
}
void Mover::SetXY(int x, int y)
{
this->x = x;
this->y = y;
}
void Mover::GetXY(int& px, int& py)
{
px = x;
py = y;
}
void Mover::SetDir(int dir)
{
if (dir == DIR_NONE)
return;
this->dir = dir;
}
void Mover::Draw()
{
// ¸ù¾İͼÏñÖĞĞÄ×ø±ê ¼ÆËã ×óÉϽÇ×ø±ê
int draw_x, draw_y;
draw_x = px - PERSON_SIZE / 2;
draw_y = py - PERSON_SIZE / 2;
// ¼ÆËã ¶ÔÓ¦µØ°åͼÏñ
SetWorkingImage(floor);
IMAGE foot = IMAGE(PERSON_SIZE, PERSON_SIZE);
getimage(&foot, draw_x, draw_y, PERSON_SIZE, PERSON_SIZE);
//// ²Á³ı¾ÉͼÏñ£¨Ê¹ÓõذåͼÏñ¸²¸Ç£©
SetWorkingImage(NULL);
putimage(draw_x, draw_y, &foot);
// »æÖÆĞÂͼÏñ
draw_x = x - PERSON_SIZE / 2;
draw_y = y - PERSON_SIZE / 2;
int num = 0;
switch (dir)
{
case DIR_UP: num = IMG_UP + move_status * 4; break;
case DIR_DOWN: num = IMG_DOWN + move_status * 4; break;
case DIR_LEFT: num = IMG_LEFT + move_status * 4; break;
case DIR_RIGHT: num = IMG_RIGHT + move_status * 4; break;
}
putimage(draw_x, draw_y, &img[num]);
// Ìæ»»¾É×ø±ê
px = x;
py = y;
}
void Mover::ImportImage(std::string path)
{
char* pathc = str2char(path);
ImportImage(pathc);
}
void Mover::ImportImage(char* path)
{
IMAGE whole_img;
loadimage(&whole_img, _T(path), PERSON_SIZE * move_status_max, PERSON_SIZE * 4, true);
SetWorkingImage(&whole_img);
int num = 0;
for (int i = 0; i < move_status_max; i++)
{
for (int j = 0; j < 4; j++)
{
num = i * 4 + j;
getimage(&img[num], i * PERSON_SIZE, j * PERSON_SIZE, PERSON_SIZE, PERSON_SIZE);
}
}
SetWorkingImage();
}