;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program 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 Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; PUBLIC ENTITY VARIABLES
;;====================================================================
sizeof_e            = 35        				;;How much memory takes one entity
e_total_size        = sizeof_e * max_entites   	;;Total size of the entities
max_entites         = 8         				;;How many entities can exist

;;====================================================================
;; TYPES FLAGS
;;====================================================================
e_type_invalid 			= 0x00
e_type_player1  		= 0x01
e_type_player2  		= 0x02
e_type_bullet			= 0x04
e_type_platform 		= 0x08
e_type_enemy			= 0x10
e_type_dying_animation 	= 0x20
e_type_dead    			= 0x80

;;====================================================================
;; COMPONENTS FLAGS
;;====================================================================
e_cmp_render				= 0x01
e_cmp_input					= 0x02
e_cmp_movable				= 0x04
e_cmp_collider				= 0x08
e_cmp_animation 			= 0x10
e_cmp_ai					= 0x20
e_cmp_platform 				= 0x40
e_cmp_gravity				= 0x80

;;====================================================================
;; ENTITY STRUCT REFERENCES USEFUL FOR IX
;;====================================================================
e_type          	= 0
e_cmp           	= 1 
e_pos_x         	= 2 
e_pos_y         	= 3 
e_width         	= 4 
e_height        	= 5 
e_vel_x         	= 6
e_vel_y         	= 7 
e_sprite_b1     	= 8
e_sprite_b2     	= 9 
e_anim_b1			= 10
e_anim_b2			= 11 
e_anim_counter  	= 12 
e_prevptr_b1		= 13
e_prevptr_b2		= 14
e_facing_right  	= 15 
e_input_bhv_b1		= 16
e_input_bhv_b2		= 17
e_physics_bhv_b1    = 18
e_physics_bhv_b2    = 19
e_collision_bhv_b1	= 20
e_collision_bhv_b2	= 21
e_ai_bhv_b1			= 22
e_ai_bhv_b2			= 23 
e_move_to_pos_x 	= 24 
e_move_to_pos_y 	= 25
e_ai_patrol_step_l 	= 26
e_ai_patrol_step_h 	= 27 
e_bullet_ref_b1 	= 28
e_bullet_ref_b2		= 29
e_last_anim_state   = 30
e_last_facing_right = 31
e_is_grounded		= 32
e_last_position_y	= 33
e_was_jump_pressed  = 34

;;====================================================================
;; MACROS
;;====================================================================

.macro DEFINE_ENTITY_TEMPLATE _name, _type, _cmp, _pos_x, _pos_y, _width, _height, _vel_x, _vel_y, _sprite, _anim, _anim_counter, _prevptr, _facing_right, _input_bhv, _physics_bhv, _collision_bhv, _ai_bhv, _move_to_pos_x, _move_to_pos_y, _ai_patrol_step, _bullet_ref, _last_anim_state, _last_facing_right, _is_grounded, _last_position_y, _was_jump_pressed
_name:
	.db _type           	; Entity's types
	.db _cmp            	; Entity's components
	.db _pos_x          	; Entity's x position
	.db _pos_y          	; Entity's y position
	.db _width          	; Entity's sprite width
	.db _height         	; Entity's sprite height
	.db _vel_x          	; Entity's velocity in the x axis
	.db _vel_y          	; Entity's velocity in the y axis
	.dw _sprite         	; Entity's memory direction for the current sprite
	.dw _anim           	; Entity's memory direction for the current animation
	.db _anim_counter   	; To store how much longer is our actual frame of the animation
	.dw _prevptr        	; Previous position (memory direction) of the entity in the screen. Useful for render stuff
	.db _facing_right   	; Wheter the entity is facing right or not
	.dw _input_bhv      	; In case we have the input cmp, the behaviour the entity should execute
	.dw _physics_bhv      	; In case we have the physics cmp, the behaviour the entity should execute
	.dw _collision_bhv		; Behaviour to execute of the collision that should have the entity
	.dw _ai_bhv         	; In case we have the ai cmp, the behaviour the entity should execute
	.db _move_to_pos_x  	; In case we have the ai cmp and its executing the move_to behaviour, where should I move (X)
	.db _move_to_pos_y  	; In case we have the ai cmp and its executing the move_to behaviour, where should I move (Y)
	.dw _ai_patrol_step		; Enemy memory direction that refrences the patrol step
	.dw _bullet_ref 		; In case our player or AI can shoot, the memory direction reference to the bullet
	.db _last_anim_state	; Anim state --> 0 Idle, 1 Run, 2 Jump
	.db _last_facing_right  ; Last facing right (-1 left, 1 right)
	.db _is_grounded		; Value to know if the entity is in the ground or not
	.db _last_position_y	; Value of y to reposition the player after have collided with a platform
	.db _was_jump_pressed	; In case we have the cmp input activate, was the jump key pressed
.endm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should point to the beginning of the entity to be checked 
;;  -_type: SHOULD NOT BE E_TYPE_INVALID cause its 0
;; Objetive: Check if an entity is matching the given type
;;
;; Return: Z = 0 if the entity is matching the type, otherwise Z = 1
;; Modifies: A
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.macro IS_ENTITY_GIVEN_TYPE_IX _type
	ld a, e_type(ix)
	and #_type
.endm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IY: should point to the beginning of the entity to be checked 
;;  -_type: SHOULD NOT BE E_TYPE_INVALID cause its 0
;; Objetive: Check if an entity is matching the given type
;;
;; Return: Z = 0 if the entity is matching the type, otherwise Z = 1
;; Modifies: A
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.macro IS_ENTITY_GIVEN_TYPE_IY _type
	ld a, e_type(iy)
	and #_type
.endm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should point to the beginning of the entity to be checked 
;; Objetive: Check if an entity is invalid
;;
;; Return: Z = 1 if the entity is invalid, otherwise Z = 0
;; Modifies: A
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.macro IS_ENTITY_INVALID _register
	ld a, e_type(_register)
	cp #e_type_invalid
.endm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should point to the beginning of the entity to be checked
;; 	-  C: should contain the signature to match
;; Objetive: Check if an entity is matching the components signature
;;
;; Return: Z = 1 if the entity is matching, otherwise Z = 0
;; Modifies: a
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.macro IS_ENTITY_MATCHING_SIGNATURE
	ld a, e_cmp(ix)
	and c
	cp c

.endm

;;====================================================================
;; PUBLIC INCLUDES
;;====================================================================
.globl man_entity_init
.globl man_entity_create
.globl man_entity_forall_matching
.globl man_entity_update
.globl man_entity_set4destruction
.globl man_entity_set_input_behaviour_not_shooting
.globl man_entity_set_input_behaviour_shooting
.globl man_entity_disable_player_move
.globl man_entity_set_is_grounded
.globl man_entity_set_is_in_air
.globl man_entity_set_type_to_dying_animation

;;Entity patrol ref
.globl man_entity_reset_patrol
.globl man_entity_load_patrol_ref
.globl man_entity_increment_patrol_step

;;Entities array
.globl m_entities

