仕様としては、
・壁に当たったら別方向に移動
・他のキャラクターと当たったら別方向に移動(どう種族を含む)
これを実装したいと思います。
将来的には360度全方位に移動したいと思っているのですが、最初は4方向に絞りたいと思います。
今は、壁に当たったら別方向に移動までできたのですが、キャラクターとの当たり判定ができません。
#include "DxLib.h"
#define NIKU_KAZU 5 //肉食の数
#define SOU_KAZU 5 //草食の数
#define KUSA_KAZU 5 //草の数
#define UP 0 //上
#define RIGHT 1 //右
#define DOWN 2 //下
#define LEFT 3 //左
int i, j;
typedef struct Chara {
int x;
int y;
int color;
int life;
}Chara; //キャラクターの構造体
Chara carni[NIKU_KAZU];
Chara herbi[SOU_KAZU];
Chara weed[KUSA_KAZU];
void DrawWall();
void CharaInit();
void DrawCarni();
void DrawHerbi();
void DrawWeed();
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode(TRUE);
if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
{
return -1; // エラーが起きたら直ちに終了
}
CharaInit(); //キャラクターの初期化
// キーが押されるまでループ(キー判定には『CheckHitKeyAll』を使用)
while(ProcessMessage() == 0 && CheckHitKey(KEY_INPUT_ESCAPE) == 0 )
{
ClsDrawScreen();
DrawWall(); //壁の表示
DrawCarni(); //肉食動物の移動と表示
DrawHerbi(); //草食動物の移動と表示
DrawWeed(); //草の表示
ScreenFlip();
}
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
//壁の表示
void DrawWall() {
DrawBox(0, 0, 640, 10, GetColor(255,255,255),TRUE);
DrawBox(0, 0, 10, 480, GetColor(255,255,255),TRUE);
DrawBox(640, 480, 640-10, 0, GetColor(255,255,255),TRUE);
DrawBox(640, 480, 0, 480-10, GetColor(255,255,255),TRUE);
}
//キャラクターの初期化
void CharaInit() {
for (i=0;i<NIKU_KAZU;++i) {
carni[i].x = GetRand(640-31)+10;
carni[i].y = GetRand(480-31)+10;
carni[i].color = GetColor(255, 0, 0);
carni[i].life = 100;
}
for (i=0;i<SOU_KAZU;++i) {
herbi[i].x = GetRand(640-31)+10;
herbi[i].y = GetRand(480-31)+10;
herbi[i].color = GetColor(0, 255, 0);
herbi[i].life = 100;
}
for (i=0;i<KUSA_KAZU;++i) {
weed[i].x = GetRand(640-31)+10;
weed[i].y = GetRand(480-31)+10;
weed[i].color = GetColor(0, 0, 255);
weed[i].life = 100;
}
}
//肉食動物の移動と表示
void DrawCarni() {
static int move[NIKU_KAZU];
for (i=0;i<NIKU_KAZU;++i) {
if (move[i] == UP) carni[i].y -= 5;
if (move[i] == RIGHT) carni[i].x += 5;
if (move[i] == DOWN) carni[i].y += 5;
if (move[i] == LEFT) carni[i].x -= 5;
if (carni[i].x < 10) {
carni[i].x = 10;
move[i] = GetRand(3);
}
if (carni[i].x > 640-20) {
carni[i].x = 640-20;
move[i] = GetRand(3);
}
if (carni[i].y < 10) {
carni[i].y = 10;
move[i] = GetRand(3);
}
if (carni[i].y > 480-20) {
carni[i].y = 480-20;
move[i] = GetRand(3);
}
DrawBox(carni[i].x, carni[i].y, carni[i].x+10, carni[i].y+10, carni[i].color, TRUE);
}
}
//草食動物の移動と表示
void DrawHerbi() {
static int move[NIKU_KAZU];
for (i=0;i<SOU_KAZU;++i) {
if (move[i] == UP) herbi[i].y -= 5;
if (move[i] == RIGHT) herbi[i].x += 5;
if (move[i] == DOWN) herbi[i].y += 5;
if (move[i] == LEFT) herbi[i].x -= 5;
if (herbi[i].x < 10) {
herbi[i].x = 10;
move[i] = GetRand(3);
}
if (herbi[i].x > 640-20) {
herbi[i].x = 640-20;
move[i] = GetRand(3);
}
if (herbi[i].y < 10) {
herbi[i].y = 10;
move[i] = GetRand(3);
}
if (herbi[i].y > 480-20) {
herbi[i].y = 480-20;
move[i] = GetRand(3);
}
DrawBox(herbi[i].x, herbi[i].y, herbi[i].x+10, herbi[i].y+10, herbi[i].color, TRUE);
}
}
//草の表示
void DrawWeed() {
static int move[NIKU_KAZU];
for (i=0;i<KUSA_KAZU;++i) {
DrawBox(weed[i].x, weed[i].y, weed[i].x+10, weed[i].y+10, weed[i].color, TRUE);
}
}