60 lines
1.1 KiB
Plaintext
60 lines
1.1 KiB
Plaintext
|
interface Update {
|
||
|
mut fn update(f64) bool;
|
||
|
fn display() string;
|
||
|
}
|
||
|
|
||
|
struct Clock12Hr {
|
||
|
bool pm = true,
|
||
|
u8 hours = 12,
|
||
|
u8 minutes = 0,
|
||
|
u8 seconds = 0,
|
||
|
f64 sub = 0.0,
|
||
|
}
|
||
|
|
||
|
impl Clock12Hr {
|
||
|
mut fn tick_seconds() {
|
||
|
.seconds = .seconds + 1;
|
||
|
if .seconds >= 60 {
|
||
|
.seconds = 0;
|
||
|
.tick_minutes();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mut fn tick_minutes() {
|
||
|
.minutes = .minutes + 1;
|
||
|
if .minutes >= 60 {
|
||
|
.minutes = 0;
|
||
|
.tick_hours();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mut fn tick_hours() {
|
||
|
.hours = .hours + 1;
|
||
|
if .hours >= 13 {
|
||
|
.hours = 1;
|
||
|
} else if hours == 12 {
|
||
|
.hours = not .hours;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Clock12Hr Update {
|
||
|
mut fn update(f64 dt) bool {
|
||
|
let mut dirty = false;
|
||
|
|
||
|
.sub = .sub + dt;
|
||
|
while .sub > 1.0 {
|
||
|
.sub = .sub - 1.0;
|
||
|
.tick_seconds();
|
||
|
dirty = true;
|
||
|
}
|
||
|
|
||
|
dirty
|
||
|
}
|
||
|
|
||
|
fn display() string {
|
||
|
let pm = if .pm { "PM" } else { "AM" };
|
||
|
"{}:{}.{} {}".format(.hours, .minutes, .seconds, pm)
|
||
|
}
|
||
|
}
|