const int width = 800;
const int height = 480;
+ const float walkSpeed = 128.0f;
+
const bool battle(false);
// std::srand(std::time(0));
MapState *mapState(new MapState(&map));
mapState->AddEntity(&mapMaxim);
mapState->ControlEntity(&mapMaxim);
+ mapState->SetWalkingSpeed(walkSpeed);
state = mapState;
}
namespace map {
Entity::Entity()
-: sprite(0) {
+: sprite(0)
+, orientation(ORIENTATION_NORTH)
+, speed(0) {
}
+void Entity::SetOrientation(Orientation o) {
+ orientation = o;
+ UpdateVelocity();
+ animation.SetColOffset(orientation);
+}
+
+void Entity::SetSpeed(float s) {
+ speed = s;
+ UpdateVelocity();
+}
+
+void Entity::UpdateVelocity() {
+ if (speed == 0.0f) {
+ velocity = Vector<float>();
+ return;
+ }
+ switch (orientation) {
+ case ORIENTATION_NORTH:
+ velocity = Vector<float>(0.0f, -speed);
+ break;
+ case ORIENTATION_EAST:
+ velocity = Vector<float>(speed, 0.0f);
+ break;
+ case ORIENTATION_SOUTH:
+ velocity = Vector<float>(0.0f, speed);
+ break;
+ case ORIENTATION_WEST:
+ velocity = Vector<float>(-speed, 0.0f);
+ break;
+ }
+}
+
+
bool Entity::TileLock(int width, int height) const {
Vector<int> tilePosition(
position.X() - (width / 2),
if (animation.Running()) {
animation.DrawCenterBottom(dest, offset + position);
} else {
- sprite->DrawCenterBottom(dest, offset + position);
+ sprite->DrawCenterBottom(dest, offset + position, orientation);
}
}
Entity();
~Entity() { }
+public:
+ enum Orientation {
+ ORIENTATION_NORTH = 0,
+ ORIENTATION_EAST = 1,
+ ORIENTATION_SOUTH = 2,
+ ORIENTATION_WEST = 3,
+ };
+
public:
geometry::Vector<float> &Position() { return position; }
const geometry::Vector<float> &Position() const { return position; }
graphics::AnimationRunner &Animation() { return animation; }
const graphics::AnimationRunner &Animation() const { return animation; }
+ void SetOrientation(Orientation);
+ void SetSpeed(float);
+
bool TileLock(int width, int height) const;
void Update(float deltaT);
void Render(SDL_Surface *, const geometry::Vector<int> &offset) const;
+private:
+ void UpdateVelocity();
+
private:
const graphics::Sprite *sprite;
graphics::AnimationRunner animation;
geometry::Vector<float> position;
geometry::Vector<float> velocity;
+ Orientation orientation;
+ float speed;
};
: map(map)
, controlled(0)
, tempTarget(20, 20)
-, camera(100, 100, &tempTarget) {
+, camera(100, 100, &tempTarget)
+, walkingSpeed(64) {
}
void MapState::HandleEvents(const Input &input) {
if (!controlled) return;
if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
+
+ if (input.IsDown(Input::PAD_UP)) {
+ controlled->SetOrientation(Entity::ORIENTATION_NORTH);
+ controlled->SetSpeed(walkingSpeed);
+ } else if (input.IsDown(Input::PAD_RIGHT)) {
+ controlled->SetOrientation(Entity::ORIENTATION_EAST);
+ controlled->SetSpeed(walkingSpeed);
+ } else if (input.IsDown(Input::PAD_DOWN)) {
+ controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
+ controlled->SetSpeed(walkingSpeed);
+ } else if (input.IsDown(Input::PAD_LEFT)) {
+ controlled->SetOrientation(Entity::ORIENTATION_WEST);
+ controlled->SetSpeed(walkingSpeed);
+ } else {
+ controlled->SetSpeed(0.0f);
+ }
}
void MapState::UpdateWorld(float deltaT) {
}
void MapState::Render(SDL_Surface *screen) {
+ SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
+
Vector<int> offset(camera.CalculateOffset());
map->Render(screen, offset);
void AddEntity(Entity *e) { entities.push_back(e); }
void ControlEntity(Entity *e) { controlled = e; camera.SetTarget(&e->Position()); }
+ void SetWalkingSpeed(float s) { walkingSpeed = s; }
+
private:
static bool ZCompare(const Entity *lhs, const Entity *rhs);
geometry::Vector<float> tempTarget;
graphics::Camera camera;
std::vector<Entity *> entities;
+ float walkingSpeed;
};