Exemplo n.º 1
0
        public Vector3FP YRotate(FixPoint radian)
        {
            FixPoint sin_v = FixPoint.Sin(radian);
            FixPoint cos_v = FixPoint.Cos(radian);

            return(new Vector3FP(cos_v * x - sin_v * z, y, sin_v * x + cos_v * z));
        }
        public List <PositionComponent> CollectEntity_ForwardSector(Vector3FP position, Vector2FP facing, FixPoint radius, FixPoint degree, int exclude_id)
        {
            FixPoint cos = FixPoint.Cos(FixPoint.Degree2Radian(degree >> 1));

            m_collection.Clear();
            Vector2FP         source = new Vector2FP(position.x, position.z);
            Vector2FP         target = new Vector2FP();
            PositionComponent cmp;

            for (int i = 0; i < m_entities.Count; ++i)
            {
                cmp = m_entities[i];
                if (cmp.GetOwnerEntityID() == exclude_id)
                {
                    continue;
                }
                target.x = cmp.CurrentPosition.x;
                target.z = cmp.CurrentPosition.z;
                Vector2FP to_target = target - source;
                FixPoint  distance  = to_target.FastNormalize();
                if (distance > radius + cmp.Radius)
                {
                    continue;
                }
                if (to_target.Dot(ref facing) < cos)
                {
                    continue;
                }
                m_collection.Add(cmp);
            }
            return(m_collection);
        }
Exemplo n.º 3
0
        public Vector3FP GetAimingLineFP()
        {
            float angle = 0;

            if (m_is_aiming_line_state)
            {
                if (m_aiming_line_trans == null)
                {
                    return(Vector3FP.Zero);
                }
                angle = m_aiming_line_trans.eulerAngles.y - 90;
            }
            else
            {
                if (m_rotate_turret_trans == null)
                {
                    return(Vector3FP.Zero);
                }
                angle = m_rotate_angle;
            }
            if (angle < 0)
            {
                angle = 360 + angle;
            }
            FixPoint radian = FixPoint.Degree2Radian(-FixPoint.CreateFromFloat(angle));

            return(new Vector3FP(FixPoint.Cos(radian), FixPoint.Zero, FixPoint.Sin(radian)));
        }
Exemplo n.º 4
0
        public static bool FsstInsideFov(ref Vector2FP source, ref Vector2FP facing, FixPoint fov_degree, ref Vector2FP target)
        {
            Vector2FP to_target = target - source;

            to_target.FastNormalize();
            return(to_target.Dot(ref facing) >= FixPoint.Cos(FixPoint.Degree2Radian(fov_degree / FixPoint.Two)));
        }
Exemplo n.º 5
0
        public void FromDegree(FixPoint degree)
        {
            FixPoint radian = FixPoint.Degree2Radian(-degree);

            x = FixPoint.Cos(radian);
            z = FixPoint.Sin(radian);
        }
Exemplo n.º 6
0
        public static bool InsideFov(ref Vector2FP source, ref Vector2FP facing, FixPoint fov_degree, ref Vector2FP target)
        {
            // 判断target是否在以source为中心的朝向为facing的角度范围fov_deg里面
            Vector2FP to_target = target - source;

            to_target.Normalize();
            return(to_target.Dot(ref facing) >= FixPoint.Cos(FixPoint.Degree2Radian(fov_degree / FixPoint.Two)));
        }
Exemplo n.º 7
0
        public static bool FastInsideFov2D(FixPoint source_d1, FixPoint source_d2, FixPoint facing_d1, FixPoint facing_d2, FixPoint fov_degree, FixPoint target_d1, FixPoint target_d2)
        {
            FixPoint to_target_d1 = target_d1 - source_d1;
            FixPoint to_target_d2 = target_d2 - source_d2;
            FixPoint distance     = FixPoint.FastDistance(to_target_d1, to_target_d2);

            to_target_d1 /= distance;
            to_target_d2 /= distance;
            return(to_target_d1 * facing_d1 + to_target_d2 * facing_d2 >= FixPoint.Cos(FixPoint.Degree2Radian(fov_degree / FixPoint.Two)));
        }
