1 /*********************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 #include <boost/foreach.hpp>
8 #include <boost/format.hpp>
9 #include <Gosu/Audio.hpp>
10 #include <Gosu/Input.hpp>
11 #include <Gosu/Math.hpp>
21 template<class Cont
, class ValueType
>
22 void removeValue(Cont
* c
, ValueType v
)
24 typename
Cont::iterator it
;
25 for (it
= c
->begin(); it
!= c
->end(); ++it
) {
35 : Entity(), velocity(0, 0)
37 nowalkFlags
= TILE_NOWALK
| TILE_NOWALK_PLAYER
;
40 bool Player::init(const std::string
& descriptor
)
42 bool b
= Entity::init(descriptor
);
44 // Set an initial phase.
45 setPhase(directionStr(setFacing(ivec2(0, 1))));
50 void Player::startMovement(ivec2 delta
)
52 switch (conf
.moveMode
) {
57 movements
.push_back(delta
);
65 stillMoving
= velocity
;
68 void Player::stopMovement(ivec2 delta
)
70 switch (conf
.moveMode
) {
74 removeValue(&movements
, delta
);
75 velocity
= movements
.size() ?
85 stillMoving
= velocity
;
88 void Player::moveByTile(ivec2 delta
)
97 // Left CTRL allows changing facing, but disallows movement.
98 const GameWindow
& window
= GameWindow::instance();
99 if (window
.input().down(Gosu::kbLeftControl
)) {
100 setPhase(directionStr(facing
));
105 Entity::moveByTile(delta
);
108 void Player::useTile()
110 std::vector
<icoord
> tiles
= frontTiles();
111 BOOST_FOREACH(icoord
& c
, tiles
) {
112 Tile
& t
= area
->getTile(c
);
113 t
.onUseScripts(this);
117 void Player::setFrozen(bool b
)
121 Entity::setFrozen(b
);
124 void Player::postMove()
130 Exit
* exit
= destTile
->exits
[EXIT_NORMAL
];
136 ivec2
dxy(deltaCoord
.x
, deltaCoord
.y
);
137 Exit
* exit
= fromTile
->exitAt(dxy
);
141 // If we have a velocity, keep moving.
142 if (conf
.moveMode
== TILE
&& velocity
)
143 moveByTile(velocity
);
146 void Player::takeExit(Exit
* exit
)
148 World
* world
= World::instance();
149 Area
* newArea
= world
->getArea(exit
->area
);
151 world
->focusArea(newArea
, exit
->coords
);
154 // Roll back movement if exit failed to open.
156 Log::err("Exit", exit
->area
+ ": failed to load properly");