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
|
//LevelObject.hpp
class LevelObject
{
public:
virtual void Update() = 0;
virtual void Draw(Canvas& canvas) = 0;
protected:
Vector3 location_;
Size size_;
Collider collider_;
Image image_;
};
//Size struct
struct Size
{
Size(int width_, int height_) : width(width_), height(height_)
{}
int width, height;
};
//PlayerNode.hpp
class PlayerNode : public LevelObject
{
public:
PlayerNode(Vector3 loc, Size s);
void Update();
void Draw(Canvas& canvas);
};
//PlayerNode.cpp
PlayerNode::PlayerNode(Vector3 loc, Size s)
{
size_ = Size(s);
location_= Vector3(loc);
collider_ = Collider(Rect(loc.getX(), loc.getY(), s.width, s.height));
ImageFile("resources/images/SnakePart.bmp").load(image_);
}
void PlayerNode::Update()
{
}
void PlayerNode::Draw(Canvas& canvas)
{
canvas.blit(image_, 0, 0, size_.width, size_.height, location_.getX(), location_.getY());
}
//Player.hpp
class Player
{
public:
Player(Vector3 loc, Size s);
void Update();
void Draw(Canvas& canvas);
private:
const int startLength_ = 4;
int length_;
std::list<PlayerNode> pNodes_;
};
//Player.hpp
Player::Player(Vector3 loc, Size s)
{
length_ = startLength_;
//pNodes_ = List<PlayerNode>.();
for ( int i = 0; i < length_; i ++)
{
pNodes_.push_front(PlayerNode(loc + Vector3(-i,0,0), s));
length_++;
}
}
void Player::Update()
{
}
void Player::Draw(Canvas& canvas)
{
for (std::list<PlayerNode>::iterator i = pNodes_.begin(); i != pNodes_.end(); i++)
{
i->Draw(canvas);
}
}
|