Exemplo n.º 8
0
        public List <PositionComponent> CollectEntity_ForwardSector(Vector3FP position, Vector2FP facing, FixPoint radius, FixPoint degree, int exclude_id)
        {
            FixPoint cos = FixPoint.Cos(FixPoint.Degree2Radian(degree >> 1));

            m_collection.Clear();
            Vector2FP start_position = new Vector2FP(position.x - radius, position.z - radius);
            Vector2FP end_position   = new Vector2FP(position.x + radius, position.z + radius);

            ComputeAreaXZ(start_position, end_position);
            Cell              cell;
            Vector2FP         source = new Vector2FP(position.x, position.z);
            Vector2FP         target = new Vector2FP();
            PositionComponent cmp;

            for (int x = m_min_x; x <= m_max_x; ++x)
            {
                for (int z = m_min_z; z <= m_max_z; ++z)
                {
                    cell = m_cells[x, z];
                    for (int i = 0; i < cell.m_entities.Count; ++i)
                    {
                        cmp = cell.m_entities[i];
                        if (cmp.GetOwnerEntityID() == exclude_id)
                        {
                            continue;
                        }
                        target.x = cmp.CurrentPosition.x;
                        target.z = cmp.CurrentPosition.z;
                        Vector2FP to_target = target - source;
                        FixPoint  distance  = to_target.FastNormalize();
                        if (distance > radius + cmp.Radius)
                        {
                            continue;
                        }
                        if (to_target.Dot(ref facing) < cos)
                        {
                            continue;
                        }
                        m_collection.Add(cmp);
                    }
                }
            }
            return(m_collection);
        }
Exemplo n.º 9
0
    public void TestCos()
    {
        m_max_precision_times = 0m;
        m_max_delta_percent   = 0m;
        Assert.True(FixPoint.Cos(FixPoint.Zero) == FixPoint.One);
        Assert.True(FixPoint.Cos(FixPoint.HalfPi) == FixPoint.Zero);
        Assert.True(FixPoint.Cos(FixPoint.Pi) == -FixPoint.One);
        Assert.True(FixPoint.Cos(FixPoint.Pi + FixPoint.HalfPi) == FixPoint.Zero);
        Assert.True(FixPoint.Cos(FixPoint.TwoPi) == FixPoint.One);
        Assert.True(FixPoint.Cos(-FixPoint.HalfPi) == -FixPoint.Zero);
        Assert.True(FixPoint.Cos(-FixPoint.Pi) == -FixPoint.One);
        Assert.True(FixPoint.Cos(-FixPoint.Pi - FixPoint.HalfPi) == FixPoint.Zero);
        Assert.True(FixPoint.Cos(-FixPoint.TwoPi) == FixPoint.One);

        for (double angle = -2 * Math.PI; angle <= 2 * Math.PI; angle += 0.0001)
        {
            double   expected = Math.Cos(angle);
            FixPoint fp       = (FixPoint)angle;
            FixPoint result   = FixPoint.Cos(fp);

            decimal delta           = (decimal)Math.Abs(expected - (double)result);
            decimal precision_times = delta / FixPoint.Precision;
            decimal delta_percent   = delta * 100.0m / 1;
            if (delta_percent > 40)
            {
                delta_percent -= 1;
            }
            if (precision_times > m_max_precision_times)
            {
                m_max_precision_times = precision_times;
            }
            if (delta_percent > m_max_delta_percent)
            {
                m_max_delta_percent = delta_percent;
            }
        }

        Console.WriteLine("TestCos() m_max_precision_times = " + m_max_precision_times + ", m_max_delta_percent = " + m_max_delta_percent);
    }
