; include file


input_player1

; entities 0 is me
; 600 start
	lea	entities,a0	;ok its at me now


;	move.b	#1,(0,a0)	;entity number
;	move.b	#200,(1,a0)	;health
;	move.w	#200,(2,a0)	;xpos
;	move.w	#200,(4,a0)	;ypos
;	move.w	#200,(6,a0)	;old x
;	move.w	#200,(8,a0)	;old y
;	move.b	#0,(10,a0)	;ground
;	move.b	#0,(11,a0)	;frame cnt
;	move.b	#0,(12,a0)	;frame
;	move.b	#1,(13,a0)	;direction 1 or 0
;	move.b	#1,(14,a0)	;needs update
;	move.b	#0,(15,a0)	;speed
;	move.w	#0,(16,a0)	;gravity
;	move.b	#0,(18,a0)	;gravity cntr
;	move.b	#0,(19,a0)	;i jumped
;	move.b	#0,(20,a0)	;spd every 2nd frame
;	move.b	#0,(21,a0)	;in water
;	move.b	#0,(22,a0)	;ladder
;	move.b	#0,(23,a0)	;action what anim set...etc
;	move.b	#0,(24,a0)	;try to grab ladder
;	move.b	#0,(25,a0)	;use item

; 1=walk
; 2=jump
; 3=fall
; 4=swim
; save old x and old y
	move.w	(2,a0),d7
	move.w	d7,(6,a0)	;save old x
	move.w	(4,a0),d7
	move.w	d7,(8,a0)	;save old y


	move.b	(23,a0),d0	;action
	move.b	(14,a0),d1	;needs update


