#module \
Goatoh

#enum LEG_LF = 0
#enum LEG_RF
#enum LEG_LH
#enum LEG_RH

#enum LEG_DIRECTION_FRONT = 1
#enum LEG_DIRECTION_HIND

#defcfunc local \
New \
		double	p_initPosX,\
		double	p_initPosY,\
		int	p_initDirection
	posX.insNum = p_initPosX
	posY.insNum = p_initPosY

	If Length2 (legArgs) = 0 {
		DDim legArgs, 4, 1
	}

	If Length2 (legDirections) = 0 {
		Dim legDirections, 4, 1
	}

	legArgs.LEG_LF.insNum = 0.
	legArgs.LEG_RF.insNum = 0.
	legArgs.LEG_LH.insNum = 0.
	legArgs.LEG_RH.insNum = 0.

	legDirections.LEG_LF.insNum = LEG_DIRECTION_FRONT
	legDirections.LEG_RF.insNum = LEG_DIRECTION_HIND
	legDirections.LEG_LH.insNum = LEG_DIRECTION_HIND
	legDirections.LEG_RH.insNum = LEG_DIRECTION_FRONT

	scale.insNum = 2.

	; 角度
	arg.insNum = 0.

	; 首の角度
	neckArg.insNum = 0.

	walkVel.insNum = 3.
	walkAcl.insNum = 0.

	runVel.insNum = 5.
	runVel.insNum = 0.

	direction.insNum = p_initDirection

	walking.insNum = True
	running.insNum = False
	eating.insNum = False
	openingEyes.insNum = False
	openingMouth.insNum = False

	frame.insNum = 0

	insNum++

	Return insNum - 1

#deffunc local \
Walk \
		int	p_insId
	If direction.p_insId = DIRECTION_LEFT {
		posX.p_insId -= walkVel.p_insId * Cos (arg.p_insId)
		posY.p_insId -= walkVel.p_insId * Sin (arg.p_insId)
	}
	Else {
		posX.p_insId += walkVel.p_insId * Cos (arg.p_insId)
		posY.p_insId += walkVel.p_insId * Sin (arg.p_insId)
	}

	Repeat 4
		If legDirections.cnt.p_insId = LEG_DIRECTION_FRONT {
			legArgs.cnt.p_insId += Deg2Rad (1)
		}
		Else {
			legArgs.cnt.p_insId -= Deg2Rad (1)
		}

		If legArgs.cnt.p_insId > Deg2Rad (15) {
			legDirections.cnt.p_insId = LEG_DIRECTION_HIND
		}
		Else: If legArgs.cnt.p_insId < Deg2Rad (-15) {
			legDirections.cnt.p_insId = LEG_DIRECTION_FRONT
		}
	Loop

	Return

#deffunc local \
DoRun \
		int	p_insId
	Return

#deffunc local \
Eat \
		int	p_insId
	Return

#deffunc local \
Draw \
		int	p_insId
	Pos posX.p_insId - scale.p_insId * 50, posY.p_insId + scale.p_insId * 26
	CelPut BUFFER_GOATOH_LEG, 0, scale.p_insId, scale.p_insId, legArgs.LEG_RF.p_insId

	Pos posX.p_insId + scale.p_insId * 30, posY.p_insId + scale.p_insId * 26
	CelPut BUFFER_GOATOH_LEG, 0, scale.p_insId, scale.p_insId, legArgs.LEG_RH.p_insId

	Pos posX.p_insId + scale.p_insId * 64, posY.p_insId - scale.p_insId * 12
	CelPut BUFFER_GOATOH_TAIL, 0, scale.p_insId, scale.p_insId

	Pos posX.p_insId, posY.p_insId
	CelPut BUFFER_GOATOH_TARSO, 0, scale.p_insId, scale.p_insId

	Pos posX.p_insId - scale.p_insId * 30, posY.p_insId + scale.p_insId * 30
	CelPut BUFFER_GOATOH_LEG, 0, scale.p_insId, scale.p_insId, legArgs.LEG_LF.p_insId

	Pos posX.p_insId + scale.p_insId * 50, posY.p_insId + scale.p_insId * 30
	CelPut BUFFER_GOATOH_LEG, 0, scale.p_insId, scale.p_insId, legArgs.LEG_LH.p_insId

	Pos posX.p_insId - scale.p_insId * 64, posY.p_insId - scale.p_insId * 20
	CelPut BUFFER_GOATOH_HEAD, 0, scale.p_insId, scale.p_insId, neckArg.p_insId

	Pos posX.p_insId - scale.p_insId * 64, posY.p_insId - scale.p_insId * 20
	If openingEyes {
		If openingMouth {
			CelPut BUFFER_GOATOH_FACE_OO, 0, scale.p_insId, scale.p_insId, neckArg.p_insId
		}
		Else {
			CelPut BUFFER_GOATOH_FACE_OC, 0, scale.p_insId, scale.p_insId, neckArg.p_insId
		}
	}
	Else {
		If openingMouth {
			CelPut BUFFER_GOATOH_FACE_CO, 0, scale.p_insId, scale.p_insId, neckArg.p_insId
		}
		Else {
			CelPut BUFFER_GOATOH_FACE_CC, 0, scale.p_insId, scale.p_insId, neckArg.p_insId
		}
	}

	Interval@Goatoh p_insId

	Return

#deffunc local \
Interval \
		int	p_insId
	If walking.p_insId {
		Walk@Goatoh p_insId

		If frame.p_insId \ 150 == 30 {
			MMPlay SOUND_MUMUMUMU

			openingMouth = True
		}
	}
	Else: If running.p_insId {
		DoRun@Goatoh p_insId
	}

	If eating.p_insId {
		Eat@Goatoh p_insId
	}

	frame.p_insId++

	Return

#global

