aboutsummaryrefslogtreecommitdiff
path: root/src/char.moon
blob: 5e1a35c0a42981713f62f1caee8ea9f3a2840b37 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
util = require "util"
broadphase = require "broadphase"
main = require "main"
constrain = require "constrain"
color = require "color"
room = require "room"
import LobbyRoom from room
ability_reg = require "ability_reg"


mod = ...
mod.characters = {}
mod.classes = {}
require "char_tank"
require "char_mage"
require "char_theif"
require "char_fool"
require "char_jugg"
mod.class_order = {
	"Tumbler",
	"Fire Breather",
	"Juggler",
	"Troubador",
	"Juggernaut"
}
mod.class_order_rev = {}
for k,v in ipairs(mod.class_order)
	mod.class_order_rev[v] = k
print("After requireing characters, mod.classes was",mod.classes)
mod.enemies = {}
--require "e_rat"
require "e_bethany"
require "e_ruminating_randy"
require "e_mopey_marvin"
require "e_sullen_salley"
require "e_child"
print("After requireing rat, mod.enemies was",mod.enemies)
--print("test")
--[[Stores a single input, and the time it was inputed]]
class KeyInput
	new:(key = "<input>") =>
		@time = 0
		@key = key
		@value = false

--print "test"

class AnimFrame
	new:(anim,interupt,mode) =>
		@anim = anim
		@interupt = interupt
		@mode = mode

class ActionInput
	new:(action = "<input>") =>
		@time = 0
		@action = action

mod.sprite_direction_co = () =>
	return () ->
		while true
			@stop_anim(@sprites.right)
			@stop_anim(@sprites.left)
			@stop_anim(@sprites.stop_right)
			@stop_anim(@sprites.stop_left)
			@stop_anim(@sprites.falling_left)
			@stop_anim(@sprites.falling_right)
			for input in *@inputs
				if @velocity.x > 0.01
					@set_anim(@sprites.right,1,"loop")
					break
				elseif @velocity.x < -0.01
					@set_anim(@sprites.left,1,"loop")
					break
			coroutine.yield!

mod.can_die_co = () =>
	return () ->
		while not @dead
			coroutine.yield!

		for k,v in pairs @sprites
			if k ~= "idle"
				@stop_anim(v)
		if @sprites.die
			@set_anim(@sprites.die,5)
		coroutine.yield(true)

