private IEnumerator BroadcastTransformPosition() { while (true) { if (Vector3.Magnitude(lastPosition - transform.position) > Vector3.kEpsilon) { lastPosition = transform.position; isFinishedMoving = false; SendService.SendMessage(new Sub60MovingFastPositionSetCommand(Identity.EntityId, UnitScaler.UnScaleYtoZ(transform.position)).ToPayload()); } else if (!isFinishedMoving) { lastPosition = transform.position; isFinishedMoving = true; //TODO: Handle rotation //Send a stop if we stopped moving SendService.SendMessage(new Sub60FinishedMovingCommand(Identity.EntityId, UnitScaler.ScaleYRotation(transform.rotation.eulerAngles.y), UnitScaler.UnScale(transform.position).ToNetworkVector3(), RoomQueryService.RoomIdForPlayerById(Identity.EntityId), ZoneData.ZoneId).ToPayload()); } yield return(new WaitForSeconds(1.0f / BroadcastsPerSecond)); } }
private void InitializeAckDataToEntityMappables(Sub60FinishedWarpAckCommand command, int entityGuid) { //We have to do basically what the 3 packet process does for newly joining clients //We need to create the world transform so that it will be known where to spawn. float rotation = UnitScaler.ScaleYRotation(command.YAxisRotation); Vector3 position = UnitScaler.Scale(command.Position); WorldTransformMappable[entityGuid] = new WorldTransform(position, Quaternion.Euler(0.0f, rotation, 0.0f)); //Then we have to actually create/set the zone data so that //it's known which zone the player is in ZoneDataMappable[entityGuid] = new PlayerZoneData(command.ZoneId); }
/// <inheritdoc /> protected override Task HandleSubMessage(IPeerMessageContext <PSOBBGamePacketPayloadClient> context, Sub60LobbySoccerBallMoveEventPayload command) { //It's possible we joined the lobby after the ball has spawned. If we havw we won't be able to see it. if (CurrentBall == null) { return(Task.CompletedTask); } //TODO: Move forward in direction of the rotation //Set the start position of the ball IKickable kickable = CurrentBall.GetComponent <IKickable>(); if (kickable == null) { throw new InvalidOperationException("Ball was not kickable."); } kickable.Kick(new Vector3(UnitScaler.ScaleX(command.KickStartPosition.X), CurrentBall.transform.position.y, UnitScaler.ScaleZ(command.KickStartPosition.Y)), UnitScaler.UnScaleYRotation(command.YAxisRotation)); return(Task.CompletedTask); }
public void MakeBody() { if (!gotShaderIds) { GetShaderIDs(); } flags = new CreatureRawFlags(race.flags); bodyCategory = FindBodyCategory(caste); //If there's already any spawned parts, we need to clear them so we can remake the body from scratch. if (spawnedParts.Count > 0) { foreach (var part in spawnedParts) { if (part.Value != null) { if (Application.isPlaying) { Destroy(part.Value.gameObject); } else { DestroyImmediate(part.Value.gameObject); } } } spawnedParts.Clear(); } //Clearing this will serve the purpose of resetting the inventory. inventoryModes = new InventoryMode[0]; //They're made standing, so the current state should reflect that. onGround = false; float unitVolume = caste.adult_size; if (unit != null && unit.size_info != null) { unitVolume = unit.size_info.size_cur; } unitVolume = UnitScaler.GetAdjustedUnitSize(unitVolume); float scale = unitVolume / caste.total_relsize * 10; MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock(); bodyScale = BodyDefinition.GetBodyScale(bodyCategory, race, caste); Vector3 unitShape = Vector3.one; for (int i = 0; i < caste.body_appearance_modifiers.Count; i++) { var mod = caste.body_appearance_modifiers[i]; int value = 100; if (unit != null && unit.appearance != null) { value = unit.appearance.body_modifiers[i]; } else { value = UnityEngine.Random.Range(mod.mod_min, mod.mod_max); } switch (mod.type) { case "BROADNESS": unitShape.x = value / 100f; break; case "HEIGHT": unitShape.y = value / 100f; break; case "LENGTH": unitShape.z = value / 100f; break; default: break; } } bodyScale = BodyPart.MultiplyScales(bodyScale, unitShape); for (int i = 0; i < caste.body_parts.Count; i++) { var part = caste.body_parts[i]; if (part.flags[(int)BodyPartFlags.BodyPartRawFlags.INTERNAL]) { continue; } var spawnedPart = new GameObject().AddComponent <BodyPart>(); spawnedPart.name = string.Format("{0} ({1})", part.token, part.category); spawnedPart.token = part.token; spawnedPart.category = part.category; spawnedPart.flags = new BodyPartFlags(part.flags); spawnedPart.volume = part.relsize * scale; spawnedPart.layers = part.layers; var model = BodyDefinition.GetPart(bodyCategory, race, caste, part); if (model != null) { #if UNITY_EDITOR var placedModel = (BodyPartModel)UnityEditor.PrefabUtility.InstantiatePrefab(model); #else var placedModel = Instantiate(model); #endif placedModel.transform.SetParent(spawnedPart.transform); placedModel.CollectEquipment(); spawnedPart.modeledPart = placedModel; } if (spawnedPart.modeledPart == null) { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube).AddComponent <VolumeKeeper>(); if ((Application.isPlaying)) { Destroy(cube.GetComponent <BoxCollider>()); } else { DestroyImmediate(cube.GetComponent <BoxCollider>()); } cube.name = spawnedPart.name + " cube"; cube.transform.SetParent(spawnedPart.transform); cube.volume = spawnedPart.volume; cube.FixVolume(); cube.gameObject.AddComponent <BodyLayer>(); spawnedPart.placeholder = cube; if (skinMat == null) { skinMat = Resources.Load <Material>("Skin"); } cube.GetComponent <MeshRenderer>().sharedMaterial = skinMat; } if (spawnedPart.flags.upperbody) { upperBody = spawnedPart; foreach (var item in spawnedPart.GetComponentsInChildren <BodyPartChildPlaceholder>()) { if (item.category == ":ATTACH:") { riderPosition = item.transform; break; } } } if (spawnedPart.flags.lowerbody) { lowerBody = spawnedPart; } var modeledLayers = spawnedPart.GetComponentsInChildren <BodyLayer>(); foreach (var modeledLayer in modeledLayers) { modeledLayer.parentPart = spawnedPart; } foreach (var layer in part.layers) { var matchedLayers = Array.FindAll(modeledLayers, x => x.layerName == layer.layer_name); if (matchedLayers.Length == 0) { matchedLayers = Array.FindAll(modeledLayers, x => string.IsNullOrEmpty(x.layerName)); if (matchedLayers.Length == 0) { spawnedPart.layerModels.Add(new BodyLayerPlaceholder(layer)); } else { bool matchedAny = false; foreach (var matchedLayer in matchedLayers) { if (!matchedLayer.placed) { matchedLayer.layerRaw = layer; spawnedPart.layerModels.Add(matchedLayer); matchedLayer.placed = true; matchedAny = true; break; } } if (!matchedAny) { spawnedPart.layerModels.Add(new BodyLayerPlaceholder(layer)); } } } else { bool matchedAny = false; foreach (var matchedLayer in matchedLayers) { if (!matchedLayer.placed) { matchedLayer.layerRaw = layer; spawnedPart.layerModels.Add(matchedLayer); matchedLayer.placed = true; matchedAny = true; break; } } if (!matchedAny) { spawnedPart.layerModels.Add(new BodyLayerPlaceholder(layer)); } } } foreach (var modeledLayer in modeledLayers) { if (!modeledLayer.placed) { modeledLayer.gameObject.SetActive(false); } } foreach (var layerModel in spawnedPart.layerModels) { if (layerModel == null || !(layerModel is BodyLayer)) { continue; } ((BodyLayer)layerModel).ApplyMaterials(race, propertyBlock); } spawnedParts[i] = spawnedPart; } for (int modNum = 0; modNum < caste.modifier_idx.Count; modNum++) { if (!spawnedParts.ContainsKey(caste.part_idx[modNum])) { continue; } var mod = caste.modifiers[caste.modifier_idx[modNum]]; var part = spawnedParts[caste.part_idx[modNum]]; if (caste.layer_idx[modNum] >= 0) { var layer = part.layerModels[caste.layer_idx[modNum]]; if (layer != null) { layer.AddMod(new BodyPart.ModValue(mod, (unit != null && unit.appearance != null) ? unit.appearance.bp_modifiers[modNum] : 100)); } } else { part.mods.Add(new BodyPart.ModValue(mod, (unit != null && unit.appearance != null) ? unit.appearance.bp_modifiers[modNum] : 100)); } } for (int modNum = 0; modNum < caste.color_modifiers.Count; modNum++) { var colorMod = caste.color_modifiers[modNum]; int seed = Mathf.Abs(GetInstanceID() * modNum) % colorMod.patterns.Count; for (int i = 0; i < colorMod.body_part_id.Count; i++) { var part = spawnedParts[colorMod.body_part_id[i]]; if (part == null || !part.gameObject.activeSelf) { continue; } var layer = part.layerModels[colorMod.tissue_layer_id[i]]; if (layer == null || !layer.IsActive || !(layer is BodyLayer)) { continue; } PatternDescriptor pattern; if (unit != null && unit.appearance != null) { pattern = colorMod.patterns[unit.appearance.colors[modNum]]; } else { pattern = colorMod.patterns[seed]; } var matIndex = ContentLoader.GetPatternIndex(race.tissues[layer.TissueID].material); var shapeIndex = ContentLoader.GetShapeIndex(race.tissues[layer.TissueID].material); if (colorMod.start_date > 0 && unit != null) { ((BodyLayer)layer).ApplyPattern(pattern, Mathf.InverseLerp(colorMod.start_date * 1200, colorMod.end_date * 1200, unit.age), propertyBlock, matIndex, shapeIndex); } else { ((BodyLayer)layer).ApplyPattern(pattern, 1, propertyBlock, matIndex, shapeIndex); } } } for (int i = 0; i < caste.body_parts.Count; i++) { if (!spawnedParts.ContainsKey(i)) { continue; } var part = caste.body_parts[i]; if (!spawnedParts.ContainsKey(part.parent)) { spawnedParts[i].transform.SetParent(transform); } else { spawnedParts[i].transform.SetParent(spawnedParts[part.parent].transform); spawnedParts[i].parent = spawnedParts[part.parent]; spawnedParts[part.parent].children.Add(spawnedParts[i]); } if (part.parent < 0) { rootPart = spawnedParts[i]; } if (spawnedParts[i].flags.stance) { stanceCount++; } } foreach (var part in spawnedParts) { foreach (var layer in part.Value.layerModels) { if (layer != null && (layer is BodyLayer)) { ((BodyLayer)layer).ApplyMods(); } } } if (rootPart == null) { return; //There's no root part, means there's no body. } //Use this when we do body part mods. //for(int i = 0; i < caste.modifier_idx.Count; i++) //{ // var modifier = caste.modifiers[caste.modifier_idx[i]]; // var part = caste.body_parts[caste.part_idx[i]]; //} rootPart.Arrange(this); foreach (var part in spawnedParts) { if (part.Value.flags.head) { part.Value.transform.localScale *= GameSettings.Instance.units.chibiness; } } if (unit != null && unit.wounds != null) { foreach (var wound in unit.wounds) { foreach (var woundPart in wound.parts) { if (spawnedParts.ContainsKey(woundPart.body_part_id)) { spawnedParts[woundPart.body_part_id].gameObject.SetActive(!wound.severed_part); } } } } bounds = rootPart.GetComponentInChildren <MeshRenderer>().bounds; foreach (var item in rootPart.GetComponentsInChildren <MeshRenderer>()) { bounds.Encapsulate(item.bounds); } rootPart.transform.localPosition = new Vector3(0, -bounds.min.y, 0); }
void UpdateSpriteUnit(UnitDefinition unit, ref int creatureCount) { UnitFlags1 flags1 = (UnitFlags1)unit.flags1; //UnitFlags2 flags2 = (UnitFlags2)unit.flags2; //UnitFlags3 flags3 = (UnitFlags3)unit.flags3; if (!IsValidCreature(unit)) { if (creatureList.ContainsKey(unit.id)) { Destroy(creatureList[unit.id].gameObject); creatureList.Remove(unit.id); } } else { MapDataStore.Tile tile = null; if (MapDataStore.Main != null) { tile = MapDataStore.Main[unit.pos_x, unit.pos_y, unit.pos_z]; } if (!ShouldRender(unit.pos_x, unit.pos_y, unit.pos_z, tile) && !singleRow) { if (creatureList.ContainsKey(unit.id)) { creatureList[unit.id].gameObject.SetActive(false); } return; } else if (creatureList.ContainsKey(unit.id)) { creatureList[unit.id].gameObject.SetActive(true); } if (!creatureList.ContainsKey(unit.id)) { creatureList[unit.id] = Instantiate(creatureTemplate, gameObject.transform); creatureList[unit.id].name = "Unit_" + unit.id; creatureList[unit.id].transform.position = new Vector3(-3000, -3000, -3000); } if (creatureList[unit.id].gameObject.activeSelf) //Only update stuff if it's actually visible. { if (singleRow) { creatureList[unit.id].transform.position = new Vector3(creatureCount * spacing, 0, 0); } else { var position = GameMap.DFtoUnityCoord(unit.pos_x + unit.subpos_x, unit.pos_y + unit.subpos_y, unit.pos_z + unit.subpos_z); //RaycastHit hitInfo; //if (((flags1 & UnitFlags1.projectile) != UnitFlags1.projectile) && Physics.Raycast(position + new Vector3(0, 2.9f, 0), Vector3.down, out hitInfo, 3, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Ignore)) // creatureList[unit.id].TargetPos = hitInfo.point; //else creatureList[unit.id].TargetPos = position + new Vector3(0, GameMap.floorHeight, 0); } if (unit.rider_id >= 0 && creatureList.ContainsKey(unit.rider_id)) { creatureList[unit.id].TargetPos += new Vector3(0, creatureList[unit.rider_id].transform.localScale.y, 0); } UnitScaler unitScaler = creatureList[unit.id].GetComponentInChildren <UnitScaler>(); if (unitScaler != null) { unitScaler.UpdateSize(unit, creatureList[unit.id].GetComponentInChildren <LayeredSprite>()); } CameraFacing cameraFacing = creatureList[unit.id].GetComponentInChildren <CameraFacing>(); if ((flags1 & UnitFlags1.on_ground) == UnitFlags1.on_ground) { cameraFacing.transform.localPosition = Vector3.zero; cameraFacing.enabled = false; cameraFacing.transform.rotation = Quaternion.Euler(90, 0, 0); } else { cameraFacing.transform.localPosition = new Vector3(0, 1, 0); cameraFacing.enabled = true; } creatureList[unit.id].UpdateCreature(unit); } creatureCount++; } }
void UpdateCreatures() { if (!GameSettings.Instance.units.drawUnits) { return; } if (creatureTemplate == null) { return; } if (ContentLoader.Instance == null) { return; } var tempUnitList = DFConnection.Instance.PopUnitListUpdate(); if (tempUnitList == null) { return; } else { Units = tempUnitList; } UnityEngine.Profiling.Profiler.BeginSample("UpdateCreatures", this); int creatureCount = 0; foreach (var unit in Units.creature_list) { if (creatureList == null) { creatureList = new Dictionary <int, Creature>(); } UnitFlags1 flags1 = (UnitFlags1)unit.flags1; //UnitFlags2 flags2 = (UnitFlags2)unit.flags2; //UnitFlags3 flags3 = (UnitFlags3)unit.flags3; if (((flags1 & UnitFlags1.dead) == UnitFlags1.dead) || ((flags1 & UnitFlags1.left) == UnitFlags1.left) || ((flags1 & UnitFlags1.caged) == UnitFlags1.caged) || ((flags1 & UnitFlags1.forest) == UnitFlags1.forest) ) { if (creatureList.ContainsKey(unit.id)) { Destroy(creatureList[unit.id].gameObject); creatureList.Remove(unit.id); } } else { if (!creatureList.ContainsKey(unit.id)) { creatureList[unit.id] = Instantiate(creatureTemplate, gameObject.transform); creatureList[unit.id].name = "Unit_" + unit.id; creatureList[unit.id].transform.position = new Vector3(-3000, -3000, -3000); } if (!singleRow) { MapDataStore.Tile tile = null; if (MapDataStore.Main != null) { tile = MapDataStore.Main[unit.pos_x, unit.pos_y, unit.pos_z]; } creatureList[unit.id].gameObject.SetActive( (unit.pos_z < (GameMap.Instance.firstPerson ? GameMap.Instance.PosZ + GameSettings.Instance.rendering.drawRangeUp : GameMap.Instance.PosZ)) && unit.pos_z >= (GameMap.Instance.PosZ - GameSettings.Instance.rendering.drawRangeDown) && (unit.pos_x / GameMap.blockSize > (GameMap.Instance.PosXBlock - GameSettings.Instance.rendering.drawRangeSide)) && (unit.pos_x / GameMap.blockSize < (GameMap.Instance.PosXBlock + GameSettings.Instance.rendering.drawRangeSide)) && (unit.pos_y / GameMap.blockSize > (GameMap.Instance.PosYBlock - GameSettings.Instance.rendering.drawRangeSide)) && (unit.pos_y / GameMap.blockSize < (GameMap.Instance.PosYBlock + GameSettings.Instance.rendering.drawRangeSide)) && (tile != null ? !tile.Hidden : true) ); } if (creatureList[unit.id].gameObject.activeSelf) //Only update stuff if it's actually visible. { if (singleRow) { creatureList[unit.id].transform.position = new Vector3(creatureCount * spacing, 0, 0); } else { var position = GameMap.DFtoUnityCoord(unit.pos_x + unit.subpos_x, unit.pos_y + unit.subpos_y, unit.pos_z + unit.subpos_z); RaycastHit hitInfo; if (((flags1 & UnitFlags1.projectile) != UnitFlags1.projectile) && Physics.Raycast(position + new Vector3(0, 2.9f, 0), Vector3.down, out hitInfo, 3, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Ignore)) { creatureList[unit.id].TargetPos = hitInfo.point; } else { creatureList[unit.id].TargetPos = position + new Vector3(0, GameMap.floorHeight, 0); } } if (unit.rider_id >= 0 && creatureList.ContainsKey(unit.rider_id)) { creatureList[unit.id].TargetPos += new Vector3(0, creatureList[unit.rider_id].transform.localScale.y, 0); } UnitScaler unitScaler = creatureList[unit.id].GetComponentInChildren <UnitScaler>(); if (unitScaler != null) { unitScaler.UpdateSize(unit, creatureList[unit.id].GetComponentInChildren <LayeredSprite>()); } CameraFacing cameraFacing = creatureList[unit.id].GetComponentInChildren <CameraFacing>(); if ((flags1 & UnitFlags1.on_ground) == UnitFlags1.on_ground) { cameraFacing.transform.localPosition = Vector3.zero; cameraFacing.enabled = false; cameraFacing.transform.rotation = Quaternion.Euler(90, 0, 0); } else { cameraFacing.transform.localPosition = new Vector3(0, 1, 0); cameraFacing.enabled = true; } creatureList[unit.id].GetComponent <Creature>().UpdateCreature(unit); } creatureCount++; } } UnityEngine.Profiling.Profiler.EndSample(); }
/// <inheritdoc /> public Task UpdatedMovementLocation(Vector3 position, Quaternion rotation) { return(SendService.SendMessage(new Sub60MovingFastPositionSetCommand(PlayerSlotModel.SlotSelected, UnitScaler.UnScaleYtoZ(position)).ToPayload())); }
/// <inheritdoc /> public Task StopMovementAsync(Vector3 position, Quaternion rotation) { return(SendService.SendMessage(new Sub60FinishedMovingCommand(PlayerSlotModel.SlotSelected, UnitScaler.ScaleYRotation(rotation.eulerAngles.y), UnitScaler.UnScale(position).ToNetworkVector3(), RoomQueryService.RoomIdForPlayerById(PlayerSlotModel.SlotSelected), ZoneData.ZoneId).ToPayload())); }