public void UpdateSpurt(FixPoint delta_time) { Skill skill = GetOwnerSkill(); Entity owner_entity = skill.GetOwnerEntity(); PositionComponent position_component = owner_entity.GetComponent(PositionComponent.ID) as PositionComponent; Vector3FP offset = position_component.Facing3D * (m_distance * delta_time / m_time); if (m_backward) { offset = -offset; } Vector3FP new_position = position_component.CurrentPosition + offset; GridGraph grid_graph = position_component.GetGridGraph(); if (grid_graph != null) { GridNode node = grid_graph.Position2Node(new_position); if (node == null || !node.Walkable) { StopSpurt(); return; } } position_component.CurrentPosition = new_position; if (m_collision_target_generator != null) { DetectCollision(position_component.GetSpacePartition(), new_position, position_component.Radius); } }
public void Construct(IRegionCallback callback, ISpacePartition partition, Vector3FP fixed_position) { m_callback = callback; m_binding_object = null; m_partition = partition; m_fixed_position = fixed_position; }
public bool FindPath(Vector3FP start_pos, Vector3FP end_pos) { m_node_path.Clear(); m_position_path.Clear(); GridNode start_node = Position2NearestNode(start_pos); GridNode end_node = Position2NearestNode(end_pos); if (start_node == null || end_node == null) { return(false); } if (!start_node.Walkable) { start_node = FindNearestWalkableNode(start_node, end_node); if (start_node == null) { return(false); } } if (!end_node.Walkable || end_node.m_area != start_node.m_area) { end_node = FindNearestWalkableNode(end_node, start_node, start_node.m_area); if (end_node == null) { return(false); } } AStarFindPath(start_node, end_node); SmoothPath(start_pos, end_pos); return(true); }
public List <PositionComponent> CollectEntity_SurroundingRing(Vector3FP position, FixPoint outer_radius, FixPoint inner_radius, int exclude_id) { m_collection.Clear(); PositionComponent cmp; for (int i = 0; i < m_entities.Count; ++i) { cmp = m_entities[i]; if (cmp.GetOwnerEntityID() == exclude_id) { continue; } Vector3FP offset = position - cmp.CurrentPosition; FixPoint distance = FixPoint.FastDistance(offset.x, offset.z); if (distance >= (outer_radius + cmp.Radius)) { continue; } if (inner_radius > FixPoint.Zero && distance <= (inner_radius - cmp.Radius)) { continue; } m_collection.Add(cmp); } return(m_collection); }
public PositionComponent GetNearestEnemy(Entity source_entity) { Player source_player = source_entity.GetOwnerPlayer(); PositionComponent source_position_cmp = source_entity.GetComponent(PositionComponent.ID) as PositionComponent; Vector3FP source_position = source_position_cmp.CurrentPosition; List <PositionComponent> list = source_position_cmp.GetSpacePartition().CollectEntity_All(); PositionComponent potential_enemy = null; FixPoint min_distance = FixPoint.MaxValue; for (int i = 0; i < list.Count; ++i) { PositionComponent target_position_component = list[i]; Entity entity = target_position_component.GetOwnerEntity(); if (entity.GetComponent(DamagableComponent.ID) == null) { continue; } if (!FactionRelation.IsFactionSatisfied(source_player.GetFaction(entity.GetOwnerPlayerID()), FactionRelation.Enemy)) { continue; } Vector3FP offset = source_position - target_position_component.CurrentPosition; FixPoint distance = FixPoint.FastDistance(offset.x, offset.z); if (distance < min_distance) { potential_enemy = target_position_component; min_distance = distance; } } return(potential_enemy); }
Cell Position2Cell(Vector3FP position) { int x_index = (int)((position.x - m_left_bottom_position.x) / CELL_SIZE); int z_index = (int)((position.z - m_left_bottom_position.z) / CELL_SIZE); return(GetCell(x_index, z_index)); }
public override void Apply() { if (m_task == null) { m_task = LogicTask.Create <ComponentCommonTaskWithLastingTime>(); } EffectDefinitionComponent definition_component = ((Effect)ParentObject).GetDefinitionComponent(); Entity entity = GetLogicWorld().GetEntityManager().GetObject(definition_component.SourceEntityID); if (entity != null) { PositionComponent position_component = entity.GetComponent(PositionComponent.ID) as PositionComponent; if (position_component != null) { m_direction = position_component.Facing3D; } } m_task.Construct(this, m_time); var schedeler = GetLogicWorld().GetTaskScheduler(); schedeler.Schedule(m_task, GetCurrentTime(), LOGIC_UPDATE_INTERVAL, LOGIC_UPDATE_INTERVAL); #if COMBAT_CLIENT LocomoteRenderMessage msg = RenderMessage.Create <LocomoteRenderMessage>(); msg.ConstructAsStartMoving(GetOwnerEntityID(), true, LocomoteRenderMessage.NotLocomotion); GetLogicWorld().AddRenderMessage(msg); #endif }
protected override void PostInitialize() { m_turn_scheduler = new TaskScheduler <LogicWorld>(this); FixPoint grid_size = FixPoint.One; FixPoint seeker_radius = FixPoint.One / FixPoint.FixPointDigit[4]; FixPoint x_size = new FixPoint(40); FixPoint z_size = new FixPoint(30); Vector3FP left_bottom_position = new Vector3FP(new FixPoint(-20), FixPoint.Zero, new FixPoint(-15)); SquareGridGraph grid_graph = new SquareGridGraph(); //HexagonGridGraph grid_graph = new HexagonGridGraph(); grid_graph.GenerateAsPlaneMap(grid_size, x_size, z_size, FixPoint.Zero, left_bottom_position, seeker_radius); grid_graph.CoverArea(new Vector3FP(FixPoint.Zero, FixPoint.Zero, FixPoint.FixPointDigit[5]), new Vector3FP(FixPoint.FixPointDigit[7] + FixPoint.Half, FixPoint.Zero, FixPoint.Half)); grid_graph.CoverArea(new Vector3FP(FixPoint.FixPointDigit[7] + FixPoint.Half, FixPoint.Zero, FixPoint.Zero), new Vector3FP(FixPoint.Half, FixPoint.Zero, FixPoint.FixPointDigit[5])); grid_graph.CoverArea(new Vector3FP(-FixPoint.FixPointDigit[7] - FixPoint.Half, FixPoint.Zero, FixPoint.Two), new Vector3FP(FixPoint.Half, FixPoint.Zero, FixPoint.FixPointDigit[7] + FixPoint.Half)); CellSpacePartition space_partition = new CellSpacePartition(this, x_size, z_size, left_bottom_position); m_scene_space = new SceneSpace(); m_scene_space.m_space_id = 0; m_scene_space.m_min_position = left_bottom_position; m_scene_space.m_max_position = new Vector3FP(left_bottom_position.x + x_size, FixPoint.Zero, left_bottom_position.z + z_size); m_scene_space.m_graph = grid_graph; m_scene_space.m_paitition = space_partition; }
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); }
public override GridNode Position2Node(Vector3FP position) { int x_index, z_index; Position2NodeIndex(position, out x_index, out z_index); return(GetNode(x_index, z_index)); }
protected override void SmoothPath(Vector3FP start_pos, Vector3FP end_pos) { for (int i = 0; i < m_node_path.Count; ++i) { m_position_path.Add(Node2Position(m_node_path[i])); } }
public void Update(FixPoint delta_time) { if (m_cur_way_point == INVALID_WAYPOINT_INDEX) { return; } if (delta_time > m_remain_time) { Vector3FP new_position = m_position_component.CurrentPosition + m_direction * m_max_speed * m_remain_time; delta_time -= m_remain_time; AdvanceWayPoint(); if (m_cur_way_point != INVALID_WAYPOINT_INDEX) { new_position += m_direction * m_max_speed * delta_time; m_remain_time -= delta_time; } m_position_component.CurrentPosition = new_position; } else { Vector3FP new_position = m_position_component.CurrentPosition + m_direction * m_max_speed * delta_time; m_remain_time -= delta_time; m_position_component.CurrentPosition = new_position; } if (m_target != null) { FixPoint distance = m_position_component.CurrentPosition.FastDistance(m_target.CurrentPosition) - m_position_component.Radius - m_target.Radius; //ZZWTODO 多处距离计算 if (distance < m_range) { m_callback.MovementFinished(); } } }
public override GridNode Position2Node(Vector3FP position) { int x_index = (int)((position.x - m_left_bottom_position.x) / GRID_SIZE); int z_index = (int)((position.z - m_left_bottom_position.z) / GRID_SIZE); return(GetNode(x_index, z_index)); }
public bool IsInRange(Vector3FP position) { if (position.x < m_min_position.x || position.z < m_min_position.z || position.x > m_max_position.x || position.z > m_max_position.z) { return(false); } return(true); }
protected override void SmoothPath(Vector3FP start_pos, Vector3FP end_pos) { if (m_node_path.Count < 0) { return; } m_position_path.Add(start_pos); GridNode start_node = Position2Node(start_pos); GridNode last_output_node = m_node_path[0]; if (start_node != last_output_node) { m_position_path.Add(Node2Position(last_output_node)); } if (m_node_path.Count > 2) { int index = 2; GridNode current_node = m_node_path[index - 1]; GridNode previous_node = m_node_path[index - 2]; int previous_dx = current_node.m_x - previous_node.m_x; int previous_dz = current_node.m_z - previous_node.m_z; bool use_dxdz = true; while (index < m_node_path.Count) { current_node = m_node_path[index]; previous_node = m_node_path[index - 1]; if (use_dxdz && current_node.m_x - previous_node.m_x == previous_dx && current_node.m_z - previous_node.m_z == previous_dz) { ++index; } else if (IsStraightLineWalkable(last_output_node, current_node)) { ++index; use_dxdz = false; } else { last_output_node = previous_node; m_position_path.Add(Node2Position(previous_node)); ++index; previous_dx = m_node_path[index - 1].m_x - m_node_path[index - 2].m_x; previous_dz = m_node_path[index - 1].m_z - m_node_path[index - 2].m_z; use_dxdz = true; } } } GridNode end_node = Position2Node(end_pos); if (end_node != m_node_path[m_node_path.Count - 1]) { m_position_path.Add(Node2Position(m_node_path[m_node_path.Count - 1])); } else { m_position_path.Add(end_pos); } }
public void AddPredictOffset(Vector3 offset) { GridGraph grid_graph = m_position_component.GetGridGraph(); if (grid_graph != null) { Vector3 interpolation_position = m_interpolation_tr.localPosition + offset; Vector3 entity_position = m_model_component.GetCurrentPosition() + interpolation_position; Vector3FP entity_position_fp = RenderWorld.Vector3_To_Vector3FP(entity_position); GridNode node = grid_graph.Position2Node(entity_position_fp); if (node == null) { if (!GetRenderWorld().OnEntityOutOfEdge(GetRenderEntity())) { return; } } else if (!node.Walkable && m_locomotor_component.AvoidObstacle()) { Vector3FP offset_fp = RenderWorld.Vector3_To_Vector3FP(offset); //try z entity_position_fp.x -= offset_fp.x; node = grid_graph.Position2Node(entity_position_fp); if (node == null || !node.Walkable) { //try x entity_position_fp.x += offset_fp.x; entity_position_fp.z -= offset_fp.z; node = grid_graph.Position2Node(entity_position_fp); if (node == null || !node.Walkable) { return; } else { offset.z = 0; } } else { offset.x = 0; } } } for (int i = 0; i < m_movement_predicts.Count; ++i) { MovementPredict predict = m_movement_predicts[i]; if (predict.m_state == MovementPredict.AccumulateOffsetState || predict.m_state == MovementPredict.FollowLogicState) { predict.m_offset += offset; m_interpolation_tr.localPosition += offset; //LogWrapper.LogDebug("AddPredictOffset, predict.m_offset = ", predict.m_offset.ToString(), ", m_interpolation_tr = ", m_interpolation_tr.localPosition.ToString()); break; } } }
public BTSKillAction_CreateObject(BTSKillAction_CreateObject prototype) : base(prototype) { m_object_type_id = prototype.m_object_type_id; m_object_proto_id = prototype.m_object_proto_id; m_object_life_time = prototype.m_object_life_time; m_generator_cfgid = prototype.m_generator_cfgid; m_offset = prototype.m_offset; m_angle_offset = prototype.m_angle_offset; }
public void SetFacing(Vector3FP direction, bool from_command = false) { if (IsRotatingDisabled) { return; } FixPoint angle = FixPoint.XZToUnityRotationDegree(direction.x, direction.z); SetFacing(angle, from_command); }
public void BuildTargetList(ISpacePartition partition, Player player, Vector3FP position, Vector2FP facing, TargetGatheringParam param, List <int> targets) { m_temp_targets.Clear(); GatherGeneral(partition, player, position, facing, param, m_temp_targets); for (int i = 0; i < m_temp_targets.Count; ++i) { targets.Add(m_temp_targets[i].ID); } m_temp_targets.Clear(); }
void AdjustDirection() { int external_data_type = m_definition_component.ExternalDataType; if (external_data_type == SkillDefinitionComponent.NeedExternalTarget) { Entity target = GetLogicWorld().GetEntityManager().GetObject(m_definition_component.ExternalID); if (target != null) { PositionComponent owner_position_component = GetOwnerEntity().GetComponent(PositionComponent.ID) as PositionComponent; PositionComponent target_position_component = target.GetComponent(PositionComponent.ID) as PositionComponent; owner_position_component.SetFacing(target_position_component.CurrentPosition - owner_position_component.CurrentPosition); } } else if (external_data_type == SkillDefinitionComponent.NeedExternalDirection || external_data_type == SkillDefinitionComponent.NeedExternalOffset) { Vector3FP vector = m_definition_component.ExternalVector; if (vector.IsAllZero()) { return; } PositionComponent position_component = GetOwnerEntity().GetComponent(PositionComponent.ID) as PositionComponent; if (position_component == null) { return; } position_component.SetFacing(vector); } else { Vector3FP vector = m_definition_component.ExternalVector; if (!vector.IsAllZero()) { Entity owner_entity = GetOwnerEntity(); PositionComponent owner_pos_cmp = owner_entity.GetComponent(PositionComponent.ID) as PositionComponent; owner_pos_cmp.SetFacing(vector); } else { int auto_face = m_definition_component.AutoFaceType; if (auto_face == SkillDefinitionComponent.AutoFaceNearestEnemy) { Entity owner_entity = GetOwnerEntity(); PositionComponent target_pos_cmp = GetLogicWorld().GetTargetGatheringManager().GetNearestEnemy(owner_entity); if (target_pos_cmp == null) { return; } PositionComponent owner_pos_cmp = owner_entity.GetComponent(PositionComponent.ID) as PositionComponent; Vector3FP offset = target_pos_cmp.CurrentPosition - owner_pos_cmp.CurrentPosition; owner_pos_cmp.SetFacing(offset); } } } }
public Vector3FP AdjustPosition2Walkable(Vector3FP position) { GridNode node = Position2NearestNode(position); node = FindNearestWalkableNode(node, null); if (node != null) { position = Node2Position(node); } return(position); }
void GatherGeneral(ISpacePartition partition, Player player, Vector3FP position, Vector2FP facing, TargetGatheringParam param, List <Entity> targets) { if (partition == null) { return; } List <PositionComponent> list = null; int gathering_type = param.m_type; if (gathering_type == TargetGatheringType.ForwardRectangle) { list = partition.CollectEntity_ForwardRectangle(position, facing, param.m_param1, param.m_param2); } else if (gathering_type == TargetGatheringType.SurroundingRing) { list = partition.CollectEntity_SurroundingRing(position, param.m_param1, param.m_param2); } else if (gathering_type == TargetGatheringType.ForwardSector) { list = partition.CollectEntity_ForwardSector(position, facing, param.m_param1, param.m_param2); } else if (gathering_type == TargetGatheringType.All) { list = partition.CollectEntity_All(); } if (list == null) { return; } for (int i = 0; i < list.Count; ++i) { PositionComponent position_component = list[i]; Entity entity = position_component.GetOwnerEntity(); if (param.m_category != 0) { EntityDefinitionComponent definition_component = entity.GetComponent(EntityDefinitionComponent.ID) as EntityDefinitionComponent; if (definition_component == null) { continue; } if (!definition_component.IsCategory(param.m_category)) { continue; } } if (player != null && !FactionRelation.IsFactionSatisfied(player.GetFaction(entity.GetOwnerPlayerID()), param.m_faction)) { continue; } targets.Add(entity); } }
public void BuildTargetList(ISpacePartition partition, Player player, Vector3FP position, Vector2FP facing, TargetGatheringParam param, List <Target> targets) { m_temp_targets.Clear(); GatherGeneral(partition, player, position, facing, param, m_temp_targets); for (int i = 0; i < m_temp_targets.Count; ++i) { Target target = RecyclableObject.Create <Target>(); target.Construct(); target.SetEntityTarget(m_temp_targets[i]); targets.Add(target); } m_temp_targets.Clear(); }
protected override void OnActionUpdate(FixPoint delta_time) { int current_target_id = (int)(m_context.GetData(BTContextKey.CurrentTargetID)); if (current_target_id <= 0) { return; } Entity current_target = GetLogicWorld().GetEntityManager().GetObject(current_target_id); if (current_target == null) { return; } Entity owner_entity = GetOwnerEntity(); if (owner_entity == null) { return; } PositionComponent position_cmp = owner_entity.GetComponent(PositionComponent.ID) as PositionComponent; LocomotorComponent locomotor_cmp = owner_entity.GetComponent(LocomotorComponent.ID) as LocomotorComponent; PositionComponent target_position_cmp = current_target.GetComponent(PositionComponent.ID) as PositionComponent; Vector3FP direction = target_position_cmp.CurrentPosition - position_cmp.CurrentPosition; FixPoint distance = direction.FastLength() - target_position_cmp.Radius - position_cmp.Radius; //ZZWTODO 多处距离计算 if (distance <= m_range) { return; } PathFindingComponent pathfinding_component = owner_entity.GetComponent(PathFindingComponent.ID) as PathFindingComponent; if (pathfinding_component != null) { if (pathfinding_component.FindPath(target_position_cmp.CurrentPosition)) { locomotor_cmp.GetMovementProvider().FinishMovementWhenTargetInRange(target_position_cmp, m_range); } } else { List <Vector3FP> path = new List <Vector3FP>(); path.Add(position_cmp.CurrentPosition); path.Add(target_position_cmp.CurrentPosition); locomotor_cmp.MoveAlongPath(path, false); locomotor_cmp.GetMovementProvider().FinishMovementWhenTargetInRange(target_position_cmp, m_range); } }
public List <PositionComponent> CollectEntity_ForwardRectangle(Vector3FP position, Vector2FP direction, FixPoint length, FixPoint width, int exclude_id) { m_collection.Clear(); Vector2FP start_position = new Vector2FP(position); Vector2FP end_position = start_position + direction * length; ComputeAreaXZ(start_position, end_position); Vector2FP side = direction.Perpendicular(); width = width >> 1; Cell cell; PositionComponent cmp; Vector2FP offset; 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; } offset.x = cmp.CurrentPosition.x - position.x; offset.z = cmp.CurrentPosition.z - position.z; FixPoint component = offset.Dot(ref direction); FixPoint radius = cmp.Radius; if (component < -radius || component > length + radius) { continue; } component = offset.Dot(ref side); if (component < FixPoint.Zero) { component = -component; } if (component > width + radius) { continue; } m_collection.Add(cmp); } } } return(m_collection); }
void AdvanceWayPoint() { m_cur_way_point += 1; if (m_cur_way_point > m_path.Count - 1) { m_cur_way_point = INVALID_WAYPOINT_INDEX; m_callback.MovementFinished(); return; } m_direction = m_path[m_cur_way_point] - m_path[m_cur_way_point - 1]; FixPoint distance = m_direction.Normalize(); m_remain_time = distance / m_max_speed; m_position_component.SetFacing(m_direction); }
public bool MoveByDirection(Vector3FP direction) { if (!IsEnable()) { return(false); } if (m_movement_provider as MovementByDirection == null) { ClearMovementProvider(); m_movement_provider = RecyclableObject.Create <MovementByDirection>(); m_movement_provider.SetCallback(this); m_movement_provider.SetMaxSpeed(m_current_max_speed); } m_movement_provider.MoveByDirection(direction); StartMoving(); return(true); }
public CellSpacePartition(LogicWorld logic_world, FixPoint x_size, FixPoint z_size, Vector3FP left_bottom_position) { m_logic_world = logic_world; m_left_bottom_position = left_bottom_position; CELL_X_COUNT = (int)((x_size + CELL_SIZE - FixPoint.PrecisionFP) / CELL_SIZE); CELL_Z_COUNT = (int)((z_size + CELL_SIZE - FixPoint.PrecisionFP) / CELL_SIZE); MAX_X_INDEX = CELL_X_COUNT - 1; MAX_Z_INDEX = CELL_Z_COUNT - 1; m_cells = new Cell[CELL_X_COUNT, CELL_Z_COUNT]; for (int x = 0; x < CELL_X_COUNT; ++x) { for (int z = 0; z < CELL_Z_COUNT; ++z) { m_cells[x, z] = new Cell(); } } }
public void UpdateEntity(PositionComponent entity, Vector3FP new_position) { Cell old_cell = Position2Cell(entity.CurrentPosition); Cell new_cell = Position2Cell(new_position); if (old_cell == new_cell) { return; } if (old_cell != null) { old_cell.m_entities.Remove(entity); } if (new_cell != null) { new_cell.m_entities.Add(entity); } }
public Vector3FP AdjustPosition2Walkable(Vector3FP position) { GridGraph graph = null; if (m_current_space != null) { graph = m_current_space.m_graph; } else { graph = GetLogicWorld().GetDefaultSceneSpace().m_graph; } if (graph == null) { return(position); } return(graph.AdjustPosition2Walkable(position)); }