private void OnReceivedPacket(Packet packet)
        {
            // Record.Log($"Receiving packet: {packet.TypeCase}");
            switch (packet.TypeCase)
            {
            case Packet.TypeOneofCase.UpdateObject:
                ObjectUpdated?.Invoke(packet.UpdateObject);
                break;

            case Packet.TypeOneofCase.UpdatePosition:
                ObjectMoved?.Invoke(packet.UpdatePosition);
                break;

            case Packet.TypeOneofCase.UpdateRotation:
                ObjectRotated?.Invoke(packet.UpdateRotation);
                break;

            case Packet.TypeOneofCase.DestroyObject:
                ObjectDestroyed?.Invoke(packet.DestroyObject);
                break;

            case Packet.TypeOneofCase.CreatePlayer:
                PlayerCreated?.Invoke(packet.CreatePlayer);
                Record.Log($"Creating player {packet.CreatePlayer}");
                break;

            default:
                // TODO: https://docs.microsoft.com/en-us/dotnet/standard/exceptions/how-to-create-user-defined-exceptions
                Record.Log($"Unimplemented packet handler ! {packet.TypeCase}", LogLevel.Error);
                break;
            }
        }
    public override void OnEvent(ObjectMoved evnt)
    {
        if (evnt.Entity.IsOwner)
        {
            // Interactive objects respond to events

            if (evnt.Entity.GetComponent <InteractiveObjects>())
            {
                i = evnt.Entity.GetComponent <InteractiveObjects>();
            }

            if (evnt.Push)
            {
                i.SetVelocity(evnt.Velocity);
                evnt.Entity.GetState <IDynamicObjectState>().Held = false;
            }
            else
            {
                if (evnt.Hold)
                {
                    i.Hold(evnt.Position, evnt.Rotation);
                    evnt.Entity.GetState <IDynamicObjectState>().Held = true;
                }
                else
                {
                    i.Drop(evnt.Velocity);
                    evnt.Entity.GetState <IDynamicObjectState>().Held = false;
                }
            }
        }
    }
    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        collisionPoint = hit.point;



        // PUSH NON KINEMATIC RIGIDBODYS



        Rigidbody rb = hit.transform.GetComponent <Rigidbody>();


        float pushPower = 2.0f;


        // no rigidbody
        if (rb == null || rb.isKinematic)
        {
            return;
        }
        // We dont want to push objects below us
        if (hit.moveDirection.y < -0.3)
        {
            return;
        }
        // Calculate push direction from move direction,
        // we only push objects to the sides never up and down
        Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);

        // If you know how fast your character is trying to move,
        // then you can also multiply the push velocity by that.
        // Apply the push


        // if it is a bolt entity
        if (rb.GetComponent <BoltEntity>() != null)
        {
            if (BoltNetwork.IsServer)
            {
                rb.velocity = pushDir * pushPower;
            }
            else
            {
                var objMoved = ObjectMoved.Create(Bolt.GlobalTargets.OnlyServer);

                objMoved.Push     = true;
                objMoved.Entity   = rb.GetComponent <BoltEntity>();
                objMoved.Velocity = pushDir * pushPower;


                objMoved.Send();
            }
        }
        else
        {
            rb.velocity = pushDir * pushPower;
        }
    }
Example #4
0
 public EngineCamera(Viewport viewport)
     : base()
 {
     _aspectRatio = ((float)viewport.Width / (float)viewport.Height);
     _near = 0.1f;
     _far = 1000f;
     UpdateProjection();
     EngineCameraUpdate(Vector3.Zero, Position);
     ObjectHasMoved += new ObjectMoved(EngineCameraUpdate);
 }
Example #5
0
 public PlanePrimitive(Vector3 position, Vector2 size, Color color)
 {
     _position = position;
     _size = size;
     _color = color;
     UpdateVertices();
     _coveringType = PrimitiveCoveringType.Color;
     ObjectHasMoved += new ObjectMoved(UpdateVerticesFromEvents);
     ObjectHasRotated += new ObjectHasRotatedHandler(UpdateVerticesFromEvents);
 }
Example #6
0
 public PlanePrimitive(Vector3 position, Vector2 size)
 {
     _position = position;
     _size = size;
     _rotation = Vector3.Zero;
     _vertexBuffer = null;
     UpdateVertices();
     _coveringType = PrimitiveCoveringType.None;
     ObjectHasMoved += new ObjectMoved(UpdateVerticesFromEvents);
     ObjectHasRotated += new ObjectHasRotatedHandler(UpdateVerticesFromEvents);
 }
Example #7
0
    void SendDropEvent(BoltEntity ent, Vector3 velocity)
    {
        var objMoved = ObjectMoved.Create(Bolt.GlobalTargets.OnlyServer);

        objMoved.Hold = false;
        objMoved.Push = false;

        objMoved.Velocity = velocity;
        objMoved.Entity   = ent;

        objMoved.Send();
    }
