예제 #1
0
        protected override void process(Entity entity)
        {
            MeleeAction      action   = (MeleeAction)m_MeleeActionMapper.get(entity);
            Position         position = (Position)m_PositionMapper.get(entity);
            SpatialPartition spatial  = (SpatialPartition)m_SpatialMapper.get(m_Spatial);

            action.ElapsedTime += ecs_instance.ElapsedTime;

            //is it time for the melee action to die?
            if (action.ElapsedTime >= action.Lifetime)
            {
                ecs_instance.delete_entity(entity);
                return;
            }

            //retrieve all local entities
            //List<Entity> locals = spatial.QuadTree.retrieveContentsAtLocation(position.Pos);
            List <Entity> locals = spatial.QuadTree.findAllWithinRange(position.Pos, action.Range);

            //is the location good?
            if (locals != null)
            {
                //is there anyone here?
                if (locals.Count > 0)
                {
                    for (int i = 0; i < locals.Count; i++)
                    {
                        //dont attempt to melee owner
                        if (locals[i] == action.Owner)
                        {
                            continue;
                        }

                        if (action.HitByAction.Contains(locals[i]))
                        {
                            continue;
                        }

                        Life life = (Life)m_LifeMapper.get(locals[i]);

                        //if no life, dont bother
                        if (life == null)
                        {
                            continue;
                        }

                        //if not alive, dont bother
                        if (!life.IsAlive)
                        {
                            continue;
                        }

                        //interaction available?
                        Interactable interactions = (Interactable)m_InteractionMapper.get(locals[i]);
                        if (interactions != null)
                        {
                            Position lPosition = (Position)m_PositionMapper.get(locals[i]);

                            Position oPosition = (Position)m_PositionMapper.get(action.Owner);
                            Vector2  lToO      = (oPosition.Pos + oPosition.Offset) - (lPosition.Pos + lPosition.Offset); //local to owner
                            Heading  head      = (Heading)m_HeadingMapper.get(entity);                                    //get the weapon heading
                            bool     facing    = (Vector2.Dot(head.getHeading(), lToO) < 0);                              //is the weapon facing the local?

                            //if you're facing, are you within range?
                            if (facing && (Vector2.Distance(lPosition.Pos + lPosition.Offset, position.Pos + position.Offset) <= action.Range))
                            {
                                //does it support this interaction?
                                if (interactions.SupportedInteractions.MELEE_ACTIONABLE &&
                                    interactions.SupportedInteractions.ATTACKABLE)
                                {
                                    Factions lfactions = (Factions)m_FactionMapper.get(locals[i]);
                                    Factions pfactions = (Factions)m_FactionMapper.get(action.Owner);

                                    //dont attack allies
                                    if (lfactions.OwnerFaction.FactionType == pfactions.OwnerFaction.FactionType)
                                    {
                                        continue;
                                    }

                                    //add to hit-list so we dont attack it again on swing follow-through
                                    action.HitByAction.Add(locals[i]);

                                    //create melee attack
                                    UtilFactory.createAttack(action.Owner, locals[i], AttackType.Melee);

                                    //destroy melee action
                                    //ecs_instance.delete_entity(entity);
                                    //return;
                                }
                            }
                        }
                    }
                }
            }

            //get info for rotation update
            Heading   heading   = (Heading)m_HeadingMapper.get(entity);
            Transform transform = (Transform)m_TransformMapper.get(entity);

            //rotate melee by degrees over the melee arc
            float rot = (((float)action.Animation.updateFrame(ecs_instance.ElapsedTime) / (float)action.Animation.Frames) * action.ArcDegrees) - (action.ArcDegrees / 2f);

            transform.Rotation = rot * (((float)Math.PI) / 180f) - VectorHelper.getAngle(new Vector2(1, 0), heading.getHeading());

            //adjust the arc based on current position (i.e., move with the player)
            Position ownerPos = (Position)m_PositionMapper.get(action.Owner);
            Vector2  pos      = ownerPos.Pos + new Vector2(16, 0);// +ownerPos.getOffset();
            Vector2  dir      = heading.getHeading();

            dir.Normalize();
            position.Pos = pos + dir * 10;
        }