#6
by 参照魚 » 1年前
ひとつのサンプルとして参考までに。できるだけ元のコードに沿った修正をしています。
コード:
struct {
static int GetTargetWidth() { return 768; }
static int GetTargetHeight(){ return 512; }
} *g_pGraphics;
struct {
static int GetWidth() { return 16; }
static int GetHeight(){ return 16; }
} gTexture;
struct CHARA {
bool reverse;
float px;
float py;
float mx;
float my;
float speed;
};
constexpr int CHARA_SPEED = 10;
constexpr int CHARA_NUM = 4;
void sample(){
int scLeft = 400;
int scTop = 200;
int scRight = scLeft + g_pGraphics->GetTargetWidth();
int scBottom = scTop + g_pGraphics->GetTargetHeight();
int imgWidth = gTexture.GetWidth();
int imgHeight = gTexture.GetHeight();
float minX = scLeft;
float minY = scTop;
float maxX = scRight - imgWidth;
float maxY = scBottom - imgHeight;
static CHARA pos[CHARA_NUM] = {
{ false, scLeft, scTop, +CHARA_SPEED, 0, CHARA_SPEED },
{ false, scLeft+scRight, scTop, 0, +CHARA_SPEED, CHARA_SPEED },
{ false, scLeft+scRight, scTop+scBottom, -CHARA_SPEED, 0, CHARA_SPEED },
{ false, scLeft, scTop+scBottom, 0, -CHARA_SPEED, CHARA_SPEED },
};
for ( int i = 0; i < CHARA_NUM; i++ ) {
extern void click_chara( CHARA*, int, int );
extern void move_chara( CHARA*, int, int, int, int );
extern void draw_chara( const CHARA*, int, int );
click_chara( &pos[i], imgWidth, imgHeight );
move_chara( &pos[i], minX, minY, maxX, maxY );
draw_chara( &pos[i], imgWidth, imgHeight );
}
}
void click_chara( CHARA* pos, int imgWidth, int imgHeight ){
if ( pos ) {
extern bool hit_chara( const CHARA* );
if ( hit_chara( pos ) ) {
pos->reverse = !pos->reverse;
pos->mx *= -1;
pos->my *= -1;
}
}
}
void move_chara( CHARA* pos, int minX, int minY, int maxX, int maxY ){
if ( pos ) {
pos->px += pos->mx;
pos->py += pos->my;
if ( pos->px > maxX ) { pos->px = maxX; pos->my = pos->reverse ? -pos->speed : +pos->speed; pos->mx = 0; return; } // →
if ( pos->py > maxY ) { pos->py = maxY; pos->mx = pos->reverse ? +pos->speed : -pos->speed; pos->my = 0; return; } // ↓
if ( pos->px < minX ) { pos->px = minX; pos->my = pos->reverse ? +pos->speed : -pos->speed; pos->mx = 0; return; } // ←
if ( pos->py < minY ) { pos->py = minY; pos->mx = pos->reverse ? -pos->speed : +pos->speed; pos->my = 0; return; } // ↑
}
}
bool hit_chara( const CHARA* pos ) { return false; }
void draw_chara( const CHARA* pos, int imgWidth, int imgHeight ){}
ひとつのサンプルとして参考までに。できるだけ元のコードに沿った修正をしています。
[code]
struct {
static int GetTargetWidth() { return 768; }
static int GetTargetHeight(){ return 512; }
} *g_pGraphics;
struct {
static int GetWidth() { return 16; }
static int GetHeight(){ return 16; }
} gTexture;
struct CHARA {
bool reverse;
float px;
float py;
float mx;
float my;
float speed;
};
constexpr int CHARA_SPEED = 10;
constexpr int CHARA_NUM = 4;
void sample(){
int scLeft = 400;
int scTop = 200;
int scRight = scLeft + g_pGraphics->GetTargetWidth();
int scBottom = scTop + g_pGraphics->GetTargetHeight();
int imgWidth = gTexture.GetWidth();
int imgHeight = gTexture.GetHeight();
float minX = scLeft;
float minY = scTop;
float maxX = scRight - imgWidth;
float maxY = scBottom - imgHeight;
static CHARA pos[CHARA_NUM] = {
{ false, scLeft, scTop, +CHARA_SPEED, 0, CHARA_SPEED },
{ false, scLeft+scRight, scTop, 0, +CHARA_SPEED, CHARA_SPEED },
{ false, scLeft+scRight, scTop+scBottom, -CHARA_SPEED, 0, CHARA_SPEED },
{ false, scLeft, scTop+scBottom, 0, -CHARA_SPEED, CHARA_SPEED },
};
for ( int i = 0; i < CHARA_NUM; i++ ) {
extern void click_chara( CHARA*, int, int );
extern void move_chara( CHARA*, int, int, int, int );
extern void draw_chara( const CHARA*, int, int );
click_chara( &pos[i], imgWidth, imgHeight );
move_chara( &pos[i], minX, minY, maxX, maxY );
draw_chara( &pos[i], imgWidth, imgHeight );
}
}
void click_chara( CHARA* pos, int imgWidth, int imgHeight ){
if ( pos ) {
extern bool hit_chara( const CHARA* );
if ( hit_chara( pos ) ) {
pos->reverse = !pos->reverse;
pos->mx *= -1;
pos->my *= -1;
}
}
}
void move_chara( CHARA* pos, int minX, int minY, int maxX, int maxY ){
if ( pos ) {
pos->px += pos->mx;
pos->py += pos->my;
if ( pos->px > maxX ) { pos->px = maxX; pos->my = pos->reverse ? -pos->speed : +pos->speed; pos->mx = 0; return; } // →
if ( pos->py > maxY ) { pos->py = maxY; pos->mx = pos->reverse ? +pos->speed : -pos->speed; pos->my = 0; return; } // ↓
if ( pos->px < minX ) { pos->px = minX; pos->my = pos->reverse ? +pos->speed : -pos->speed; pos->mx = 0; return; } // ←
if ( pos->py < minY ) { pos->py = minY; pos->mx = pos->reverse ? -pos->speed : +pos->speed; pos->my = 0; return; } // ↑
}
}
bool hit_chara( const CHARA* pos ) { return false; }
void draw_chara( const CHARA* pos, int imgWidth, int imgHeight ){}
[/code]