;; This file is part of Moonsters
;;  Copyright (C) 2021 Dam Studios
;;
;;     Moonsters is free software: you can redistribute it and/or modify
;;     it under the terms of the GNU General Public License as published by
;;     the Free Software Foundation, either version 3 of the License, or
;;     (at your option) any later version.
;; 
;;     SHOOT TO MOVE is distributed in the hope that it will be useful,
;;     but WITHOUT ANY WARRANTY; without even the implied warranty of
;;     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;     GNU General Public License for more details.
;; 
;;     You should have received a copy of the GNU General Public License
;;     along with Moonsters  If not, see <https://www.gnu.org/licenses/>.


.include "sys/sys_ai.h.s"
.include "man/entity_manager.h.s"
.include "assets/assets.h.s"

sys_ai_init::

	ret


DefineEntity bala_enemigo , e_type_bullet_enemy,e_render_visible, 30, 12, 2, 1, 0, 0, _sp_alien, e_ai_noAI, 0

sys_ai_update::

	;;update speed controller
	ld a, (velocidad_controller)
	cp #4
	jr z, put_2_0
	inc a
	ld (velocidad_controller), a
	jr end_speed_controller
put_2_0:
	ld a, #0
	ld (velocidad_controller), a
end_speed_controller:


	call man_entity_getArray
	ld (_ent_counter), a

loopAI:

	ld a, e_ai_st(ix)
	cp #e_ai_noAI
	jr z, _no_AI_ent

_AI_ent:

	cp #e_ai_stand_by
	call z, sys_ai_stand_by
	cp #e_ai_move_to
	call z, sys_ai_move_to


_no_AI_ent:


_ent_counter = . + 1
	ld a, #0
	dec a
	ret z

	ld (_ent_counter), a

	ld de, #entity_size
	add ix, de


	jr loopAI


sys_ai_move_to::

	ld a, e_ai_aim_x(ix) 
	ld b, e_x(ix)
	cp b
	jr z, objx_end

	ld a, e_ai_aim_x(ix)  ;; A = x_objetivo
	sub e_x(ix)            ;; a = x_obj - x
	jr nc, objx_greater

objx_not_greater:
	ld e_vx(ix), #-1
	jr objx_end
objx_greater:
	ld e_vx(ix), #1
objx_end:


;;check if finished
	ld a, e_x(ix)
	ld b, e_ai_aim_x(ix)
	cp b
	jr nz, notFinished

	

	ld b, e_ai_dist(ix)
	ld a, #0
	sub a, b
	ld e_ai_dist(ix), a

	ld e_ai_st(ix), #e_ai_stand_by

	ld iy, #bala_enemigo
	ld a, e_x(ix)
	ld e_x(iy), a
	ld a, e_y(ix)
	add #10
	ld e_y(iy), a
	ld a, e_vx(ix)
	ld e_vx(iy), a
	ld hl, #bala_enemigo
   	call man_entity_create

notFinished:

   	;;COMPROBAMOS VELOCIDAD
   	ld a, (velocidad_controller)
   	cp #3
   	jr z, move

   	ld a, #0
   	ld e_vx(ix), a

move:
	ret

velocidad_controller:
	.db 0x00


sys_ai_stand_by::

	;ld a, #3 ;;objetivo
	ld a, #1 ;;aqui tendria que comprobar cuando moverse
	or a
	ret z

	;;Move to activated
	ld a, e_x(ix)
	ld b, e_ai_dist(ix)
	add a, b
	ld e_ai_aim_x(ix), a


	;ld a, e_y(ix) 
	;ld e_ai_aim_y(ix), a
	;;objetivo puesto
	ld e_ai_st(ix), #e_ai_move_to

	ret