summaryrefslogtreecommitdiff
path: root/client/data/init.lua
blob: fabd5f1d74e6528063f7dea0ec6fe0d032e99be7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
math.randomseed(os.time())
function assertf(bool,fmt,...)
	assert(bool,string.format(fmt,unpack({...})))
end
function printf(fmt,...)
	print(string.format(fmt,unpack({...})))
end
-- Override tostring to display more info about the table
--local old_tostring = tostring
--local numtabs = 0
--function tostring(el)
	--if type(el) == "table" then
		--numtabs = numtabs + 1
		--local strbuilder = {"{"}
		--for k,v in pairs(el) do
			--strbuilder[#strbuilder + 1] = string.format("%s%s : %s", string.rep("\t",numtabs), tostring(k), tostring(v))
		--end
		--strbuilder[#strbuilder + 1] = "}"
		--numtabs = numtabs - 1
		--return table.concat(strbuilder,"\n")
	--end
	--return old_tostring(el)
--end


local w,h = scrw(),scrh()
--gui elements
local textlabel = gui.newlabel({{20,0},{w-20,h - 80}},"")
local hintbox = gui.newlabel({{20,h-80},{w-20,h-40}},"")
local cmdbox = gui.neweditbox({{20,h-40},{w-50,h-20}})
local cmdbut = gui.newbutton({{w-50,h-40},{w-20,h-20}},"->")
local fail_hint_tex = video.newtexturefromfile("../data/res/fail_hint_box.png")
local fail_hint_guiimg = gui.newiguiimage({w,h-200},true,fail_hint_tex)
local faillabel = gui.newlabel({{w + 30,h-180},{w+220,h-100}},"")
multibox = gui.neweditbox({{20,h/2},{w-20,h-100}},"")
multibox:set_multiline(true)
multibox:setvisible(false)
multibox_donebut = gui.newbutton({{w-100,h-100},{w-20,h-80}},"Done")
multibox_donebut:setvisible(false)
cmdbox:focus()
--width=200,height=120

function out(text)
	local prev = textlabel:gettext()
	textlabel:settext(prev .. "\n" .. text)
end
function cls()
	textlabel:settext("")
end
function set_hints(...)
	local t = table.concat({...},"\t")
	hintbox:settext(t)
end
function enable_multibox(bool)
	multibox:settext("")
	multibox_donebut:setvisible(bool)
	multibox:setvisible(bool)
end
function get_multibox()
	return multibox
end
function get_multibox_donebut()
	return multibox_donebut
end
function clear_cmdbox()
	cmdbox:settext("")
end

local cmd_hooks = {}
function add_cmdbox_hook(func)
	table.insert(cmd_hooks,func)
end
local cmds
cmdbox.onChange = function()
	for k,v in pairs(cmd_hooks) do
		v(cmdbox)
	end
end

local fail_anim = 0
local fail_queue = {}
function fail_hint(...)
	fail_queue[#fail_queue + 1] = table.concat({...})
end
function fail_hintf(fmt,...)
	s = string.format(fmt,unpack({...}))
	fail_hint(s)
end

function GAME.drawPostGui()
	if fail_anim > 0 then
		if fail_anim < 11 then
			fail_hint_guiimg:move({30,0})
			faillabel:move({30,0})
		end
		fail_anim = fail_anim - 1
		if fail_anim == 0 then
			for i = 1,#fail_queue do
				fail_queue[i] = fail_queue[i + 1]
			end
		end
	else
		if #fail_queue > 0 then
			fail_anim = 100
			fail_hint_guiimg:move({-300,0})
			faillabel:settext(fail_queue[1])
			faillabel:move({-300,0})
		end
	end
end



--function command_over(text)
--end

local function run_command()
	print("Got command")
	local command = cmdbox:gettext()
	local parts = {}
	for word in string.gmatch(command,"([^%s]+)") do
		parts[#parts + 1] = word
	end
	print("Command parts:")
	for k,v in pairs(parts) do
		print(k,":",v)
	end
	local root = parts[1]
	for i = 1,#parts do --Remove the first part from the command.
		parts[i] = parts[i + 1]
	end
	if command_over then
		command_over(command)
	else
		if cmds[root] and cmds["can_" .. root] and cmds["can_" .. root]() then
			print("found command, calling with:")
			for k,v in pairs(parts) do
				print(k,":",v)
			end
			cmds[root](unpack(parts))
			cmdbox:settext("")
			print("Done calling command")
		else
			fail_hintf("Unkown command: %s",root)
		end
	end
end

--multibox_donebut.onClick = function(self)
	--print("Ran multibutton click command")
	--print("Over multibut click:", over_multibut_click)
	--if over_multibut_click then
		--over_multibut_click(self)
	--end
--end
cmdbox.onEnter = run_command
cmdbut.onClick = run_command

GAME.setbackgroundcolor({224,248,208})
local world = require("world")
local locations = require("locations")
print("about to load localplayer")
print("done loading localplayer")
require("loc_city")
require("loc_beach")
require("loc_forest")
require("loc_mountains")
require("loc_fields")
local player = require("localplayer")
require("interface")
cmds = require("commands")


--local house = require("house")
--local save = require("save")
--local nhouse = house.create_new(text)
--local house_data = save.table_to_string(nhouse)
--print("Made house:",house_data)