Skip to content

Commit

Permalink
Improved on sleeping and Time system
Browse files Browse the repository at this point in the history
* Worked on sleeping
* Added minimum timeDelta to Update
* Added float versions of time conversions functions
* Sleeping restores hp as well as sp
  • Loading branch information
nenofite committed Nov 10, 2012
1 parent 1ba8fcc commit 2798a02
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 5 deletions.
1 change: 1 addition & 0 deletions wyld/core/ent.d
Expand Up @@ -188,6 +188,7 @@ abstract class DynamicEnt : Ent {
/// An action performed by an Ent, involving time and Stat requirements
abstract class Update {
int consumeTime; /// The amount of ticks needed to perform this
uint timeDelta = 1; /// The minimum timeDelta this Update shall run at
StatRequirement[] requireStats, /// Stats that must be at a certain amount
consumeStats; /// Stats that will be used up by this

Expand Down
25 changes: 23 additions & 2 deletions wyld/core/world.d
Expand Up @@ -30,15 +30,18 @@ class World {


void update() {
uint elapsed = time.delta;
time.delta = 1;
foreach (ent; dynamicEnts) {
uint elapsed = time.delta;

for (int i = 0; i < elapsed; i++) {
ent.clearNearby();
ent.tickUpdate();
}

if (ent.update !is null) {
if (ent.update.timeDelta > time.delta)
time.delta = ent.update.timeDelta;

bool statsMet;
bool keep = ent.update.run(elapsed, statsMet);

Expand Down Expand Up @@ -376,20 +379,38 @@ struct Time {
return secs * ticksPerSecond;
}

static int fromSeconds(float secs) {
return cast(int)(secs * ticksPerSecond);
}

/// ditto
static int fromMinutes(int mins) {
return fromSeconds(mins * 60);
}

static int fromMinutes(float mins) {
return fromSeconds(mins * 60);
}

/// ditto
static int fromHours(int hrs) {
return fromMinutes(hrs * 60);
}

/// ditto
static int fromHours(float hrs) {
return fromMinutes(hrs * 60);
}

/// ditto
static int fromPeriods(int pers) {
return fromHours(pers * 12);
}

/// ditto
static int fromPeriods(float pers) {
return fromHours(pers * 12);
}
}

abstract class Sound {
Expand Down
48 changes: 46 additions & 2 deletions wyld/ent.d
Expand Up @@ -235,7 +235,7 @@ class Player : StatEnt {
}

Update update() {
int time = Time.fromSeconds(weapon.tags.weight / 10);
int time = Time.fromSeconds(weapon.tags.weight / 10.0);
return Attack.update(time, weapon.tags.weight * 5);
}

Expand Down Expand Up @@ -443,7 +443,7 @@ class Wolf : StatEnt {
}

Update update() {
return Attack.update(150, 25);
return Attack.update(Time.fromSeconds(1.5), 25);
}

Message message() {
Expand Down Expand Up @@ -861,3 +861,47 @@ class WalrusFriend : StatEnt {
}
}
}

class Sleep : Update {
static const int pieceLength = Time.fromSeconds(10);

int remainingPieces;
StatEnt ent;

this(int remainingPieces, StatEnt ent) {
this.remainingPieces = remainingPieces;
this.ent = ent;
timeDelta = Time.fromSeconds(10);
super(pieceLength, [], []);
}

static Sleep forTime(int time, StatEnt ent) {
return new Sleep(time / pieceLength, ent);
}

static Sleep untilDawn(StatEnt ent) {
int ticks = Time.fromPeriods(1) - world.time.periodTicks();
if (world.time.isDay())
ticks += Time.fromPeriods(1);
return Sleep.forTime(ticks, ent);
}

Sleep next() {
if (remainingPieces > 0) {
remainingPieces--;
consumeTime = pieceLength;
return this;
} else {
return null;
}
}

void apply() {
ent.sp.amount += 10;
ent.hp.amount += 10;
}

int timeRemaining() {
return remainingPieces * pieceLength;
}
}
87 changes: 86 additions & 1 deletion wyld/ui.d
Expand Up @@ -1106,19 +1106,104 @@ class EscapeScreen : Menu.Screen {

class Skills : Menu.Screen {
Jump jump;
SleepScreen sleep;

this() {
super("Skills");
jump = new Jump();
sleep = new SleepScreen();
}

Menu.Entry[] entries() {
return [
new Menu.SubEntry('j', jump)
new Menu.SubEntry('j', jump),
new Menu.SubEntry('s', sleep)
];
}
}

class SleepScreen : Menu.Screen {
UntilDawn untilDawn;
OneHour oneHour;

Sleep update;

this() {
super("Sleep");

untilDawn = new UntilDawn(this);
oneHour = new OneHour(this);
}

Menu.Entry[] entries() {
return [untilDawn, cast(Menu.Entry)oneHour];
}

static class UntilDawn : Menu.Entry {
SleepScreen screen;

this(SleepScreen screen) {
this.screen = screen;
super('D', "Until Dawn");
}

void select() {
auto upd = Sleep.untilDawn(player);
player.update = upd;
menu.addScreen(new WaitScreen(upd));
}
}

static class OneHour : Menu.Entry {
SleepScreen screen;

this(SleepScreen screen) {
this.screen = screen;
super('H', "For 1 Hour");
}

void select() {
auto upd = Sleep.forTime(Time.fromHours(1), player);
player.update = upd;
menu.addScreen(new WaitScreen(upd));
}
}

static class WaitScreen : Menu.Screen {
Sleep update;

TimeEntry time;

this(Sleep update) {
this.update = update;
time = new TimeEntry(this);
super("Sleeping...");
}

Menu.Entry[] entries() {
time.updateTitle();
return [time];
}

static class TimeEntry : Menu.Entry {
WaitScreen screen;

this(WaitScreen screen) {
this.screen = screen;
super('\0', "");
updateTitle();
}

void updateTitle() {
auto hours = screen.update.timeRemaining() / Time.fromMinutes(1);
title = format("%d minutes remaining", hours);
}

void select() {}
}
}
}

class Jump : Menu.Screen, WorldView.Overlay {
Coord marker;

Expand Down

0 comments on commit 2798a02

Please sign in to comment.