mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-12-01 15:46:02 +03:00
36 lines
463 B
Go
36 lines
463 B
Go
package memento
|
|
|
|
import "fmt"
|
|
|
|
type Memento interface{}
|
|
|
|
type Game struct {
|
|
hp, mp int
|
|
}
|
|
|
|
type gameMemento struct {
|
|
hp, mp int
|
|
}
|
|
|
|
func (g *Game) Play(mpDelta, hpDelta int) {
|
|
g.mp += mpDelta
|
|
g.hp += hpDelta
|
|
}
|
|
|
|
func (g *Game) Save() Memento {
|
|
return &gameMemento{
|
|
hp: g.hp,
|
|
mp: g.mp,
|
|
}
|
|
}
|
|
|
|
func (g *Game) Load(m Memento) {
|
|
gm := m.(*gameMemento)
|
|
g.mp = gm.mp
|
|
g.hp = gm.hp
|
|
}
|
|
|
|
func (g *Game) Status() {
|
|
fmt.Printf("Current HP:%d, MP:%d\n", g.hp, g.mp)
|
|
}
|