mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-23 01:57:22 +00:00
Save and load state
This commit is contained in:
parent
1036da1f77
commit
da99d97d32
6 changed files with 36 additions and 15 deletions
8
main.go
8
main.go
|
|
@ -63,8 +63,8 @@ var rooms map[string]*Room
|
|||
func init() {
|
||||
}
|
||||
|
||||
func startGame(path string, imageChannel chan *image.RGBA, inputChannel chan int) {
|
||||
ui.Run([]string{path}, imageChannel, inputChannel)
|
||||
func startGame(path string, roomID string, imageChannel chan *image.RGBA, inputChannel chan int) {
|
||||
ui.Run([]string{path}, roomID, imageChannel, inputChannel)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
@ -118,7 +118,7 @@ func initRoom(roomID, gameName string) string {
|
|||
rtcSessions: []*webrtc.WebRTC{},
|
||||
}
|
||||
go fanoutScreen(imageChannel, roomID)
|
||||
go startGame("games/"+gameName, imageChannel, inputChannel)
|
||||
go startGame("games/"+gameName, roomID, imageChannel, inputChannel)
|
||||
|
||||
return roomID
|
||||
}
|
||||
|
|
@ -255,7 +255,7 @@ func postSession(w http.ResponseWriter, r *http.Request) {
|
|||
rtcSessions: []*webrtc.WebRTC{},
|
||||
}
|
||||
go fanoutScreen(imageChannel, roomID)
|
||||
go startGame("games/"+postPacket.Game, imageChannel, inputChannel)
|
||||
go startGame("games/"+postPacket.Game, roomID, imageChannel, inputChannel)
|
||||
// fanin input channel
|
||||
// fanout output channel
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ Room ID: <input type="text" id="roomID"><br>
|
|||
X to sprint (B) <br />
|
||||
C is start button <br />
|
||||
V is select button <br />
|
||||
S to save <br />
|
||||
L to load <br />
|
||||
|
||||
<!--Fullscreen media for better gaming experience<br /-->
|
||||
</div><br>
|
||||
|
|
@ -200,6 +202,8 @@ keyMap = {
|
|||
88: "b",
|
||||
67: "start",
|
||||
86: "select",
|
||||
83: "save",
|
||||
76: "load",
|
||||
}
|
||||
|
||||
INPUT_FPS = 100;
|
||||
|
|
@ -243,7 +247,7 @@ function sendInput() {
|
|||
|
||||
if (stateUnchange || unchangePacket > 0) {
|
||||
st = "";
|
||||
["a", "b", "select", "start", "up", "down", "left", "right"].forEach(elem => {
|
||||
["a", "b", "select", "start", "up", "down", "left", "right", "save", "load"].forEach(elem => {
|
||||
st += keyState[elem]?1:0;
|
||||
});
|
||||
ss = parseInt(st, 2);
|
||||
|
|
|
|||
|
|
@ -20,14 +20,16 @@ type Director struct {
|
|||
timestamp float64
|
||||
imageChannel chan *image.RGBA
|
||||
inputChannel chan int
|
||||
roomID string
|
||||
}
|
||||
|
||||
func NewDirector(imageChannel chan *image.RGBA, inputChannel chan int) *Director {
|
||||
func NewDirector(roomID string, imageChannel chan *image.RGBA, inputChannel chan int) *Director {
|
||||
// func NewDirector(audio *Audio, imageChannel chan *image.RGBA, inputChannel chan int) *Director {
|
||||
director := Director{}
|
||||
// director.audio = audio
|
||||
director.imageChannel = imageChannel
|
||||
director.inputChannel = inputChannel
|
||||
director.roomID = roomID
|
||||
return &director
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +72,7 @@ func (d *Director) Run() {
|
|||
}
|
||||
|
||||
func (d *Director) PlayGame(path string) {
|
||||
hash, err := hashFile(path)
|
||||
hash, err := hashFile(path, d.roomID)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,14 +20,14 @@ type GameView struct {
|
|||
record bool
|
||||
frames []image.Image
|
||||
|
||||
keyPressed [8]bool
|
||||
keyPressed [10]bool
|
||||
|
||||
imageChannel chan *image.RGBA
|
||||
inputChannel chan int
|
||||
}
|
||||
|
||||
func NewGameView(director *Director, console *nes.Console, title, hash string, imageChannel chan *image.RGBA, inputChannel chan int) View {
|
||||
gameview := &GameView{director, console, title, hash, false, nil, [8]bool{false}, imageChannel, inputChannel}
|
||||
gameview := &GameView{director, console, title, hash, false, nil, [10]bool{false}, imageChannel, inputChannel}
|
||||
go gameview.ListenToInputChannel()
|
||||
return gameview
|
||||
}
|
||||
|
|
@ -35,7 +35,8 @@ func NewGameView(director *Director, console *nes.Console, title, hash string, i
|
|||
func (view *GameView) ListenToInputChannel() {
|
||||
for {
|
||||
key := <-view.inputChannel
|
||||
s := fmt.Sprintf("%.8b", key)
|
||||
s := fmt.Sprintf("%.10b", key)
|
||||
fmt.Println(s)
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == '1' {
|
||||
view.keyPressed[i] = true
|
||||
|
|
@ -107,5 +108,17 @@ func (view *GameView) updateControllers() {
|
|||
// buttons[nes.ButtonStart] = view.keyPressed[13]
|
||||
// buttons[nes.ButtonSelect] = view.keyPressed[16]
|
||||
// view.console.Controller1.SetButtons(buttons)
|
||||
view.console.Controller1.SetButtons(view.keyPressed)
|
||||
var gameKeys [8]bool
|
||||
copy(gameKeys[:], view.keyPressed[:8])
|
||||
|
||||
if view.keyPressed[8] {
|
||||
fmt.Println("saving", view.hash)
|
||||
view.console.SaveState(savePath(view.hash))
|
||||
}
|
||||
if view.keyPressed[9] {
|
||||
fmt.Println("loading", view.hash)
|
||||
view.console.LoadState(savePath(view.hash))
|
||||
}
|
||||
|
||||
view.console.Controller1.SetButtons(gameKeys)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ func init() {
|
|||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int) {
|
||||
func Run(paths []string, roomID string, imageChannel chan *image.RGBA, inputChannel chan int) {
|
||||
// TODO: stream audio
|
||||
// initialize audio
|
||||
// portaudio.Initialize()
|
||||
|
|
@ -33,7 +33,7 @@ func Run(paths []string, imageChannel chan *image.RGBA, inputChannel chan int) {
|
|||
// defer audio.Stop()
|
||||
|
||||
// run director
|
||||
director := NewDirector(imageChannel, inputChannel)
|
||||
director := NewDirector(roomID, imageChannel, inputChannel)
|
||||
// director := NewDirector(audio, imageChannel, inputChannel)
|
||||
director.Start(paths)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,13 +45,15 @@ func combineButtons(a, b [8]bool) [8]bool {
|
|||
return result
|
||||
}
|
||||
|
||||
func hashFile(path string) (string, error) {
|
||||
// hashFile : signature of a room, maybe not need path
|
||||
func hashFile(path string, roomID string) (string, error) {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%x", md5.Sum(data)), nil
|
||||
return fmt.Sprintf("%x", md5.Sum(append(data, []byte(roomID)...))), nil
|
||||
}
|
||||
|
||||
func copyImage(src image.Image) *image.RGBA {
|
||||
dst := image.NewRGBA(src.Bounds())
|
||||
draw.Draw(dst, dst.Rect, src, image.ZP, draw.Src)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue