Get rid of class position

This commit is contained in:
Petr Baudis 2011-11-26 20:40:05 +01:00
parent f622f79283
commit 56726a6c05
5 changed files with 40 additions and 43 deletions

View file

@ -6,10 +6,9 @@
#include "map.h"
void
agent::put_at(struct position *pos)
agent::put_at(class tile &t)
{
class tile *t = &pos->map->tile_at(*pos);
if (!t->on_agent_enter(this)) {
if (!t.on_agent_enter(*this)) {
std::cerr << "Collision.";
exit(EXIT_FAILURE);
}

12
agent.h
View file

@ -1,19 +1,21 @@
#ifndef BRMLIFE__AGENT_H
#define BRMLIFE__AGENT_H
class position;
#include "map.h"
class agent {
public:
int id;
class position *pos;
class tile *tile;
agent(int id_, class position &pos_) : id (id_), pos (&pos_)
agent(int id_, class tile &tile_) : id (id_), tile (&tile_)
{
put_at(pos);
put_at(tile_);
};
void put_at(struct position *pos);
private:
/* Just for initial placement. */
void put_at(class tile &tile);
};
#endif

View file

@ -8,7 +8,7 @@ main(int argc, char *argv[])
{
class map map(10, 10);
class position agentpos(4, 4, map);
class tile &agentpos = map.tile_at(4, 4);
class agent agent(0, agentpos);
map.print_map();

21
map.cc
View file

@ -5,17 +5,17 @@
#include "map.h"
bool
tile::on_agent_enter(class agent *a)
tile::on_agent_enter(class agent &a)
{
if (agent) return false;
agent = a;
agent = &a;
return true;
}
void
tile::on_agent_leave(class agent *a)
tile::on_agent_leave(class agent &a)
{
assert(a == agent);
assert(&a == agent);
agent = NULL;
}
@ -38,12 +38,12 @@ tile_ground::type_symbol(void)
return '.';
}
class position *
position::next_in(int dir_x, int dir_y)
class tile &
tile::tile_in_dir(int dir_x, int dir_y)
{
int x2 = (map->w + x + dir_x) % map->w;
int y2 = (map->h + y + dir_y) % map->h;
return new position(x2, y2, *map);
int x2 = (map.w + x + dir_x) % map.w;
int y2 = (map.h + y + dir_y) % map.h;
return map.tile_at(x2, y2);
}
@ -52,8 +52,7 @@ map::print_map(void)
{
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
class position p = position(x, y, *this);
std::cout << tile_at(p).symbol();
std::cout << tile_at(x, y).symbol();
}
std::cout << '\n';
}

43
map.h
View file

@ -6,34 +6,29 @@ class map;
class tile {
public:
int x, y;
class map &map;
class agent *agent;
virtual bool on_agent_enter(class agent *);
virtual void on_agent_leave(class agent *);
virtual void on_tick(void);
tile(int x_, int y_, class map &map_) : x(x_), y(y_), map(map_), agent(NULL) {};
char symbol(void);
virtual char type_symbol(void) = 0;
class tile &tile_in_dir(int dir_x, int dir_y);
virtual bool on_agent_enter(class agent &);
virtual void on_agent_leave(class agent &);
virtual void on_tick(void);
};
class tile_ground : public tile {
char type_symbol(void);
};
class position {
public:
int x, y;
class map *map;
position(int x_, int y_, class map &map_)
{
x = x_;
y = y_;
map = &map_;
};
class position *next_in(int dir_x, int dir_y);
tile_ground(int x_, int y_, struct map &map_) : tile(x_, y_, map_) {};
private:
char type_symbol(void);
};
class map {
@ -47,14 +42,16 @@ public:
h = h_;
tiles = new class tile * [w * h];
for (int i = 0; i < w * h; i++) {
tiles[i] = new tile_ground;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
tiles[y * h + x] = new tile_ground(x, y, *this);
}
}
};
class tile &tile_at(class position &pos)
class tile &tile_at(int x, int y)
{
return *tiles[pos.y * h + pos.x];
return *tiles[y * h + x];
};
void print_map(void);