; default to walk anim
	if	b,(23,a0)=#0
	lea	main_player01,a1
	sega_loadtile(#600,#4,a1)
	move.b	#1,(23,a0)	;walk run anim
	move.b	#0,(14,a0)	;no update
	endif





;walk
	move.w	#2,speed	;default speed start
	move.b	(21,a0),d0
	move.b	(22,a0),d1
	if	b,(21,a0)=#1
	sub.w	#1,speed	;slow down in water
	endif
	if	b,d1=#1
	sub.w	#1,speed	;slow down on ladder
	endif

;adjust speed
;	add.b	#1,(15,a0)
;	move.b	(15,a0),d6
;	if	b,d6>#1
;	add.w	#1,speed	;adjusted every second frame
;	move.b	#0,(15,a0)
;	endif

	move.b	(21,a0),d0	;in water
	if	x,#0=inputplayer1_data2
	add.w	#1,speed
	endif


;ladder
	if	x,#0=inputplayer1_data1	or	x,#1=inputplayer1_data1	
	move.b	#1,(24,a0)	;try to grab ladder
	endif
	move.w	#0,moving


	move.b	(22,a0),d6

;	if	b,d6=#1
;	move.w	#1,speed
;	endif

	if	b,(22,a0)=#1	x,#1=inputplayer1_data1
	move.w	speed,d7
	add.w	d7,(4,a0)	;y
	move.w	#1,moving
	endif

	if	b,(22,a0)=#1	x,#0=inputplayer1_data1
	move.w	speed,d7
	sub.w	d7,(4,a0)	;y
	move.w	#1,moving
	endif






;use item
	move.b	(25,a0),d0
	if	x,#2=inputplayer1_data2	b,(25,a0)=#0
	move.b	#1,(25,a0)
	move.b	#0,(12,a0)
	move.b	#0,(11,a0)

	lea	main_player01,a1
	move.l	#0,d7
	move.b	(12,a0),d7
	mulu.w	#128,d7
	add.w	#512,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)


	endif

	if	x,#2!inputplayer1_data2	b,(25,a0)=>#20
	move.b	#0,(25,a0)	;can use again
	lea	main_player01,a1
	move.l	#0,d7
	move.b	(12,a0),d7
	mulu.w	#128,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)
	endif


	move.b	(25,a0),d0
	if	bs,(25,a0)>#0
	add.b	#1,(25,a0)
;	move.w	#1,speed
	move.b	(25,a0),d0
	if	b,(25,a0)>#20
	move.b	#20,(25,a0)
	endif
	endif








	if	x,#3=inputplayer1_data1
	move.w	#1,moving
	move.b	#0,(13,a0)	;direction facing
	move.w	speed,d7
	add.w	d7,(2,a0)
	endif

	if	x,#2=inputplayer1_data1
	move.w	#1,moving
	move.b	#1,(13,a0)	;direction facing
	move.w	speed,d7
	sub.w	d7,(2,a0)
	endif


	move.b	(18,a0),d6	;grav cntr
	move.b	(19,a0),d5	;jump cnt
	move.b	(10,a0),d4	;on ground
	move.b	(21,a0),d3	;in water
	move.b	(23,a0),d2	;action fix
	if	b,(21,a0)=#1	b,(23,a0)!#1
	move.b	#0,(23,a0)
	move.b	#1,(14,a0)
	endif




	if	x,#1=inputplayer1_data2	b,d5>#0	b,d5<#9	b,d3=#0
	add.w	#2,(16,a0)
	add.b	#1,(19,a0)	;add counter
	endif
;start jump
	if	x,#1=inputplayer1_data2	b,d5=#0	b,d4=#1	b,d3=#0

	ym_playsfx('jump.wav',#3,#1)

	add.b	#1,(19,a0)	;add counter
	move.w	#4,(16,a0)	;start grav
	move.b	#2,(18,a0)	;fix start
	move.b	#0,(10,a0)	;off ground
	move.b	#2,(23,a0)	;jump anim
	move.b	#0,(22,a0)	;let go of ladder
	move.b	#0,(24,a0)	;let go

	lea	main_player01,a1
	move.l	#2048,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)

	endif

;start jump swim
	if	x,#1=inputplayer1_data2	b,d3=#1	b,d5=#0
	add.b	#10,(19,a0)	;add counter
	move.w	#8,(16,a0)	;start grav

;	if	x,#1=inputplayer1_data1
;	move.w	#65529,(16,a0)	;start grav
;	endif
;	if	x,#0=inputplayer1_data1
;	move.w	#8,(16,a0)	;start grav
;	endif


	move.b	#2,(18,a0)	;fix start
	move.b	#0,(10,a0)	;off ground
;	move.b	#2,(23,a0)	;jump anim
	endif


;jump reset
	if	x,#1!inputplayer1_data2	b,d4=#1	or	b,d3=#1	x,#1!inputplayer1_data2
	move.b	#0,(19,a0)
	endif
;*********************************************************************************************
;update animation
	move.b	(21,a0),d2	;in water
	move.b	(15,a0),d3	;spd frame
	move.b	(25,a0),d4	;using item

	if	w,moving=#1	b,d4=#0	or	b,d2=#1	b,d4=#0
	add.b	#1,(11,a0)
	move.b	(11,a0),d0	;frame cnt
	if	b,d0>#3	or	b,d4=#0	b,d0>#1	x,#0=inputplayer1_data2
	add.b	#1,(12,a0)	;increase frame
	move.b	#0,(11,a0)	;reset counter
	move.b	(12,a0),d1	;current frame
	if	b,d1>#3
	move.b	#0,(12,a0)
	endif
;load it
	move.b	(12,a0),d0	;frame
	move.b	(23,a0),d1	;action
	move.b	(21,a0),d2	;in water
	move.b	(22,a0),d3	;ladder
	move.b	(25,a0),d4	;using item


;walk run
	if	b,d1=#1	b,d2=#0	b,d3=#0
	lea	main_player01,a1
	move.l	#0,d7
	move.b	(12,a0),d7
	mulu.w	#128,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)
	endif
;jumping
	if	b,d1=#2
	lea	main_player01,a1
	move.l	#2048,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)
	endif

;swim
	if	b,d1=#1	b,d2=#1	b,d3=#0
	lea	main_player01,a1
	move.l	#0,d7
	move.b	(12,a0),d7
	mulu.w	#128,d7
	add.w	#1024,d7	;swim range
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)
	endif

;ladder
	if	b,d1=#1	b,d2=#0	b,d3=#1
	lea	main_player01,a1
	move.l	#0,d7
	move.b	(12,a0),d7
	mulu.w	#128,d7
	add.w	#1536,d7	;swim range
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)
	endif


	endif


	endif


