* GameDataManager

 

다음과 같은 원칙을 지키면서 개발하였다.

 

  • 여러 곳에서 참조할 수 있고 이를 기반으로 캐릭터의 여러 스탯들을 결정하기 때문에 변경되면 안된다.
  • 읽기 전용으로 만들기 위해 __newindex를 빈 함수로 설정하여 막고있다.
  • 키에 따라 게임데이터를 반환할 때 게임데이터의 테이블을 직접 반환하는 것이 아니라 메타테이블로 설정하여 반환한다.(앞에서 __newindex를 빈함수로 설정한 효과를 볼 수 있다.)

 

 

- GameData 의 메타테이블 : GameDataBase

 

해당 게임데이터가 존재하는지 확인하고 받아오는 Get함수를 제공하고 있으며, 각 게임데이터를 readonly로 만들기 위해 Initialize와 Insert 메소드를 제공하고 있다.

local ServerStorage = game:GetService("ServerStorage")
local ServerModule = ServerStorage:WaitForChild("ServerModule")
local Utility = require(ServerModule:WaitForChild("Utility"))


function EmptyFunction() end

local GameDataBase = {}
GameDataBase.__index = GameDataBase
GameDataBase.__newindex = EmptyFunction

-- readonly로 만들어준다.
function GameDataBase:Initialize()
	rawset(self, "__index", self)
	rawset(self, "__newindex", EmptyFunction)
end

function GameDataBase:Get(key)
	
	local keyTypeString = type(key)
	
	if keyTypeString ~= "number" then
		Utility.Assert(false, "잘못된 키값입니다.")
		return nil
	end
	
	local value = self[key]
	
	if not value then
		Utility.Assert(false, "존재하지 않는 키값입니다. => ".. tostring(key))
		return nil
	end
	
	return value
end

function GameDataBase:InsertData(key, value)
	
	local keyTypeString = type(key)
	
	if keyTypeString ~= "number" then
		Utility.Assert(false, "정수형 키만 가질 수 있습니다.")
		return
	end
	
	if self[key] ~= nil then
		Utility.Assert(false, "중복 삽입하려고 합니다.")
		return
	end
	
	local valueTypeString = type(value)
	
	if valueTypeString ~= "table" then
		Utility.Assert(false, "잘못된 값을 삽입하려고 합니다.")
		return
	end
	
	value.__index = value
	value.__newindex = EmptyFunction
	
	rawset(self, key, setmetatable({}, value))
	
end


return GameDataBase

 

 

- GameData 테이블

 

지금은 그냥 데이터를 넣고 있지만 추후 xml과 json과 같은 파일들을 읽어서 삽입할 수 있는 기능을 추가하면 좋을 것 같다.

 

이 현재는 두 가지 데이터를 기반으로 플레이어의 공격력, 방어력, 이동속도, 공격 속도 등을 결정하고 있다. 

 

-- ToolGameData

local GameDataBase = require(script.Parent:WaitForChild("GameDataBase"))

local ToolGameData = setmetatable({}, GameDataBase)
ToolGameData:Initialize()

--[[ 기본 	--]] ToolGameData:InsertData(1, {STR = 10, DEF = 10, Move = 10, AttackSpeed = 10, Skill = ""})
--[[ 검 	--]] ToolGameData:InsertData(2, {STR = 10, DEF = 10, Move = 15, AttackSpeed = 30, Skill = ""})
--[[ 도끼	--]] ToolGameData:InsertData(3, {STR = 25, DEF = 5, Move = 1, AttackSpeed = 10, Skill = ""})

return setmetatable({}, ToolGameData)

 

-- CharacterGameData

local GameDataBase = require(script.Parent:WaitForChild("GameDataBase"))

local CharacterGameData = setmetatable({}, GameDataBase)
CharacterGameData:Initialize()


--[[ 기본 	--]] CharacterGameData:InsertData(1, {STR = 10, DEF = 10, Move = 10, AttackSpeed = 10, Skill = ""})
--[[ 성장	--]] CharacterGameData:InsertData(2, {STR = 50, DEF = 50, Move = 50, AttackSpeed = 50, Skill = ""})

return setmetatable({}, CharacterGameData)

 

 

- GameDataManager 테이블

 

외부에서 사용하기 편하도록 이들을 래핑하고 있는 테이블이다.

local GameDataManager = {
	CharacterGameData = require(script:WaitForChild("CharacterGameData")),
	ToolGameData = require(script:WaitForChild("ToolGameData"))
}

return GameDataManager

 

+ Recent posts