Example #8
0
    // Events and their Overloads


    // Hold
    void SendHoldEvent(BoltEntity ent, Vector3 position, Quaternion rotation)
    {
        var objMoved = ObjectMoved.Create(Bolt.GlobalTargets.OnlyServer);

        objMoved.Hold = true;
        objMoved.Push = false;

        objMoved.Entity   = ent;
        objMoved.Position = position;
        objMoved.Rotation = rotation;

        objMoved.Send();
    }
        private void Simulation_LeftMouseButtonJustReleased(object sender, EventArgs e)
        {
            // Cannon fire
            if (SelectedObject is Cannon cannon && !objectMoving)
            {
                cannon.Fire();
            }

            if (objectMoving)
            {
                objectMoving = false;
                ObjectMoved?.Invoke(this, new EventArgs());
            }
        }
 public FloorObject this[int row, int column]
 {
     get { return(floorPlan[row, column]); }
     set
     {
         try
         {
             floorPlan[row, column] = value;
             ObjectMoved?.Invoke(this, new EventArgs());
         }
         catch
         {
             throw new IndexOutOfRangeException();
         }
     }
 }
Example #11
0
        private void GameObject_Moved(object sender, GameObject e)
        {
            if (e.CenterPolygonAbsolute.X + e.MaxRadiusObject < 0 - 1)
            {
                e.CenterPolygonAbsolute = new GamePoint(WightField + e.MaxRadiusObject, e.CenterPolygonAbsolute.Y);
            }
            if (e.CenterPolygonAbsolute.X - e.MaxRadiusObject > WightField + 1)
            {
                e.CenterPolygonAbsolute = new GamePoint(0 - e.MaxRadiusObject, e.CenterPolygonAbsolute.Y);
            }
            if (e.CenterPolygonAbsolute.Y + e.MaxRadiusObject < 0 - 1)
            {
                e.CenterPolygonAbsolute = new GamePoint(e.CenterPolygonAbsolute.X, HeightField + e.MaxRadiusObject - 1);
            }
            if (e.CenterPolygonAbsolute.Y - e.MaxRadiusObject > HeightField + 1)
            {
                e.CenterPolygonAbsolute = new GamePoint(e.CenterPolygonAbsolute.X, 0 - e.MaxRadiusObject);
            }

            ObjectMoved?.Invoke(this, e);
        }
Example #12
0
        /// <summary>
        /// Constructor.  Constructs map with the given terrain layer, determining width/height based on the width/height of that terrain layer.
        /// </summary>
        /// <remarks>
        /// Because of the way polymorphism works for custom classes in C#, the <paramref name="terrainLayer"/> parameter MUST be of type
        /// <see cref="ISettableMapView{IGameObject}"/>, rather than <see cref="ISettableMapView{T}"/> where T is a type that derives from or implements
        /// <see cref="IGameObject"/>.  If you need to use a map view storing type T rather than IGameObject, use the
        /// <see cref="CreateMap{T}(ISettableMapView{T}, int, Distance, uint, uint, uint)"/> function to create the map.
        /// </remarks>
        /// <param name="terrainLayer">The <see cref="ISettableMapView{IGameObject}"/> that represents the terrain layer for this map.  After the
        /// map has been created, you should use the <see cref="SetTerrain(IGameObject)"/> function to modify the values in this map view, rather
        /// than setting the values via the map view itself -- if you re-assign the value at a location via the map view, the
        /// <see cref="ObjectAdded"/>/<see cref="ObjectRemoved"/> events are NOT guaranteed to be called, and many invariants of map may not be properly
        /// enforced.</param>
        /// <param name="numberOfEntityLayers">Number of non-terrain layers for the map.</param>
        /// <param name="distanceMeasurement"><see cref="Distance"/> measurement to use for pathing/measuring distance on the map.</param>
        /// <param name="layersBlockingWalkability">Layer mask containing those layers that should be allowed to have items that block walkability.
        /// Defaults to all layers.</param>
        /// <param name="layersBlockingTransparency">Layer mask containing those layers that should be allowed to have items that block FOV.
        /// Defaults to all layers.</param>
        /// <param name="entityLayersSupportingMultipleItems">Layer mask containing those layers that should be allowed to have multiple objects at the same
        /// location on the same layer.  Defaults to no layers.</param>
        public Map(ISettableMapView <IGameObject> terrainLayer, int numberOfEntityLayers, Distance distanceMeasurement, uint layersBlockingWalkability = uint.MaxValue,
                   uint layersBlockingTransparency = uint.MaxValue, uint entityLayersSupportingMultipleItems = 0)
        {
            _terrain = terrainLayer;
            Explored = new ArrayMap <bool>(_terrain.Width, _terrain.Height);

            _entities = new LayeredSpatialMap <IGameObject>(numberOfEntityLayers, 1, entityLayersSupportingMultipleItems);

            LayersBlockingWalkability  = layersBlockingWalkability;
            LayersBlockingTransparency = layersBlockingTransparency;

            _entities.ItemAdded   += (s, e) => ObjectAdded?.Invoke(this, e);
            _entities.ItemRemoved += (s, e) => ObjectRemoved?.Invoke(this, e);
            _entities.ItemMoved   += (s, e) => ObjectMoved?.Invoke(this, e);

            if (layersBlockingTransparency == 1)             // Only terrain so we optimize
            {
                TransparencyView = new LambdaMapView <bool>(_terrain.Width, _terrain.Height, c => _terrain[c] == null || _terrain[c].IsTransparent);
            }
            else
            {
                TransparencyView = new LambdaMapView <bool>(_terrain.Width, _terrain.Height, FullIsTransparent);
            }

            if (layersBlockingWalkability == 1)             // Similar, only terrain blocks, so optimize
            {
                WalkabilityView = new LambdaMapView <bool>(_terrain.Width, _terrain.Height, c => _terrain[c] == null || _terrain[c].IsWalkable);
            }
            else
            {
                WalkabilityView = new LambdaMapView <bool>(_terrain.Width, _terrain.Height, FullIsWalkable);
            }

            _fov  = new FOV(TransparencyView);
            AStar = new AStar(WalkabilityView, distanceMeasurement);
        }
