Beispiel #1
0
        private void MergeFrom(JoinState joinState,
                               Entity otherLineEntity,
                               out DynamicBuffer <LineKnotData> knotBuffer)
        {
            var thisKnots  = LineKnotData[lineEntity];
            var otherKnots = LineKnotData[otherLineEntity];
            var newKnots   = Ecb.SetBuffer <LineKnotData>(index, lineEntity);

            void AddKnotsInSameOrder(DynamicBuffer <LineKnotData> knots, bool skipFirst = false)
            {
                var len = knots.Length;

                for (var i = skipFirst ? 1 : 0; i < len; i++)
                {
                    newKnots.Add(knots[i]);
                }
            }

            void AddKnotsInReverseOrder(DynamicBuffer <LineKnotData> knots, bool skipFirst = false)
            {
                var len = knots.Length;

                for (var i = len - (skipFirst ? 2 : 1); i >= 0; i--) // -2 as to skip the first knot
                {
                    newKnots.Add(knots[i]);
                }
            }

            // ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
            switch (joinState)
            {
            case JoinState.BtoA:
                AddKnotsInSameOrder(thisKnots);
                AddKnotsInSameOrder(otherKnots, true);
                break;

            case JoinState.BtoB:
                AddKnotsInSameOrder(thisKnots);
                AddKnotsInReverseOrder(otherKnots, true);
                break;

            case JoinState.AtoB:
                AddKnotsInSameOrder(otherKnots);
                AddKnotsInSameOrder(thisKnots, true);
                break;

            case JoinState.AtoA:
                AddKnotsInReverseOrder(otherKnots);
                AddKnotsInSameOrder(thisKnots, true);
                break;
            }

            knotBuffer = newKnots;
        }
        public StateData Copy(int jobIndex, EntityCommandBuffer.Concurrent entityCommandBuffer)
        {
            var stateEntity       = entityCommandBuffer.Instantiate(jobIndex, StateEntity);
            var traitBasedObjects = entityCommandBuffer.SetBuffer <TraitBasedObject>(jobIndex, stateEntity);

            traitBasedObjects.CopyFrom(TraitBasedObjects.AsNativeArray());
            var traitBasedObjectIds = entityCommandBuffer.SetBuffer <TraitBasedObjectId>(jobIndex, stateEntity);

            traitBasedObjectIds.CopyFrom(TraitBasedObjectIds.AsNativeArray());

            var Locations = entityCommandBuffer.SetBuffer <Location>(jobIndex, stateEntity);

            Locations.CopyFrom(LocationBuffer.AsNativeArray());
            var Robots = entityCommandBuffer.SetBuffer <Robot>(jobIndex, stateEntity);

            Robots.CopyFrom(RobotBuffer.AsNativeArray());
            var Dirts = entityCommandBuffer.SetBuffer <Dirt>(jobIndex, stateEntity);

            Dirts.CopyFrom(DirtBuffer.AsNativeArray());
            var Moveables = entityCommandBuffer.SetBuffer <Moveable>(jobIndex, stateEntity);

            Moveables.CopyFrom(MoveableBuffer.AsNativeArray());

            return(new StateData
            {
                StateEntity = stateEntity,
                TraitBasedObjects = traitBasedObjects,
                TraitBasedObjectIds = traitBasedObjectIds,

                LocationBuffer = Locations,
                RobotBuffer = Robots,
                DirtBuffer = Dirts,
                MoveableBuffer = Moveables,
            });
        }
        public StateData Copy(int jobIndex, EntityCommandBuffer.Concurrent entityCommandBuffer)
        {
            var stateEntity       = entityCommandBuffer.Instantiate(jobIndex, StateEntity);
            var traitBasedObjects = entityCommandBuffer.SetBuffer <TraitBasedObject>(jobIndex, stateEntity);

            traitBasedObjects.CopyFrom(TraitBasedObjects.AsNativeArray());
            var traitBasedObjectIds = entityCommandBuffer.SetBuffer <TraitBasedObjectId>(jobIndex, stateEntity);

            traitBasedObjectIds.CopyFrom(TraitBasedObjectIds.AsNativeArray());

            var Games = entityCommandBuffer.SetBuffer <Game>(jobIndex, stateEntity);

            Games.CopyFrom(GameBuffer.AsNativeArray());
            var Coordinates = entityCommandBuffer.SetBuffer <Coordinate>(jobIndex, stateEntity);

            Coordinates.CopyFrom(CoordinateBuffer.AsNativeArray());
            var Cells = entityCommandBuffer.SetBuffer <Cell>(jobIndex, stateEntity);

            Cells.CopyFrom(CellBuffer.AsNativeArray());
            var Blockers = entityCommandBuffer.SetBuffer <Blocker>(jobIndex, stateEntity);

            Blockers.CopyFrom(BlockerBuffer.AsNativeArray());

            return(new StateData
            {
                StateEntity = stateEntity,
                TraitBasedObjects = traitBasedObjects,
                TraitBasedObjectIds = traitBasedObjectIds,

                GameBuffer = Games,
                CoordinateBuffer = Coordinates,
                CellBuffer = Cells,
                BlockerBuffer = Blockers,
            });
        }
        public StateData Copy(int jobIndex, EntityCommandBuffer.Concurrent entityCommandBuffer)
        {
            var stateEntity       = entityCommandBuffer.Instantiate(jobIndex, StateEntity);
            var traitBasedObjects = entityCommandBuffer.SetBuffer <TraitBasedObject>(jobIndex, stateEntity);

            traitBasedObjects.CopyFrom(TraitBasedObjects.AsNativeArray());
            var traitBasedObjectIds = entityCommandBuffer.SetBuffer <TraitBasedObjectId>(jobIndex, stateEntity);

            traitBasedObjectIds.CopyFrom(TraitBasedObjectIds.AsNativeArray());

            var Agents = entityCommandBuffer.SetBuffer <Agent>(jobIndex, stateEntity);

            Agents.CopyFrom(AgentBuffer.AsNativeArray());
            var Locations = entityCommandBuffer.SetBuffer <Location>(jobIndex, stateEntity);

            Locations.CopyFrom(LocationBuffer.AsNativeArray());
            var Targets = entityCommandBuffer.SetBuffer <Target>(jobIndex, stateEntity);

            Targets.CopyFrom(TargetBuffer.AsNativeArray());
            var Collectibles = entityCommandBuffer.SetBuffer <Collectible>(jobIndex, stateEntity);

            Collectibles.CopyFrom(CollectibleBuffer.AsNativeArray());

            return(new StateData
            {
                StateEntity = stateEntity,
                TraitBasedObjects = traitBasedObjects,
                TraitBasedObjectIds = traitBasedObjectIds,

                AgentBuffer = Agents,
                LocationBuffer = Locations,
                TargetBuffer = Targets,
                CollectibleBuffer = Collectibles,
            });
        }
