;;;;;;;;;;;;;-------LICENSE NOTICE-----------;;;;;;;;;;;;;;;
;; MIT License

;; Copyright (c) 2021 DreamyZombies Studio

;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:

;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.

;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;
;; Authors:
;;  Abderrahamane Guermat Benaouali (agb139@alu.ua.es)
;;  Jorge Angulo Espí (jae17@alu.ua.es)
;;  Sami Hadj-Djilani (sh101@alu.ua.es)

;;
;; PLAYER MANAGER
;;

;;         x    y   w   h vx vy orientation(1->left, 2->right)
hero:: .db 0, 0, 16, 8, 0, 0, 2

spawnWorldX = 13
spawnWorldY = 165
spawnLobbyX = 13
spawnLobbyY = 20
spawnWorldCompletedX = 41
spawnWorldCompletedY = 165

.globl _SpriteOrder
.globl _player_last_x
.globl _player_last_y

hero_size == 7
_hero_pos:: .dw hero_size

entityman_init::
    ret

;;   INPUT: 
;;      hl: Pointer to entity
;; DESTROY: DE, BC, A, HL
entityman_create_hero::

    ld  de, (_hero_pos)
    ld  bc, #hero_size
    ldir

    ld  hl, (_hero_pos)
    ld  bc, #hero_size
    add hl, bc

    ret
    
entityman_getHero_pos::

   ld  ix, (_hero_pos)
   
   ret

entityman_player_spawn_lobby::

    xor a
    ld (_SpriteOrder), a

    ld a, #spawnLobbyX
    ld b, #spawnLobbyY
    call entityman_player_spawn

    ret

entityman_player_spawn_world::

    cp a, #2
    jp z, world3spawn

    cp a, #1
    jp z, world2spawn

world1spawn:
    ld a, #spawnWorldX
    ld b, #spawnWorldY
    jp process_spawn

world2spawn:
    ld a, #13
    ld b, #74
    jp process_spawn

world3spawn:
    ld a, #13
    ld b, #40
    jp process_spawn

process_spawn:

    call entityman_player_spawn
    xor a
    ld (_SpriteOrder), a
    ret

entityman_player_spawn_world_completed::

    xor a
    ld (_SpriteOrder), a

    ld a, #spawnWorldCompletedX
    ld b, #spawnWorldCompletedY
    call entityman_player_spawn

    ret

;;   INPUT: 
;;      a: X to set spawn
;;      b: Y to set spawn
;; DESTROY: IX, A
entityman_player_spawn:

    ld (_player_last_x), a
    
    
    ld  ix, (_hero_pos)
    ld 0(ix), a
    ld a, b
    ld (_player_last_y), a
    ld 1(ix), a
    
    ld a, #2
    ld 6(ix), a

    ret
