PlayerModel = class(function(o)
	-- should include the unlocked rooms here as well
	o.unlocks = {['shadow']={}}
	o.highscores = {['shadow']={}}
	-- because openframeworks wants to crash rather than exit, we need to save everytime something is changed
	local mt = {}
	mt.__newindex = function(op, k, v)
		-- set the value
	    rawset(op.shadow,k,v)
	    blud.exit()
	end
	mt.__index = function(op, k) return rawget(op, 'shadow')[k] end
	setmetatable(o.unlocks, mt)
	setmetatable(o.highscores, mt)
	o.autosave = mt
	o.unlocks['level_data_1_0'] = true
	o.unlocks['world_pink'] = true
	o.allLevelsUnlocked = true
	o.currentWorld = worlds[1]
	o.shotCount = 0
	o.destroyCount = 0
	o.resolution = 0
	o.fullscreen = 1
	o.achievements = AchievementsModel()
	o.leaderboards = LeaderboardModel()
end)

function PlayerModel:resetShotCount()
	self.levelShotCount = 0
end
function PlayerModel:addShot(numShots)
	numShots = numShots or 1
	self.achievements:addBulletLifetime(numShots)
	self.levelShotCount = self.levelShotCount+numShots
	self.achievements:addBulletLevel(self.levelShotCount)
end
function PlayerModel:getLevelShots()
	return self.levelShotCount
end

function PlayerModel:resetDestroyCount()
	self.levelDestroyCount = 0
end
function PlayerModel:addDestroy(numShots)
	numShots = numShots or 1
	self.levelDestroyCount = self.levelDestroyCount+numShots
end
function PlayerModel:getLevelDestroy()
	return self.levelDestroyCount
end

function PlayerModel:saveTo(data)
	data.unlocks = self.unlocks
	data.highscores = self.highscores
	data.achievements = self.achievements:saveTo({})
	data.leaderboards = self.leaderboards:saveTo({})
	data.fullscreen = self.fullscreen
	data.resolution = self.resolution
	return data
end

function PlayerModel:loadFrom(data)
	-- since we have the bizzare save system we need to raw set everything.
	self.highscores = data.highscores
	self.unlocks = data.unlocks
	self.fullscreen = data.fullscreen or 1
	self.resolution = data.resolution or 0
	setmetatable(self.unlocks, self.autosave)
	setmetatable(self.highscores, self.autosave)
	if data.achievements then
  		self.achievements:loadFrom(data.achievements)
  	end
	if data.leaderboards then
  		self.leaderboards:loadFrom(data.leaderboards)
  	else
  		-- copy the data from the highscore table to the leaderboards
  		for k,v in pairs(self.highscores.shadow) do
  			self.leaderboards:addScore(k, v)
  		end
  	end
	self.unlocks['level_data_1_0'] = true
	self.unlocks['world_pink'] = true
	bg:setFullscreen(fullscreen[player["fullscreen"]+1][2])
	buildScreenInfo()
	-- rebuild this screen
	-- mainState = WrapperState(Group());
	-- mainState:changeState(SettingState(self.lastState))
end