Beispiel #5
0
        public void spawn_internal(EntityCommandBuffer.Concurrent command_buffer,
                                   ref LockTarget lt,
                                   ref float3 position,
                                   ref quaternion rotation,
                                   ref RandomLocal random,
                                   int index,
                                   Entity target,
                                   float arrive_period,
                                   int job_index)
        {
            command_buffer.CreateEntity(job_index, archetype_);
            command_buffer.SetComponent(job_index, new Position {
                Value = position,
            });
            command_buffer.SetComponent(job_index, new LaserData {
                end_time_      = CV.MaxValue,
                target_entity_ = target,
                arrive_period_ = arrive_period,
                lock_target_   = lt,
            });
            var         rb            = new RigidbodyPosition(0f /* damper */);
            const float FIRE_VEL      = 8f;
            var         fire_velocity = new float3(index % 2 == 0 ? FIRE_VEL * 2f : -FIRE_VEL * 2f,
                                                   random.range(-FIRE_VEL, FIRE_VEL),
                                                   random.range(-FIRE_VEL, FIRE_VEL));

            rb.velocity_ = math.mul(rotation, fire_velocity);
            command_buffer.SetComponent(job_index, rb);
            var td = new TrailData {
                color_type_ = (int)TrailManager.ColorType.Green,
            };

            command_buffer.SetComponent(job_index, td);

            {
                var buffer = command_buffer.SetBuffer <TrailPoint>(job_index);
                for (var i = 0; i < buffer.Capacity; ++i)
                {
                    buffer.Add(new TrailPoint {
                        position_ = position, normal_ = new float3(0f, 0f, 1f),
                    });
                }
            }

            var material = TrailManager.Instance.getMaterial();
            var renderer = new TrailRenderer {
                material_ = material,
            };

            command_buffer.SetSharedComponent <TrailRenderer>(job_index, renderer);
        }
Beispiel #6
0
        public void Execute(int index)
        {
            // If index is different, this means this line entity was already in the
            // array, there for this is a duplicate and we can skip it.
            if (LineEntities.IndexOf <Entity>(LineEntities[index]) != index)
            {
                return;
            }

            newKnotData = Ecb.SetBuffer <LineKnotData>(index, LineEntities[index]);

            line = Lines[LineEntities[index]];

            lineProfile = LineProfiles.Exists(line.Profile) ? LineProfiles[line.Profile] : LineProfile.Default();

            var b1 = BezierData[index].B1;
            var b2 = BezierData[index].B2;

            SetKnotsForBezier(b1);

            if (!b1.c2.IsCloseTo(b2.c2, lineProfile.KnotSpacing))
            {
                SetKnotsForBezier(new float3x3(b1.c2, math.lerp(b1.c2, b2.c2, 0.5f), b2.c2));
            }

            SetKnotsForBezier(b2, true);

            AdjustHeight(HeightBezierData[index]);

            var jpA = LineJoinPoints[index].A;
            var jpB = LineJoinPoints[index].B;

            if (newKnotData.Length > 0)
            {
                jpA.Pivot = newKnotData[0].Position;
                jpB.Pivot = newKnotData[newKnotData.Length - 1].Position;
            }

            LineJoinPoints[index] = new JoinPointPair {
                A = jpA, B = jpB
            };
        }
