Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Nov 12, 2023
1 parent 1cc26a4 commit b95cd86
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions examples/gno.land/r/demo/tamagotchi/tamagotchi.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ type Tamagotchi struct {
age uint
maxAge uint
sleepy uint
created time.Time
lastUpdated time.Time
}

func New(name string) *Tamagotchi {
now := time.Now()
return &Tamagotchi{
name: name,
hunger: 50,
happiness: 50,
health: 50,
maxAge: 100,
lastUpdated: time.Now(),
lastUpdated: now,
created: now,
}
}

Expand Down Expand Up @@ -64,6 +67,11 @@ func (t *Tamagotchi) Sleepy() uint {
// Feed method for Tamagotchi
func (t *Tamagotchi) Feed() {
t.update()

if t.health == 0 { // dead
return
}

t.hunger -= 10
if t.hunger < 0 {
t.hunger = 0
Expand All @@ -73,6 +81,11 @@ func (t *Tamagotchi) Feed() {
// Play method for Tamagotchi
func (t *Tamagotchi) Play() {
t.update()

if t.health == 0 { // dead
return
}

t.happiness += 10
if t.happiness > 100 {
t.happiness = 100
Expand All @@ -82,6 +95,11 @@ func (t *Tamagotchi) Play() {
// Heal method for Tamagotchi
func (t *Tamagotchi) Heal() {
t.update()

if t.health == 0 { // dead
return
}

t.health += 10
if t.health > 100 {
t.health = 100
Expand All @@ -96,30 +114,34 @@ func (t *Tamagotchi) update() {
}

duration := now.Sub(t.lastUpdated)
elapsedMins := uint(duration.Minutes())

t.hunger += uint(duration.Minutes())
t.hunger += elapsedMins
if t.hunger > 100 {
t.hunger = 100
}

t.happiness -= uint(duration.Minutes())
if t.happiness < 0 {
if t.happiness-elapsedMins > 0 {
t.happiness -= elapsedMins
} else {
t.happiness = 0
}

t.health -= uint(duration.Minutes())
if t.health < 0 {
t.health = 0
if t.health-elapsedMins > 0 {
t.health -= elapsedMins
} else {
t.health = 0 // dead
}

t.sleepy += uint(duration.Minutes())
t.sleepy += elapsedMins
if t.sleepy > 100 {
t.sleepy = 100
}

t.age += uint(duration.Hours())
// age is hours since created
t.age = uint(now.Sub(t.created).Hours())
if t.age > t.maxAge {
t.health = 0
t.health = 0 // dead
}
t.lastUpdated = now
}
Expand Down

0 comments on commit b95cd86

Please sign in to comment.