Skip to content

Latest commit

 

History

History
74 lines (59 loc) · 2.5 KB

2023-09-02.md

File metadata and controls

74 lines (59 loc) · 2.5 KB

2023-09-02

It's more than today's work, but the idea is that I made some rework on many things to make map transitions work

First, I changed most of the x,y coordinates pairs in functions as Coord structs, which allows me to add Map information:

// CreateAvatar create a copy of an Avatar on given x,y coordinates.
- func CreateAvatar(avatar Avatar, x, y int) Avatar {
+ func CreateAvatar(avatar Avatar, coord Coord) Avatar {
	return Avatar{
		CanvasPath:  avatar.CanvasPath,
		CanvasImage: canvas.NewImageFromImage(utils.GetImageFromEmbed(avatar.CanvasPath)),
-		PosX:        x,
-		PosY:        y,
+		Coord:       coord,
	}
}

Then, I removed the SpawnNPC logic which didn't really make sense, and populated directly NPCList and ObjectList in init.go. This way, It's easier to load data from a save.

I also had to make a few fixes with currentMap which turned out to be a whole other object than the one in the AllTheMaps slice. I cleaned some pointers and used the right instance everywhere.

I reworked the whole save game file, which was a bit bloated. I now save maps data from all the maps (using AllTheMaps variable) rather than just the currentMap. I struggled a bit but found out I was doing some things relly unefficiently. The file is now much shorter and works.

I now use (both for load and save) a struct

type savedGameData struct {
	Player         model.CharacterStats
	AllTheMaps     []maps.Map
	TimeSinceBegin int
}

Which is Marshalled/Unmarshalled like this

// updateLoadedGameData updates the player, currentMap and TimeSinceBegin with the loaded data.
func updateLoadedGameData(data map[string]interface{}) error {
	loadedData := savedGameData{}
	jsonData, err := json.Marshal(data)
	if err != nil {
		return err
	}
	err = json.Unmarshal(jsonData, &loadedData)
	if err != nil {
		return err
	}

	player = &loadedData.Player
	maps.AllTheMaps = loadedData.AllTheMaps
	model.TimeSinceBegin = loadedData.TimeSinceBegin

	return nil
}

I also had to refresh the Images which are cleaned during save to avoid storing the binary data of the image in the save file...

	// NPCs and Objects were saved without their Image, refresh it
	for indexMap := range maps.AllTheMaps {
		for _, npc := range maps.AllTheMaps[indexMap].NPCList {
			npc.Avatar.RefreshAvatar()
		}
		for _, object := range maps.AllTheMaps[indexMap].ObjectList {
			object.RefreshObject()
		}
	}

Last, for convenience, I added an Event on the L key, to open "load game" popup in-game. It's saving me a lot of time for testing :-P