Beispiel #7
0
            public void Execute(Entity entity, int index, [ReadOnly] DynamicBuffer <LinkedEntityGroup> linkedGroup)
            {
                using (var linkedList = new NativeList <LinkedEntityGroup>(linkedGroup.Length, Allocator.Temp))
                {
                    foreach (var linked in linkedGroup)
                    {
                        if (keepTag.HasComponent(linked.Value))
                        {
                            commandBuffer.RemoveComponent <KeepTag>(index, linked.Value);
                            linkedList.Add(linked);
                        }
                        else
                        {
                            commandBuffer.DestroyEntity(index, linked.Value);
                        }
                    }

                    commandBuffer.SetBuffer <LinkedEntityGroup>(index, entity).CopyFrom(linkedList);
                    commandBuffer.RemoveComponent <KeepTag>(index, entity);
                }
            }
        public void Execute(Entity entity, int index, [ReadOnly] ref ChunkPosition chunkPos, [ReadOnly] ref ChunkFinishedGenerate chunk)
        {
            DynamicBuffer <ChunkBufferBlockToCreateData> buffer = cbd[entity];

            DynamicBuffer <ChunkChildBlock> children = commandBuffer.SetBuffer <ChunkChildBlock>(index, entity);

            children.Clear();

            for (int i = 0; i < buffer.Length; ++i)
            {
                var e = buffer[i];

                var ent = commandBuffer.Instantiate(index, blockPrefab[e.blockId]);
                commandBuffer.SetComponent(index, ent, new Translation {
                    Value = e.position
                });

                children.Add(new ChunkChildBlock {
                    Value = ent
                });
            }
            commandBuffer.RemoveComponent <ChunkFinishedGenerate>(index, entity);
            commandBuffer.AddComponent(index, entity, new ChunkSpawned());
        }
Beispiel #9
0
            public void Execute(Entity entity, int index, [ReadOnly] ref SetPlaceOwnerEvent eventInfo)
            {
                if (!eventInfo.IsValidate)
                {
                    return;
                }

                var ownerEntity  = eventInfo.OwnerEntity;
                var objectEntity = eventInfo.ObjectEntity;
                var place        = Places[eventInfo.ObjectEntity];

                // 检查数量是否超过允许占有的上限
                if (BeyondOccupationLimit(ref eventInfo, place.PlaceType, place.ProcessionLimit))
                {
                    return;
                }

                Ecb.SetBuffer <MyOwnPlace>(index, ownerEntity).Add(new MyOwnPlace
                {
                    Entity = eventInfo.ObjectEntity, Type = place.PlaceType
                });

                // 当占有public的时候,没有owner,需要add;当占有私有并有主时,需要Set。
                if (eventInfo.IsForce && eventInfo.HasOwner)
                {
                    Ecb.SetComponent(index, objectEntity, new Owner {
                        OwnerEntity = ownerEntity
                    });
                }
                else
                {
                    Ecb.AddComponent(index, objectEntity, new Owner {
                        OwnerEntity = ownerEntity
                    });
                }
            }
