Hola de nuevo.
La idea anterior, encapsulada y un poco mas organizada :
Ship.h
Código PHP:
#ifndef ShipH
#define ShipH
#include <ExtCtrls.hpp>
#include <jpeg.hpp>
const int MAX_X = 1366;
class Ship {
private:
TImage* _image;
TTimer* _timer;
TWinControl* _parent;
TPoint _route[MAX_X];
TPoint _start;
TPoint _stop;
int _inx;
int _toproute;
int calcRoute();
const TPoint &getPt( const int &ix );
void setParent( TWinControl* parent );
void __fastcall _timerTimer(TObject *Sender);
public:
Ship( TWinControl* parent, const TPoint& start, const TPoint& stop);
~Ship();
__property TWinControl* Parent = { read = _parent, write = setParent };
__property TImage* Image = { read = _image, write = _image };
__property TTimer* Timer = { read = _timer, write = _timer };
__property int TopRoute = { read = _toproute };
__property TPoint Route[int] = { read = getPt };
};
#endif
Ship.cpp
Código PHP:
#pragma hdrstop
#include "Ship.h"
#pragma package(smart_init)
Ship::Ship( TWinControl* parent, const TPoint& start, const TPoint& stop ) {
_start = start;
_stop = stop;
_inx = 0;
_toproute = calcRoute();
_image = new TImage(NULL);
_image->Visible = false;
_image->Parent = parent;
_image->Left = start.x;
_image->Top = start.y;
_timer = new TTimer(NULL);
_timer->Interval = 20;
_timer->Enabled = false;
_timer->OnTimer = _timerTimer;
}
Ship::~Ship()
{
delete _timer;
delete _image;
}
/* private */
int Ship::calcRoute()
{
int dx = _stop.x - _start.x;
int dy = _stop.y - _start.y;
int c, x;
for(c=0, x = _start.x; x < _stop.x; c++, x++ ) {
int y = _start.y + dy * ( x - _start.x ) / dx;
_route[c].x = _start.x + x;
_route[c].y = y;
}
return c;
}
const TPoint& Ship::getPt( const int &ix )
{
return _route[ix];
}
void Ship::setParent( TWinControl* parent )
{
_image->Parent = parent;
}
void __fastcall Ship::_timerTimer(TObject *Sender)
{
_image->Visible = _inx < _toproute;
_timer->Enabled = _image->Visible;
if (_image->Visible) {
_image->Left = _route[_inx].x;
_image->Top = _route[_inx].y;
}
else {
_inx = 0;
}
++_inx;
}
Ejemplo del uso:
Código PHP:
#pragma hdrstop
#include "Unit1.h"
#include "Ship.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
Ship* s1;
Ship* s2;
Ship* s3;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
s1 = new Ship( this, Point(10,100), Point( 240,200) );
s2 = new Ship( this, Point( 50, 50 ), Point( 150, 300 ) );
s3 = new Ship( this, Point( 80, 90 ), Point( 220, 100 ) );
s1->Image->Picture->LoadFromFile("face-smile.jpg");
s2->Image->Picture->LoadFromFile("face-sad.jpg");
s3->Image->Picture->LoadFromFile("face-monkey.jpg");
s1->Timer->Interval = 10;
s2->Timer->Interval = 10;
s3->Timer->Interval = 10;
}
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
s1->Timer->Enabled = true;
s2->Timer->Enabled = true;
s3->Timer->Enabled = true;
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete s1;
delete s2;
delete s3;
}
Saludos