Example #13
0
    // Update is called once per frame
    void Update()
    {
        _mousePos = _mainCamera.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(_mousePos, Vector2.zero);

        if (!_movingItem)
        {
            if (hit.collider != null && hit.transform.CompareTag("Moveable"))
            {
                // Open grabby hand
                _grabbyHand.OpenHand();

                if (_previousCollider == null || !_active)
                {
                    _active = true;

                    _previousCollider = hit.collider;

                    _renderer = hit.collider.GetComponent <SpriteRenderer>();
                    _renderer.material.SetFloat("Vector1_5D8044E5", 1);

                    _highlightedItem = hit.collider.GetComponent <GridItem>();
                }
                else if (_previousCollider != hit.collider)
                {
                    _active           = true;
                    _previousCollider = hit.collider;

                    // Remove outline from previously selected item
                    _renderer.material.SetFloat("Vector1_5D8044E5", 0);

                    _renderer = hit.collider.GetComponent <SpriteRenderer>();
                    _renderer.material.SetFloat("Vector1_5D8044E5", 1);

                    _highlightedItem = hit.collider.GetComponent <GridItem>();
                }
            }
            else if (_active)
            {
                _active = false;
                _renderer.material.SetFloat("Vector1_5D8044E5", 0);
                _highlightedItem = null;

                _grabbyHand.Pointer();
            }
        }
        else
        {
            _highlightedItem.Move(_mousePos);
            _currentlyInValidPosition = IsPositionValid(_grid, _highlightedItem.X, _highlightedItem.Y, _highlightedItem.Width, _highlightedItem.Height) &&
                                        (_highlightedItem.X != _highlightedItem.InitialX || _highlightedItem.Y != _highlightedItem.InitialY);
            if (_currentlyInValidPosition)
            {
                _renderer.material.SetColor("Color_BC0A261F", Color.green);
            }
            else
            {
                _renderer.material.SetColor("Color_BC0A261F", Color.red);
            }
        }


        if (Input.GetMouseButtonDown(0))
        {
            // Check if a object is being hovered
            if (_highlightedItem != null)
            {
                // Check if we are currently holding something
                if (_movingItem)
                {
                    // Reset the object's position is placed in an invalid location
                    if (!_currentlyInValidPosition || OverMovingLimit())
                    {
                        _highlightedItem.ResetPosition();
                    }
                    else
                    {
                        // Update the collision grid then update the objects initial positions
                        // so that it will reset to this spot next time and can update the grid
                        // correctly if moved again
                        UpdateGrid(_grid, _highlightedItem);
                        _highlightedItem.RoundX = _highlightedItem.X;
                        _highlightedItem.RoundY = _highlightedItem.Y;

                        _grabbyHand.OpenHand();

                        _movedObjects.Add(_highlightedItem.Id);
                        _audioSource.PlayOneShot(PlaceSound);

                        ObjectMoved?.Invoke(_highlightedItem.Id, _highlightedItem.X, _highlightedItem.Y);
                    }

                    // Either way, drop it
                    _highlightedItem = null;
                    _renderer.material.SetColor("Color_BC0A261F", Color.white);
                }
                else
                {
                    // Close/Grab with grabby hand
                    _grabbyHand.CloseHand();

                    _audioSource.PlayOneShot(PickupSound);
                }

                _movingItem = !_movingItem;
            }
        }
    }