;*********************************************************************************************
;update animation item
	move.b	(21,a0),d2	;in water
	move.b	(15,a0),d3	;spd frame
	move.b	(25,a0),d4	;using item

	if	b,d4!#0
	add.b	#1,(11,a0)
	move.b	(11,a0),d0	;frame cnt
	if	b,d0>#2
	add.b	#1,(12,a0)	;increase frame
	move.b	#0,(11,a0)	;reset counter
	move.b	(12,a0),d1	;current frame
	if	b,d1>#3
	move.b	#0,(12,a0)
	move.b	#0,(14,a0)
	move.b	#0,(23,a0)

	endif
;load it
	move.b	(12,a0),d0	;frame
	move.b	(23,a0),d1	;action
	move.b	(21,a0),d2	;in water
	move.b	(22,a0),d3	;ladder
	move.b	(25,a0),d4	;using item


;use weapon

	lea	main_player01,a1
	move.l	#0,d7
	move.b	(12,a0),d7
	mulu.w	#128,d7
	add.w	#512,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)


	endif
	endif




;gravity
;***************************************************************************************
	move.b	(18,a0),d6	;grav cntr


;check at all times cause it hits every second frame
	move.b	(10,a0),d5
	if	b,d5=#1
	move.w	#0,(16,a0)
;reset fall here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	move.b	(23,a0),d0	;action
	if	b,d0=#3	or	b,d0=#2
	move.b	#0,(23,a0)	;need reset
	move.b	#1,(14,a0)	;need update
	endif
	endif


	if	bs,d6>#1
	move.b	#0,(18,a0)
	move.b	(10,a0),d0	;on ground ?

	if	b,d0=#0
	sub.w	#2,(16,a0)	;pull
; 2 minimum pull
	move.w	(16,a0),d6


	if	w,d6=#65535
	move.w	#65534,(16,a0)
	endif


	move.w	(16,a0),d6
; switch to fall
	move.b	(23,a0),d0	;action
	move.b	(14,a0),d1	;needs update

	if	ws,d6<#65532	b,d0!#3
	move.b	#0,(14,a0)	;no update
	move.b	#3,(23,a0)	;action= fall
	lea	main_player01,a1
	move.l	#2176,d7
	lea	(a1,d7),a1
	sega_loadtile(#600,#4,a1)	;loaded fall
	endif


	move.b	(21,a0),d5	;in water
	if	ws,d6<#65529	b,d5=#0
	move.w	#65529,(16,a0)
	endif
;in water
	if	ws,d6<#65534	b,d5=#1
	move.w	#65534,(16,a0)	;go back up
	endif



	else
	move.w	#0,(16,a0)	;no pull
	endif


	move.w	(16,a0),d6
	sub.w	d6,(4,a0)	;set gravity


	endif
	add.b	#1,(18,a0)	;gravity cntr

;debug
;	move.b	(22,a0),d6
;	and.w	#255,d6
;	wordstring(d6,#word)
;	sega_printa(#10,#10,#word,#0,#0,#1,#0)
;



	jsr	tile_checks


	jsr	update_screenscroll



;update graphic

	move.w	(2,a0),d5	;xpos
	sub.w	nscreenx,d5

	move.w	(4,a0),d6	;ypos
	sub.w	screeny,d6

	if	w,d5>#100	w,d5<#500	w,d6>#100	w,d6<#500

; now me
;	move.b	(13,a0),d4	;direction

	sega_setsprite(sprite2,#600,d5,d6,#1,#1,(13,a0),#0,#2,#0)
	add.b	#1,sprite2


	endif



	rts

; each entity has 0-39 slots

def	temp	word
def	temp2	word

def	entities	string	480
def	tilex	word
def	tiley	word
def	tile	word
def	ground	byte
def	ground2	byte
def	moving	word
def	speed	word