mod.make_animate = (c) ->
	assertf(c.sprites ~= nil, "Tried to animate something that had no .sprites: %q", c.__class.__name)
	assertf(c.sprites.idle ~= nil and #c.sprites.idle > 0, "Tried to animate something without a .idle animation: %q", c.__class.__name)
	c.anim_stack = {AnimFrame(c.sprites.idle,0,"loop")}
	c.anim = c.sprites.idle
	c.keyframe = 0
	c.animrate = c.animrate or 1
	c.anim_interupt = 0
	c.set_anim = (self,tbl,interupt,mode) ->
		if type(tbl) ~= "table"
			error("Tried to set anim to something that was not a table!",2)
		if #tbl == 0
			error("Tried to set anim to an empty table",2)
		if interupt > @anim_interupt
			table.insert(@anim_stack,AnimFrame(tbl,interupt,mode))
			@anim = @anim_stack[#@anim_stack].anim
			@anim_interupt = interupt

	c.stop_anim = (self,tbl,err_if_unable=false) ->
		anim_found = false
		for k,v in pairs @anim_stack
			if v.anim == tbl
				--print("Found anim to remove")
				anim_found = true
				table.remove(@anim_stack,k)
				break
		if err_if_unable
			assertf(anim_found, "Could not find animation to remove")
		@anim = @anim_stack[#@anim_stack].anim
		@anim_interupt = @anim_stack[#@anim_stack].interupt

	c.node\action(coroutine.create(() ->
		while not c.dead
			c.keyframe = math.floor(am.current_time()*c.animrate) % #c.anim
			spritename = c.anim[c.keyframe + 1]
			assert(spritename, "Failed to find an appropriate image to draw.")
			--print("Setting:",spritename)
			c.node("sprite").source = spritename
			coroutine.yield!
		--we are dead
		keyframe_0 = am.current_time()
		while c.keyframe < #c.anim - 1
			c.keyframe = math.floor((am.current_time! - keyframe_0)*c.animrate)
			assert(c.anim[c.keyframe + 1], "Failed to find an appropriate image to draw.")
			c.node("sprite").source = c.anim[c.keyframe + 1]
			coroutine.yield!
		c\remove()
		coroutine.yield(true)
	))

mod.inherited = {}
hp_bar_width = 20
--[[The character, extended to make both players and ai]]
class Character
	@classes = {}
	@players = {} -- players singleton, [peerid] = Character
	new:(uname, data, charclass) =>
		assert(charclass, "Charclass may not be nil")
		@uname = uname or false
		@data = data or {
			status: "active",
			location: -1,
			position: charclass.default_position
		}
		@class = charclass
		assert(@class.name, "Character classes must have a name")
		@calc_class_values!
		@node = am.group!

		--print("Creating character!",uname,data,charclass)
		@node\append(am.translate(0,0)\tag("char_translate")^ am.sprite(@class.sprite,color.white,"center","bottom")\tag("char_sprite"))
		print("A character has been created!")
		print(debug.traceback!)
		--Draw healthbar
		healthbar_trans = am.translate(0,60)
		healthbar_left = am.sprite("data/bar_left.png", color.white,"left","center")\tag("hp_l")
		healthbar_right = am.sprite("data/bar_right.png", color.white, "right","center")\tag("hp_r")
		healthbar_bar = am.sprite("data/bar_mid.png", color.white,"left","center")\tag("hp_b")
		healthbar_fill = am.sprite("data/bar_fill.png", color.white,"left","center")\tag("hp_f")
		healthbar_scale = am.scale(hp_bar_width,1)
		healthbar_fill_scale = am.scale(hp_bar_width + 1,1)\tag("hp_fill_scale")
		healthbar_trans\append(am.translate(-hp_bar_width,0)^ healthbar_left)
		healthbar_trans\append(am.translate(hp_bar_width,0)^ healthbar_right)
		healthbar_trans\append(am.translate(-hp_bar_width/2,0)^ healthbar_scale^ healthbar_bar)
		healthbar_trans\append(am.translate((-hp_bar_width/2) - 1,0)^ healthbar_fill_scale^ healthbar_fill)
		@.node("char_sprite")\append(healthbar_trans)
		--End draw healthbar
		assert(@.__class.draw,"Characters must have a draw() method")
		table.insert(mod.characters,@)
		constrain(@,"set anim", (self,value) ->
			assertf(type(value) == "table", "Tried to set anim on %q to something other than a table (%q)",@, type(value))
			assert(#value > 0, "Tried to set animation for char to something with 0 frames!")
		)
		assert(@.__class != Character,"Character class must be subclassed")
		--main.root("world_characters")\append(@node)

	__tostring: () =>
		return string.format(
			"<%s, %s> at (%d)",
			@.__class.__name,
			(@class and @class.name or "no class"),
			( @data and @data.position or -1)
		)

	@__inherited: (c) =>
		assert(c, "Inheritance must exist")
		assert(c.__name, "Inherited class must have a .__name")
		@@.classes[c.__name] = c
		mod.inherited[c.__name] = c

	calc_class_values: () =>
		for k,v in pairs(@class)
			if k\match("^default")
				field = k\match("^default_(.*)")
				if type(v) == "function"
					@data[field] = v!
				else
					@data[field] = v

	set_field: (name, value) =>
		assert(@data[name], "Field must exist to be set")
		@data[name] = value
		print("my data table is:",@data)
		if name == "hp"
			perc = (@data.hp / @data.maxhp) * 20
			@.node("hp_fill_scale").x = perc

	serialize: () =>
		print("Serializing char:",@)
		print("Name:", @@__name)
		print("uname:",@uname)
		print("data:",@data)
		data_abilities = {}
		for i,ability in pairs(@data.abilities)
			data_abilities[i] = ability.__name
		data_copy = table.shallow_copy(@data)
		data_copy.abilities = data_abilities
		print("class.name:",@class.name)
		ret = am.to_json({name:@@__name, uname:@uname, data:data_copy, class:@class.name})
		print("Ret is:",ret)
		ret
	
	deserialize: (data) ->
		print("Deserializing character")
		tbl = am.parse_json(data)
		if mod.classes[tbl.class]
			data_abilities = {}
			for i, ability_name in pairs(tbl.data.abilities)
				data_abilities[i] = ability_reg[ability_name]
			tbl.data.abilities = data_abilities
			return mod.inherited[tbl.name](tbl.uname, tbl.data, mod.classes[tbl.class])
		elseif mod.enemies[tbl.class]
			e = mod.Enemy(tbl.data, mod.enemies[tbl.class])
			e.uname = tbl.uname
			return e


	draw:(screenpos) =>
		print("draw")
	remove: () =>
		@node\remove_all!
	die: () =>
		@dead = true
		print(@,"is dieing, node is",@.node,"and color is",color)
		@.node("char_sprite")\append(am.line(vec2(-10,-10),vec2(10,10),5,color.bright))
		@.node("char_sprite")\append(am.line(vec2(10,-10),vec2(-10,10),5,color.bright))
	enter_room: (room) =>
		@room = room
		print("Character",@,"entered room",room)
		@.node("char_translate").y = room.floor_y
		if room.__class == LobbyRoom
			print("Class was lobbyRoom")
			rng_x = math.random(-(main.width/2), (main.width/2))
			print("RNG x:",rng_x)
			@.node("char_translate").x = rng_x
		else
			print("Class was not LobbyRoom")
			x_pos = @room\player_location_of(@data.position)
			print("Got x pos for",@,x_pos)
			@.node("char_translate").x = x_pos + math.random(-15,15) --some random variance to stop stacking
	set_class: (newclass) =>
		assert(newclass, "Cannot set a class to nil")
		@class = newclass
		@calc_class_values!
		@.node("char_sprite").source = newclass.sprite
	
	set_position: (pos) =>
		@data.position = pos
	
	set_location: (loc) =>
		@data.location = loc

mod.enemy_counter = 0
class Enemy extends Character
	new: (data, eclass) =>
		super(eclass.name .. ":" .. tostring(mod.enemy_counter), data, eclass)
		mod.enemy_counter += 1
	nwuname: (uname, data, eclass) ->
		@(data, eclass)

		mod.enemy_counter += 1
	__tostring: () =>
		return string.format("<%s> at (%d)",@uname, @data.position or 0)

	enter_room: (room) => --overload character's to get enemy locations
		print("Character",@,"entered room",room)
		@room = room
		@.node("char_translate").y = @room.floor_y
		@.node("char_translate").x = @room\enemy_location_of(@data.position)
	
	select_action: () =>
		return @class.select_action(@)

mod["Character"] = Character
mod["KeyInput"] = KeyInput
mod["Enemy"] = Enemy
--Things that extend the character class
mod