-- Default character skin functions
-- Handles all the old-style MyChar.pbm skin replacements! :3
-- All characters should be based on this.
-- WARNING: RIDICULOUSLY MASSIVE WORK-IN-PROGRESS! :x
local CHAR = {}
CHAR.collision = {5,8,5,8}
CHAR.physics = {
	{ -- Normal physics
		accel = 0x55,
		air_accel = 0x20,
		decel = 0x33,
		max_walk = 0x32C,
		jump = 0x500,
		air_jump = 0x20,
		air_fall = 0x50,
		max_gravity = 0x5FF
	},
	{ -- Underwater physics
		accel = 0x2A,
		air_accel = 0x10,
		decel = 0x19,
		max_walk = 0x196,
		jump = 0x280,
		air_jump = 0x10,
		air_fall = 0x28,
		max_gravity = 0x2FF
	},
	-- Waterway boss physics, etc.
}

if CLIENT

CHAR.sheet = pbm.load("MyChar")
CHAR.death = 0x11
CHAR.pain = 0x10
CHAR.smoke = 4
CHAR.display = {8,8,8,8}

CHAR.anims = {
	[ANIM.STAND] = {
		{{0,0},{0,16},-1}
	},
	[ANIM.STAND|ANIM.UP] = {
		{{48,0},{48,16},-1}
	},
	[ANIM.WALK] = {
		{{16, 0},{16,16},5},
		{{ 0, 0},{ 0,16},5},
		{{32, 0},{32,16},5},
		{{ 0, 0},{ 0,16},5}
	},
	[ANIM.WALK|ANIM.UP] = {
		{{64, 0},{64,16},5},
		{{48, 0},{48,16},5},
		{{80, 0},{80,16},5},
		{{48, 0},{48,16},5}
	},
	[ANIM.JUMP] = {
		{{16,0},{16,16},-1}
	},
	[ANIM.JUMP|ANIM.UP] = {
		{{64,0},{64,16},-1}
	},
	[ANIM.JUMP|ANIM.DOWN] = {
		{{96,0},{96,16},-1}
	},
	[ANIM.LAND] = {
		{{32,0},{32,16},-1}
	},
	[ANIM.LAND|ANIM.UP] = {
		{{80,0},{80,16},-1}
	},
	[ANIM.LAND|ANIM.DOWN] = {
		{{96,0},{96,16},-1}
	},
	[ANIM.CHECK] = {
		{{112,0},{112,16},-1}
	},
	[ANIM.SIT] = {
		{{128,16},nil,-1}
	},
	[ANIM.FALL] = {
		{{144,0},{144,16},-1}
	},
}

function CHAR:think()
	local anim = self.anim or ANIM.STAND

	local btn_start = input.btn_start
	local buttons = input.buttons
	if tsc.keylock
		btn_start = 0
		buttons = 0
	end

	local control = 0
	if buttons & 1<<BTN.LEFT ~= 0
		control = control-1
	end
	if buttons & 1<<BTN.RIGHT ~= 0
		control = control+1
	end

	local accel
	if control == 0
		-- Slide to a halt!
		if self.grounded and anim ~= ANIM.CHECK
			anim = ANIM.STAND
		end
		if self.mx > 0
			self.mx = self.mx - self.physics.decel
			if self.mx < 0
				self.mx = 0
			end
		elseif self.mx < 0
			self.mx = self.mx + self.physics.decel
			if self.mx > 0
				self.mx = 0
			end
		end
	elseif self.grounded
		-- If you're on the ground, walk normally
		accel = self.physics.accel
	else
		-- In the air you have less control
		accel = self.physics.air_accel
	end

	if accel
		if self.grounded
			anim = ANIM.WALK
		end
		if control < 0
			if self.mx > -self.physics.max_walk
				self.mx = self.mx - accel
				if self.mx < -self.physics.max_walk
					self.mx = -self.physics.max_walk
				end
			end
			self.flip = false
		elseif control > 0
			if self.mx < self.physics.max_walk
				self.mx = self.mx + accel
				if self.mx > self.physics.max_walk
					self.mx = self.physics.max_walk
				end
			end
			self.flip = true
		end
	end

	if self.gravity
		if self.my < self.physics.max_gravity
			if buttons & 1<<BTN.JUMP ~= 0 and self.my < 0
				self.my = self.my + self.physics.air_jump
			else
				self.my = self.my + self.physics.air_fall
			end
			if self.my > self.physics.max_gravity
				self.my = self.physics.max_gravity
			end
		end

		if self.grounded
			if btn_start & 1<<BTN.JUMP ~= 0
				anim = ANIM.JUMP
				self.my = -self.physics.jump
			elseif anim == ANIM.STAND and btn_start & 1<<BTN.CHECK ~= 0
				anim = ANIM.CHECK
				net.send_check(self)
			end
		else
			if self.my < 0
				anim = ANIM.JUMP
			else
				anim = ANIM.LAND
			end
		end
	end

	-- Change your animation if needed
	if (anim == ANIM.STAND or anim == ANIM.WALK or anim == ANIM.JUMP or anim == ANIM.LAND)
	and buttons & 1<<BTN.UP ~= 0
		anim = anim | ANIM.UP
	elseif (anim == ANIM.JUMP or anim == ANIM.LAND)
	and buttons & 1<<BTN.DOWN ~= 0
		anim = anim | ANIM.DOWN
	end
	if anim ~= self.anim
		self.anim = anim
		self.frame = 1
		self.ticks = 0
	end
end

end

return CHAR
