Example #1
0
    public void Init()
    {
        UICanvas = new GameObject("UICanvas",
                                  typeof(RectTransform),
                                  typeof(UIBehavior))
        {
            tag = "UICanvas"
        };
        InitHealthBar();
        InitEquipmentPanels();
        _player = new Player();
        camera  = new GameObject("Main Camera",
                                 typeof(Camera),
                                 typeof(CameraFollow))
        {
            tag = "MainCamera"
        };
        camera.GetComponent <Camera>().enabled = true;
        invPanel = new GameObject("Inventory Panel",
                                  typeof(InventoryPanel),
                                  typeof(GridLayoutGroup))
        {
            tag = "InventoryPanel"
        };
        invPanel.GetComponent <InventoryPanel>().entity = _player;

        UICanvas.GetComponent <UIBehavior>().inventoryPanel = invPanel;

        player = new GameObject("Player", typeof(EntityHealthManager));
        anim   = player.AddComponent <Animator>();
        playerHealthManager           = player.GetComponent <EntityHealthManager>();
        playerObject                  = new PlayerObject(player, 100.0f);
        playerHealthManager.entityObj = playerObject;
    }
Example #2
0
    /// <summary>
    /// Checks whether the player can attack a monster
    /// </summary>
    /// <param name="_player">The Player's GameObject</param>
    /// <returns>true if the clicked on monster is within the hotbar's selected item's attack range</returns>
    public override bool Validate(GameObject _player)
    {
        Camera              main          = Camera.main;
        bool                attack        = Input.GetButtonDown("Fire1");
        Vector3             mousePosition = Input.mousePosition;
        EntitySpriteManager em;
        Player              player;
        EquipmentManager    equips;

        em     = _player.GetComponent <EntitySpriteManager>();
        player = (Player)em.entity;
        equips = (player.entityObject as PlayerObject).EquipmentManager;

        if (attack)
        {
            Vector2 origin            = _player.transform.position;
            Vector2 worldPointClicked = main.ScreenToWorldPoint(mousePosition);
            Vector2 direction         = (worldPointClicked - origin);
            direction.Normalize();
            float      attackRange = 1f; // melee range (no weapon)
            UsableItem selected    = equips.GetSelectedItem();
            WeaponItem weapon      = selected is WeaponItem ?
                                     (WeaponItem)selected : null;

            // check if hotbar is a weapon and update the attack range
            if (weapon != null && weapon.SetYet())
            {
                attackRange = Mathf.Max((float)weapon.AttackRange, attackRange);
                Debug.Log("Attack Range: " + attackRange);
            }

            RaycastHit2D hit2D = Physics2D.Raycast(origin, direction, attackRange, 1 << LayerMask.NameToLayer("Enemy"));
            Debug.DrawLine(origin, worldPointClicked, Color.green, 0.5f);
            if (hit2D.collider != null)
            {
                GameObject target = hit2D.collider.gameObject;
                em = target.GetComponent <EntitySpriteManager>();
                Entity ent = em != null ? em.entity : null;
                if (ent != null && ent is Monster)
                {
                    this.weapon          = weapon;
                    this.target          = ent;
                    this.targetHM        = target.GetComponent <EntityHealthManager>();
                    this.targetTransform = target.transform;
                    return(true);
                }
            }
        }
        return(false);
    }
Example #3
0
    /// <summary>
    /// Performs a melee attack.
    /// </summary>
    /// <param name="obj">The GameObject that wants to execute this action.</param>
    /// <returns>Returns true if this action is executed successfully, false otherwise.</returns>
    public override bool Execute(GameObject obj)
    {
        base.Execute(obj);
        GameObject player = GameObject.FindGameObjectWithTag("Player");

        if (player == null)
        {
            return(false);
        }

        EntityHealthManager healthManager = player.GetComponent <EntityHealthManager>();

        if (healthManager == null)
        {
            return(false);
        }

        healthManager.InflictDamage(attackDamage);
        timer.Reset();
        return(true);
    }
Example #4
0
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    //
    //}

    public override void OnStateExit(Animator animator, AnimatorStateInfo animatorStateInfo, int layerIndex)
    {
        EntityHealthManager ehm = animator.gameObject.GetComponent <EntityHealthManager>();

        if (ehm != null)
        {
            EntityObject entityObject = ehm.entityObj;
            if (entityObject == null)
            {
                throw new System.Exception("Dead entity's EntityHealthManager.entityObject field was null!");
            }
            //Drop random loot items if any
            entityObject.DropLootAroundGameObject();
        }
        else
        {
            Debug.Log("WARNING: Dead entity GameObject had no EntityHealthManager component!");
        }
        //Destroy entity's GameObject
        Destroy(animator.gameObject);
    }
Example #5
0
    override public GameObject Spawn(Map map, Vector2 location)
    {
        GameObject player = base.Spawn(map, location);

        player.tag   = "Player";
        player.layer = LayerMask.NameToLayer("Player");

        Animator anim = player.AddComponent <Animator>();

        anim.runtimeAnimatorController = animatorController;
        MovementAnimation moveAnim = player.AddComponent <MovementAnimation>();

        AttackAnimationObject = CreateAttackAnimGameObject();
        AttackAnimationObject.transform.SetParent(player.transform);
        AttackAnimationObject.transform.localPosition = Vector3.zero;

        EntityActionManager actionManager = player.AddComponent <EntityActionManager>();

        actionManager.entity = this;
        PlayerObject entityObj = new PlayerObject(player, maxHealth);

        this.entityObject       = entityObj;
        entityObj.entityDropGen = entityDropGen;
        EntityHealthManager healthManagerTest = player.GetComponent <EntityHealthManager>();

        if (healthManagerTest == null)
        {
            EntityHealthManager healthManager = player.AddComponent <EntityHealthManager>();
            healthManager.entityObj = entityObj;
        }
        else
        {
            healthManagerTest.entityObj = this.entityObject;
        }
        player.AddComponent <EntityObjectManager>().entityObject = entityObj;


        return(player);
    }