Exemplo n.º 10
0
        public static Entity CreateEntityForSkillAndEffect(Component caller_component, Entity owner_entity, Target projectile_target, Vector3FP position_offset, FixPoint angle_offset, int object_type_id, int object_proto_id, FixPoint object_life_time, EffectGenerator attached_generator)
        {
            LogicWorld      logic_world = owner_entity.GetLogicWorld();
            IConfigProvider config      = logic_world.GetConfigProvider();
            ObjectTypeData  type_data   = config.GetObjectTypeData(object_type_id);

            if (type_data == null)
            {
                return(null);
            }

            PositionComponent owner_position_cmp = owner_entity.GetComponent(PositionComponent.ID) as PositionComponent;
            Vector3FP         source_pos         = owner_position_cmp.CurrentPosition;

            Vector2FP xz_facing;
            FixPoint  angle;
            Vector3FP facing;

            if (projectile_target == null)
            {
                xz_facing = owner_position_cmp.Facing2D;
                angle     = owner_position_cmp.FacingAngle;
                facing.x  = xz_facing.x;
                facing.y  = FixPoint.Zero;
                facing.z  = xz_facing.z;
            }
            else
            {
                Vector3FP target_pos = projectile_target.GetPosition(logic_world);
                xz_facing.x = target_pos.x - source_pos.x;
                xz_facing.z = target_pos.z - source_pos.z;
                xz_facing.Normalize();
                angle  = xz_facing.ToDegree();
                facing = target_pos - source_pos;
                facing.Normalize();
            }
            Vector2FP side      = xz_facing.Perpendicular();
            Vector2FP xz_offset = xz_facing * position_offset.z + side * position_offset.x;

            if (angle_offset != FixPoint.Zero)
            {
                angle += angle_offset;
                FixPoint radian = FixPoint.Degree2Radian(-angle);
                facing.x = FixPoint.Cos(radian);
                facing.z = FixPoint.Sin(radian);
            }

            Vector3FP         birth_position = new Vector3FP(source_pos.x + xz_offset.x, source_pos.y + position_offset.y, source_pos.z + xz_offset.z);
            BirthPositionInfo birth_info     = new BirthPositionInfo(birth_position.x, birth_position.y, birth_position.z, angle, owner_position_cmp.GetCurrentSceneSpace());

            ProjectileComponent owner_entity_projectile_component = owner_entity.GetComponent(ProjectileComponent.ID) as ProjectileComponent;

            if (owner_entity_projectile_component != null)
            {
                Entity original_owner = logic_world.GetEntityManager().GetObject(owner_entity_projectile_component.SourceEntityID);
                if (original_owner != null)
                {
                    owner_entity = original_owner;
                }
            }

            Player owner_player = owner_entity.GetOwnerPlayer();
            ObjectCreationContext object_context = new ObjectCreationContext();

            object_context.m_object_proxy_id = owner_player.ProxyID;
            object_context.m_object_type_id  = object_type_id;
            object_context.m_object_proto_id = object_proto_id;
            object_context.m_birth_info      = birth_info;
            object_context.m_type_data       = type_data;
            object_context.m_proto_data      = config.GetObjectProtoData(object_proto_id);
            object_context.m_logic_world     = logic_world;
            object_context.m_owner_id        = owner_player.ID;
            object_context.m_is_ai           = true;
            object_context.m_is_local        = owner_player.IsLocal;

            Entity created_entity = logic_world.GetEntityManager().CreateObject(object_context);

            DeathComponent death_component = created_entity.GetComponent(DeathComponent.ID) as DeathComponent;

            if (death_component != null && object_life_time > FixPoint.Zero)
            {
                death_component.SetLifeTime(object_life_time);
            }

            SummonedEntityComponent summoned_component = created_entity.GetComponent(SummonedEntityComponent.ID) as SummonedEntityComponent;

            if (summoned_component != null)
            {
                summoned_component.SetMaster(owner_entity);
            }

            ProjectileComponent projectile_component = created_entity.GetComponent(ProjectileComponent.ID) as ProjectileComponent;

            if (projectile_component != null)
            {
                ProjectileParameters param = RecyclableObject.Create <ProjectileParameters>();
                param.m_start_time       = logic_world.GetCurrentTime();
                param.m_life_time        = object_life_time;
                param.m_source_entity_id = owner_entity.ID;
                param.m_start_position   = birth_position;
                param.m_fixed_facing     = facing;
                if (projectile_target != null)
                {
                    param.m_target_entity_id = projectile_target.GetEntityID();
                    param.m_target_position  = projectile_target.GetPosition(logic_world);
                }
                else
                {
                    Skill          owner_skill     = null;
                    SkillComponent skill_componnet = caller_component as SkillComponent;
                    if (skill_componnet != null)
                    {
                        owner_skill = skill_componnet.GetOwnerSkill();
                    }
                    if (owner_skill != null && owner_skill.GetDefinitionComponent().ExternalDataType == SkillDefinitionComponent.NeedExternalTarget)
                    {
                        param.m_target_entity_id = 0;
                        FixPoint range = owner_skill.GetDefinitionComponent().MaxRange;
                        if (range <= 0)
                        {
                            range = FixPoint.Ten;  //ZZWTODO
                        }
                        if (projectile_component.Speed > FixPoint.Zero)
                        {
                            param.m_life_time = range / projectile_component.Speed;
                        }
                        param.m_target_position = param.m_start_position + param.m_fixed_facing * range;
                    }
                }
                param.m_generator_id = attached_generator == null ? 0 : attached_generator.ID;
                projectile_component.InitParam(param);
            }
            else if (attached_generator != null)
            {
                EffectApplicationData app_data = RecyclableObject.Create <EffectApplicationData>();
                app_data.m_original_entity_id = owner_entity.ID;
                app_data.m_source_entity_id   = owner_entity.ID;
                attached_generator.Activate(app_data, created_entity);
                RecyclableObject.Recycle(app_data);
            }
            return(created_entity);
        }
