.module UI_System
.include "cpct_globals.h.s"
.include "./manager/entities.h.s"
.include "./manager/music.h.s"
.include "./manager/game.h.s"
.include "cpctelera.h.s"
.include "./manager/rounds.h.s"
.include "./manager/screen.h.s"

.globl _myDrawStringM0
.globl _myDrawChar_inner

lifeString: .asciz "LIFES"
foodString: .asciz "SCORE"
scoreString: .asciz "0"

food_life: .db 0x10
food_life_pos: .ds #2

score: .db #00, #00, #00, #00
score_protection: .db #0

score_u = 3
score_d = 2
score_c = 1
score_m = 0

rounds_ui: .db #01, #00 ;;SIEMPRE 01, 00

rounds_u = 0
rounds_d = 1

.globl _sprite_corazon ;4bytes x 9

sys_ui_init::
    cpctm_screenPtr_asm de, #0xC000, #0, #0
    ld c, #64 ;x
    ld b, #10;y
    xor a
    call cpct_drawSolidBox_asm

    cpctm_screenPtr_asm de, #0xC000, #64, #0
    ld c, #16 ;x
    ld b, #10;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;; Cargamos los colores
    ld hl, #0x0004
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    ;;  -------------------------------------------------
    ;;
    ;;  Dibujar "Lifes"
    ;;
    ;;
    call sys_ui_draw_lifes
    ;;  -------------------------------------------------
    ;;
    ;;  Dibujar "Score"
    ;;
    ;;

    cpctm_screenPtr_asm hl, #CPCT_VMEM_START_ASM, #40, #01

    ld   iy, #foodString   ;; IY = Pointer to the string 
    call _myDrawStringM0  ;; Draw the string

    ;;  -------------------------------------------------
    ;;
    ;; Numero del score
    ;;
    ;;score_u = 3
    ;;score_d = 2
    ;;score_c = 1
    ;;score_m = 0

    ld 	iy, #score
    ;;
    ;;  MILLAR
    ;;
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    ld de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ld c,   #63
    ld b,   #01
    call cpct_getScreenPtr_asm
    ld a, score_m(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    ld e, a
    call _myDrawChar_inner
    ;;
    ;;  CENTENAS
    ;;
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    ld de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ld c,   #67
    ld b,   #01
    call cpct_getScreenPtr_asm
    ld a, score_c(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    ld e, a
    call _myDrawChar_inner  
    ;;
    ;;  DECENAS
    ;;
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    ld de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ld c,   #71
    ld b,   #01
    call cpct_getScreenPtr_asm
    ld a, score_d(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    ld e, a
    call _myDrawChar_inner
    ;;
    ;;  UNIDADES
    ;;
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    ld de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ld c,   #75
    ld b,   #01
    call cpct_getScreenPtr_asm
    ld a, score_u(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    ld e, a
    call _myDrawChar_inner

    ;;  -------------------------------------------------
    ;;
    ;; RONDAS
    ;;
    call get_round
    cp #1
    jr z, first_round

    ret

    first_round:

    ld 	iy, #rounds_ui
    ;;
    ;;  UNIDADES
    ;;
    ld l, #14    ;; Color de la letra
    ld h, #3    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    cpctm_screenPtr_asm hl, #0xC000, #23, #17
    ld a, rounds_u(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    ld e, a
    call _myDrawChar_inner
ret

inc_unidades_rounds::
	ld 	iy, #rounds_ui
	ld   a, rounds_u(iy)
	cp   a, #9
	jr  nz, inc_unidades_round
		ld rounds_u(iy), #0
		call inc_decenas_rounds
        ret
	inc_unidades_round:
	inc a
	ld  rounds_u(iy), a
ret


inc_decenas_rounds::
	ld 	iy, #rounds_ui
	ld  a, rounds_d(iy)
	inc a
	ld  rounds_d(iy), a
ret

sys_ui_update_rounds_decenas::
    ld 	iy, #rounds_ui
    ld l, #14    ;; Color de la letra
    ld h, #3    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    cpctm_screenPtr_asm hl, #0xC000, #21, #17
    ld a, rounds_d(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_update_rounds_unidades::
    ld 	iy, #rounds_ui
    ld l, #14    ;; Color de la letra
    ld h, #3    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    call get_round
    cp #10
    jp m, centro_unidades

    cpctm_screenPtr_asm hl, #0xC000, #25, #17
    jp left_side
    centro_unidades:

    cpctm_screenPtr_asm hl, #0xC000, #23, #17
    left_side:

    ld a, rounds_u(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

inc_unidades_score::
    ld a, (#score_protection)
    cp #4
    ret nc
	ld 	iy, #score
    repeat_unidades_score:
	ld   a, score_u(iy)
	cp   a, #9
	jr  nz, inc_unidades
        ld a, (#score_protection)
        cp #3
        jr nc, not_inc_unidades
		ld score_u(iy), #0
        push bc
        ld b, #1
		call inc_decenas_score
        pop bc
        dec b
        jr nz, repeat_unidades_score
        ret
	inc_unidades:
	inc a
	ld  score_u(iy), a
    dec b
    jr nz, repeat_unidades_score
    ret 
    not_inc_unidades:
    ld a, #4
    ld (#score_protection), a
ret

inc_decenas_score::
    ld a, (#score_protection)
    cp #3
    jr c, decenas_protection
        ld b, #10
        call inc_unidades_score
        ret
    decenas_protection:

	ld 	iy, #score
    repeat_decenas_score:
	ld   a, score_d(iy)
	cp   a, #9
	jr  nz, inc_decenas
        ld a, (#score_protection)
        cp #2
        jr nc, not_inc_decenas
		ld score_d(iy), #0
        push bc
        ld b, #1
        call inc_centenas_score
        pop bc
        dec b
        jr nz, repeat_decenas_score
        ret
	inc_decenas:
	inc a
	ld  score_d(iy), a
    dec b
    jr nz, repeat_decenas_score
    ret
    not_inc_decenas:
    ld a, #3
    ld (#score_protection), a
    ld b, #10
    call inc_unidades_score
ret

inc_centenas_score::
    ld a, (#score_protection)
    cp #2
    jr c, centenas_protection
        ld b, #10
        call inc_decenas_score
        ret
    centenas_protection:

	ld 	iy, #score
    repeat_centenas_score:
	ld   a, score_c(iy)
	cp   a, #9
	jr  nz, inc_centenas
        ld a, (#score_protection)
        cp #1
        jr nc, not_inc_centenas
		ld score_c(iy), #0
		call inc_millar_score
        dec b
        jr nz, repeat_centenas_score
        ret
	inc_centenas:
	inc a
	ld  score_c(iy), a
    dec b
    jr nz, repeat_centenas_score
    ret
    not_inc_centenas:
    ld a, #2
    ld (#score_protection), a
    ld b, #10
    call inc_decenas_score
ret

inc_millar_score::
	ld 	iy, #score
	ld   a, score_m(iy)
	cp   a, #9
	jr  nz, inc_millar
		ret
	inc_millar:
	inc a
	ld  score_m(iy), a
    cp   a, #9
	jr  nz, inc_millar_protection
        ld a, #1
        ld (#score_protection), a
	inc_millar_protection:
ret

sys_ui_update_score_millar::
    ld 	iy, #score
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    cpctm_screenPtr_asm hl, #0xC000, #63, #01
    ld a, score_m(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_update_score_centenas::
    ld 	iy, #score
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours

    cpctm_screenPtr_asm hl, #0xC000, #67, #01
    ld a, score_c(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_update_score_decenas::
    ld 	iy, #score
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    cpctm_screenPtr_asm hl, #0xC000, #71, #01
    ld a, score_d(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_update_score_unidades::
    ld 	iy, #score
    ld l, #4    ;; Color de la letra
    ld h, #0    ;; Color del fondo
    call cpct_setDrawCharM0_asm   ;; Set draw char colours
    cpctm_screenPtr_asm hl, #0xC000, #75, #01
    ld a, score_u(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

;; ----------------------------------------
;;  FUNCIONES PARA LA DEATH SCREEN
;;


sys_ui_update_score_millar_ds::
    ld 	iy, #score
    cpctm_screenPtr_asm hl, #0xC000, #60, #140
    ld a, score_m(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_update_score_centenas_ds::
    ld 	iy, #score
    cpctm_screenPtr_asm hl, #0xC000, #64, #140
    ld a, score_c(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret


sys_ui_update_score_decenas_ds::
    ld 	iy, #score
    cpctm_screenPtr_asm hl, #0xC000, #68, #140
    ld a, score_d(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_update_score_unidades_ds::
    ld 	iy, #score
    cpctm_screenPtr_asm hl, #0xC000, #72, #140
    ld a, score_u(iy)
    and #0x0F
    add #48  ;; primer digito en Ascii
    call _myDrawChar_inner
ret

sys_ui_init_food_life::
    ld a, #16
    ld (food_life), a
ret

sys_ui_get_life::
    ld  a, (#food_life)
    ld b, a
ret

sys_ui_init_score::
    ld ix, #score
    ld score_m(ix), #0
    ld score_d(ix), #0
    ld score_c(ix), #0
    ld score_u(ix), #0
    ld a, #0
    ld (#score_protection), a
ret

sys_ui_init_rounds::
    ld ix, #rounds_ui
    ld rounds_d(ix), #0
    ld rounds_u(ix), #1
ret

sys_ui_rounds_unidades::
    ld ix, #rounds_ui
    ld a, rounds_u(ix)
ret

sys_ui_rounds_decenas::
    ld ix, #rounds_ui
    ld a, rounds_d(ix)
ret

sys_ui_draw_lifes::
    xor a
    call man_entity_get_from_idx
    ld a, e_lifes(ix)
    cp #1
    jr c, no_lifes
    ;; CORAZON 1
    cpctm_screenPtr_asm de, #0xC000, #02, #00
    ld hl, #_sprite_corazon
    ld c, #4
    ld b, #9
    call cpct_drawSprite_asm
    ld a, e_lifes(ix)

    cp #2
    jr c, no_lifes
    cpctm_screenPtr_asm de, #0xC000, #06, #00
    ld hl, #_sprite_corazon
    ld c, #4
    ld b, #9
    call cpct_drawSprite_asm
    ld a, e_lifes(ix)

    cp #3
    jr c, no_lifes
    cpctm_screenPtr_asm de, #0xC000, #10, #00
    ld hl, #_sprite_corazon
    ld c, #4
    ld b, #9
    call cpct_drawSprite_asm
    ld a, e_lifes(ix)

    cp #4
    jr c, no_lifes
    cpctm_screenPtr_asm de, #0xC000, #14, #00
    ld hl, #_sprite_corazon
    ld c, #4
    ld b, #9
    call cpct_drawSprite_asm
    ld a, e_lifes(ix)

    cp #5
    jr c, no_lifes
    cpctm_screenPtr_asm de, #0xC000, #18, #00
    ld hl, #_sprite_corazon
    ld c, #4
    ld b, #9
    call cpct_drawSprite_asm
    ld a, e_lifes(ix)

    cp #6
    jr c, no_lifes
    cpctm_screenPtr_asm de, #0xC000, #22, #00
    ld hl, #_sprite_corazon
    ld c, #4
    ld b, #9
    call cpct_drawSprite_asm

    no_lifes:
ret

sys_ui_erase_heart::
    ld l, #1 ; que sonido va a sonar
    ld h, #15 ; volumen 0-15
    ld e, #30 ; nota 0-143
    ld d, #0 ; speed (0 es original, 1 es la mas rapida)
    ld bc, #0xFFFF ;pitch
    ld a, #10 ;canal del medio
    push ix
    call cpct_akp_SFXPlay_asm
    pop ix

    ld a, e_lifes(ix)
    
    cp #5
    jr z, delete_heart_6
    cp #4
    jr z, delete_heart_5
    cp #3
    jr z, delete_heart_4
    cp #2
    jr z, delete_heart_3
    cp #1
    jr z, delete_heart_2
    cp #0
    jr z, delete_heart_1
    ret
    delete_heart_6:
        cpctm_screenPtr_asm de, #0xC000, #22, #00

        ld c, #4
        ld b, #9

        xor a
        call cpct_drawSolidBox_asm
        ret
    delete_heart_5:
        cpctm_screenPtr_asm de, #0xC000, #18, #00


        ld c, #4
        ld b, #9

        xor a
        call cpct_drawSolidBox_asm
        ret
    delete_heart_4:
        cpctm_screenPtr_asm de, #0xC000, #14, #00

        ld c, #4
        ld b, #9

        xor a
        call cpct_drawSolidBox_asm
        ret
    delete_heart_3:
        cpctm_screenPtr_asm de, #0xC000, #10, #00

        ld c, #4
        ld b, #9

        xor a
        call cpct_drawSolidBox_asm
        ret

    delete_heart_2:
        cpctm_screenPtr_asm de, #0xC000, #06, #00

        ld c, #4
        ld b, #9

        xor a
        call cpct_drawSolidBox_asm
        ret
    delete_heart_1:
        cpctm_screenPtr_asm de, #0xC000, #02, #00

        ld c, #4
        ld b, #9

        xor a
        call cpct_drawSolidBox_asm
        ret
ret

sys_ui_add_round::
    call get_round
    cp #10
    jp m, not_render_decenas
    call sys_ui_update_rounds_decenas
    not_render_decenas:
    call sys_ui_update_rounds_unidades
ret

sys_ui_food_damaged::
    ld hl, #food_life
    dec (hl)
    call z, sys_ui_food_die
ret

sys_ui_food_increase::
    ld hl, #food_life
    ld a, (hl)
    cp #16
    jr z, ended_increase
    inc (hl)
    ld a, (hl)
    cp #16
    jr z, ended_increase
    inc (hl)
    ld a, (hl)
    cp #16
    jr z, ended_increase
    inc (hl)

    ended_increase:
ret

;;AQUI DIBUJAREMOS SOBRE LA BARRA DE VIDA DISMINUYENDO

sys_ui_draw_food_counter::
    ;cpctm_screenPtr_asm de, #0xC000, #32, #89
    ld de, #0xCB90
    ld c, #16
    ld b, #3
    ld a, #0x30
    call cpct_drawSolidBox_asm

    ld a, (food_life)
    and a
    jr z, no_comida
        ;cpctm_screenPtr_asm de, #0xC000, #32, #89
        ld de, #0xCB90

        ld a, (food_life)
        ld c, a
        ld b, #3

        ld a, #0x33
        call cpct_drawSolidBox_asm

    ;dibuja barra verde
    no_comida:
ret

;;AQUI LA VIDA DE LA COMIDA ES 0
sys_ui_food_die::
    xor a
    call man_entity_get_from_idx

    call man_entity_set4destruction
ret