Beispiel #10
0
            public void Execute(Entity entity, int index, ref Translation position, ref Rotation rotation, ref Velocity velocity,
                                ref ShipStateComponentData state, [ReadOnly] ref PlayerIdComponentData playerIdData, [ReadOnly] ref PredictedGhostComponent prediction)
            {
                if (!GhostPredictionSystemGroup.ShouldPredict(currentTick, prediction))
                {
                    return;
                }
                var             input = inputFromEntity[entity];
                ShipCommandData inputData;

                if (!input.GetDataAtTick(currentTick, out inputData))
                {
                    inputData.shoot = 0;
                }

                state.State = inputData.thrust;

                if (inputData.left == 1)
                {
                    rotation.Value = math.mul(rotation.Value, quaternion.RotateZ(math.radians(-displacement * deltaTime)));
                }

                if (inputData.right == 1)
                {
                    rotation.Value = math.mul(rotation.Value, quaternion.RotateZ(math.radians(displacement * deltaTime)));
                }

                if (inputData.thrust == 1)
                {
                    float3 fwd = new float3(0, playerForce * deltaTime, 0);
                    velocity.Value += math.mul(rotation.Value, fwd).xy;
                }

                position.Value.xy += velocity.Value * deltaTime;

                var canShoot = state.WeaponCooldown == 0 || SequenceHelpers.IsNewer(currentTick, state.WeaponCooldown);

                if (inputData.shoot != 0 && canShoot)
                {
                    if (bulletPrefab != Entity.Null)
                    {
                        var e = commandBuffer.Instantiate(index, bulletPrefab);

                        commandBuffer.SetComponent(index, e, position);
                        commandBuffer.SetComponent(index, e, rotation);

                        var vel = new Velocity
                        {
                            Value = math.mul(rotation.Value, new float3(0, bulletVelocity, 0)).xy
                        };

                        commandBuffer.SetComponent(index, e,
                                                   new PlayerIdComponentData {
                            PlayerId = playerIdData.PlayerId
                        });
                        commandBuffer.SetComponent(index, e, vel);
                    }
                    else
                    {
                        var e          = commandBuffer.CreateEntity(index, bulletSpawnArchetype);
                        var bulletData = default(BulletSnapshotData);
                        bulletData.tick = currentTick;
                        bulletData.SetRotationValue(rotation.Value);
                        bulletData.SetTranslationValue(position.Value);
                        // Offset bullets for debugging spawn prediction
                        //bulletData.SetTranslationValue(position.Value + new float3(0,10,0));
                        bulletData.SetPlayerIdComponentDataPlayerId(playerIdData.PlayerId, default(GhostSerializerState));
                        var bulletSnapshots = commandBuffer.SetBuffer <BulletSnapshotData>(index, e);
                        bulletSnapshots.Add(bulletData);
                    }

                    state.WeaponCooldown = currentTick + k_CoolDownTicksCount;
                }
                else if (canShoot)
                {
                    state.WeaponCooldown = 0;
                }
            }
Beispiel #11
0
        public unsafe void Execute(Entity entity, int index, ref Translation position, ref Rotation rotation, ref Velocity velocity,
                                   ref ShipStateComponentData state, [ReadOnly] ref PlayerIdComponentData playerIdData)
        {
            var snapshot = snapshotFromEntity[entity];
            ShipSnapshotData snapshotData;

            snapshot.GetDataAtTick(currentTick, out snapshotData);

            var input = inputFromEntity[entity];

            // Iterate over last snapshot tick + 1 (we just applied the first one)
            // to the current tick + 1 and apply prediction
            for (uint tick = snapshotData.Tick + 1; tick != currentTick + 1; ++tick)
            {
                // Get input at the tick we're predicting
                ShipCommandData inputData;
                input.GetDataAtTick(tick, out inputData);

                // For movement, apply the input so position/rotation will be adjusted
                state.State = inputData.thrust;

                if (inputData.left == 1)
                {
                    rotation.Value = math.mul(rotation.Value,
                                              quaternion.RotateZ(math.radians(-displacement * deltaTime)));
                }

                if (inputData.right == 1)
                {
                    rotation.Value = math.mul(rotation.Value,
                                              quaternion.RotateZ(math.radians(displacement * deltaTime)));
                }

                if (inputData.thrust == 1)
                {
                    float3 fwd = new float3(0, playerForce * deltaTime, 0);
                    velocity.Value += math.mul(rotation.Value, fwd).xy;
                }

                position.Value.xy += velocity.Value * deltaTime;

                if (inputData.shoot != 0)
                {
                    // Only spawn a bullet if this is a tick we have not simulated before
                    if (lastPredictedSpawn[0] == 0 || SequenceHelpers.IsNewer(tick, lastPredictedSpawn[0] + k_CoolDownTicksCount))
                    {
                        var e          = commandBuffer.CreateEntity(index, bulletSpawnArchetype);
                        var bulletData = default(BulletSnapshotData);
                        bulletData.tick = tick;
                        bulletData.SetRotationValue(rotation.Value);
                        bulletData.SetTranslationValue(position.Value);
                        // Offset bullets for debugging spawn prediction
                        //bulletData.SetTranslationValue(position.Value + new float3(0,10,0));
                        bulletData.SetPlayerIdComponentDataPlayerId(playerIdData.PlayerId);
                        var bulletSnapshots = commandBuffer.SetBuffer <BulletSnapshotData>(index, e);
                        bulletSnapshots.Add(bulletData);
                        lastPredictedSpawn[0] = tick;
                    }
                }
            }
        }
 public DynamicBuffer <T> SetBuffer <T>() where T : struct, IBufferElementData
 {
     return(_ecb.SetBuffer <T>(_jobIndex, _entity));
 }