Exemplo n.º 11
0
        public virtual FixPoint Evaluate(IExpressionVariableProvider variable_provider)
        {
            Stack <FixPoint> stack = new Stack <FixPoint>();
            FixPoint         var1, var2, var3;
            int index = 0;
            int total_count = m_instructions.Count;

            while (index < total_count)
            {
                OperationCode op_code = (OperationCode)m_instructions[index];
                ++index;
                switch (op_code)
                {
                case OperationCode.PUSH_NUMBER:
                    stack.Push(FixPoint.CreateFromRaw(m_instructions[index]));
                    ++index;
                    break;

                case OperationCode.PUSH_VARIABLE:
                    if (variable_provider != null)
                    {
                        var1 = variable_provider.GetVariable(m_variables[(int)m_instructions[index]], 0);
                        stack.Push(var1);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    ++index;
                    break;

                case OperationCode.NEGATE:
                    var1 = stack.Pop();
                    stack.Push(-var1);
                    break;

                case OperationCode.ADD:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 + var2);
                    break;

                case OperationCode.SUBTRACT:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 - var2);
                    break;

                case OperationCode.MULTIPLY:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 * var2);
                    break;

                case OperationCode.DIVIDE:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 / var2);
                    break;

                case OperationCode.SIN:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Sin(var1));
                    break;

                case OperationCode.COS:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Cos(var1));
                    break;

                case OperationCode.TAN:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Tan(var1));
                    break;

                case OperationCode.SQRT:
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Sqrt(var1));
                    break;

                case OperationCode.MIN:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Min(var1, var2));
                    break;

                case OperationCode.MAX:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Max(var1, var2));
                    break;

                case OperationCode.CLAMP:
                    var3 = stack.Pop();
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(FixPoint.Clamp(var1, var2, var3));
                    break;

                case OperationCode.EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 == var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.NOT_EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.GREATER:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 > var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.GREATER_EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 >= var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.LESS:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 < var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.LESS_EQUAL:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 <= var2)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.AND:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != FixPoint.Zero && var2 != FixPoint.Zero)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.OR:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != FixPoint.Zero || var2 != FixPoint.Zero)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.NOT:
                    var1 = stack.Pop();
                    if (var1 == FixPoint.Zero)
                    {
                        stack.Push(FixPoint.One);
                    }
                    else
                    {
                        stack.Push(FixPoint.Zero);
                    }
                    break;

                case OperationCode.AND_BITWISE:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 & var2);
                    break;

                case OperationCode.OR_BITWISE:
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    stack.Push(var1 | var2);
                    break;

                case OperationCode.CONDITIONAL_EXPRESSION:
                    var3 = stack.Pop();
                    var2 = stack.Pop();
                    var1 = stack.Pop();
                    if (var1 != FixPoint.Zero)
                    {
                        stack.Push(var2);
                    }
                    else
                    {
                        stack.Push(var3);
                    }
                    break;

                default:
                    break;
                }
            }
            return(stack.Pop());
        }