Skip to content

Commit

Permalink
Made MoveUpdate.avoiding() and put it to use
Browse files Browse the repository at this point in the history
* Added MoveUpdate.avoiding() generator function
* Made Wolf, Deer, and WalrusFriend use avoiding()
* Made Player use avoiding()
* Got rid of StatEnt.move() as it is obsolete
  • Loading branch information
nenofite committed Nov 10, 2012
1 parent 2798a02 commit 262ff06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
19 changes: 19 additions & 0 deletions wyld/core/ent.d
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ class MoveUpdate : Update {
return new MoveUpdate(ent, delta);
}

static MoveUpdate avoiding(DynamicEnt ent, Direction dir) {
auto moveDelta = coordFromDirection(dir);

auto update = MoveUpdate.withCheck(ent, moveDelta);
if (update is null) {
bool sign = cast(bool)rand.uniform!("[]")(0, 1);
for (int delta = 0; delta <= 2; delta++) {
auto avoidDir = toDirection(dir + delta * (sign ? 1 : -1));
update = MoveUpdate.withCheck(ent, coordFromDirection(avoidDir));
if (update !is null) break;

avoidDir = toDirection(dir - delta * (sign ? 1 : -1));
update = MoveUpdate.withCheck(ent, coordFromDirection(avoidDir));
if (update !is null) break;
}
}
return update;
}

void apply() {
if (!world.isBlockingAt(dest)) {
ent.coord = dest;
Expand Down
38 changes: 8 additions & 30 deletions wyld/ent.d
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ abstract class StatEnt : DynamicEnt {
return hp == 0;
}


bool move(Coord delta) {
update = MoveUpdate.withCheck(this, delta);

return update !is null;
}

SpRequirement spRequirement(int amount) {
return new SpRequirement(this, amount);
}
Expand Down Expand Up @@ -174,7 +167,8 @@ class Player : StatEnt {
menu.running = false;
}

void attackMove(Coord delta) {
void attackMove(Direction dir) {
auto delta = coordFromDirection(dir);
auto newCoord = coord + delta;

foreach (DynamicEnt ent; world.dynamicEnts) {
Expand All @@ -191,7 +185,7 @@ class Player : StatEnt {
}
}

move(delta);
update = MoveUpdate.avoiding(this, dir);
}

Ent corpse() {
Expand Down Expand Up @@ -300,16 +294,14 @@ class Deer : StatEnt {
}

if (predator !is null) {
auto delta = coordFromDirection(oppositeDirection(directionBetween(coord, predator.coord)));
move(delta);
update = MoveUpdate.avoiding(this, oppositeDirection(directionBetween(coord, predator.coord)));
} else {
if (coord == dest) {
hasDest = false;
}

if (hasDest) {
auto delta = coordFromDirection(directionBetween(coord, dest));
move(delta);
update = MoveUpdate.avoiding(this, directionBetween(coord, dest));

if (update is null) {
hasDest = false;
Expand Down Expand Up @@ -401,8 +393,8 @@ class Wolf : StatEnt {
} else if (distanceBetween(coord, prey.coord) <= 1) {
update = (new Bite(this, prey)).update();
} else {
auto delta = coordFromDirection(directionBetween(coord, prey.coord));
if (!move(delta)) {
update = MoveUpdate.avoiding(this, directionBetween(coord, prey.coord));
if (update is null) {
prey = null;
}
}
Expand Down Expand Up @@ -817,21 +809,7 @@ class WalrusFriend : StatEnt {
if (update is null) {
if (distanceBetween(coord, follow.coord) > 3) {
auto dir = directionBetween(coord, follow.coord);
auto moveDelta = coordFromDirection(dir);

update = MoveUpdate.withCheck(this, moveDelta);
if (update is null) {
bool sign = cast(bool)rand.uniform!("[]")(0, 1);
for (int delta = 0; delta <= 4; delta++) {
auto avoidDir = toDirection(dir + delta * (sign ? 1 : -1));
update = MoveUpdate.withCheck(this, coordFromDirection(avoidDir));
if (update !is null) break;

avoidDir = toDirection(dir - delta * (sign ? 1 : -1));
update = MoveUpdate.withCheck(this, coordFromDirection(avoidDir));
if (update !is null) break;
}
}
update = MoveUpdate.avoiding(this, dir);
} else {
snortDelay--;
if (snortDelay <= 0) {
Expand Down
2 changes: 1 addition & 1 deletion wyld/ui.d
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MainScreen : Menu.Screen {
auto dir = directionFromKey(key, isDir);

if (isDir) {
player.attackMove(coordFromDirection(dir));
player.attackMove(dir);
} else if (key == '5') {
player.update = new class() Update {
this() {
Expand Down

0 comments on commit 262ff06

Please sign in to comment.