The piece for puzzle game example.
Inheritance: MapObject
Beispiel #1
0
 void ServerOrSingle_DestroyPuzzles()
 {
     foreach (Entity entity in Map.Instance.Children)
     {
         JigsawPuzzlePiece piece = entity as JigsawPuzzlePiece;
         if (piece != null)
         {
             piece.SetShouldDelete();
         }
     }
 }
Beispiel #2
0
        void ServerOrSingle_CreatePiece(Vec2i index)
        {
            JigsawPuzzlePiece piece = (JigsawPuzzlePiece)Entities.Instance.Create(
                "JigsawPuzzlePiece", Map.Instance);

            piece.ServerOrSingle_SetIndex(index);

            //calculate position
            {
                Rect area = GetGameArea();
                area.Expand(-.5f);
                Rect exceptArea = GetDestinationArea();
                exceptArea.Expand(1);

                float x = 0;
                float y = 0;

                bool free = false;
                do
                {
                    free = true;

                    EngineRandom random = World.Instance.Random;
                    x = area.Minimum.X + random.NextFloat() * area.Size.X;
                    y = area.Minimum.Y + random.NextFloat() * area.Size.Y;

                    if (exceptArea.IsContainsPoint(new Vec2(x, y)))
                    {
                        free = false;
                    }

                    Bounds checkBounds = new Bounds(
                        new Vec3(x - .5f, y - .5f, -100),
                        new Vec3(x + .5f, y + .5f, 100));

                    Map.Instance.GetObjects(checkBounds, delegate(MapObject mapObject)
                    {
                        JigsawPuzzlePiece p = mapObject as JigsawPuzzlePiece;
                        if (p != null)
                        {
                            free = false;
                        }
                    });
                } while(!free);

                piece.Position = new Vec3(x, y, .1f);
            }

            piece.PostCreate();
        }
Beispiel #3
0
        void RenderPieceSelectionBorder( JigsawPuzzlePiece piece, bool selected )
        {
            Camera camera = RendererWorld.Instance.DefaultCamera;

            if( selected )
                camera.DebugGeometry.Color = new ColorValue( 0, 1, 0 );
            else
                camera.DebugGeometry.Color = new ColorValue( 1, 1, 0 );

            camera.DebugGeometry.AddBounds( piece.MapBounds );
        }
Beispiel #4
0
        protected override bool OnMouseUp( EMouseButtons button )
        {
            //If atop openly any window to not process
            if( Controls.Count != 1 )
                return base.OnMouseUp( button );

            switch( button )
            {
            case EMouseButtons.Left:
                if( tryingToMovePiece != null )
                {
                    UpdateShouldSendMovingPiecePositionToServer( true );

                    if( EntitySystemWorld.Instance.IsServer() || EntitySystemWorld.Instance.IsSingle() )
                        tryingToMovePiece.ServerOrSingle_MoveFinish();
                    else
                        tryingToMovePiece.Client_MoveTryToFinish();

                    tryingToMovePiece = null;

                    return true;
                }
                break;
            }

            return base.OnMouseUp( button );
        }
Beispiel #5
0
        protected override bool OnMouseDown( EMouseButtons button )
        {
            //If atop openly any window to not process
            if( Controls.Count != 1 )
                return base.OnMouseDown( button );

            switch( button )
            {
            case EMouseButtons.Left:
                {
                    tryingToMovePiece = GetPieceByCursor();
                    if( tryingToMovePiece != null )
                    {
                        if( EntitySystemWorld.Instance.IsServer() )
                        {
                            //server
                            GameNetworkServer server = GameNetworkServer.Instance;
                            tryingToMovePiece.Server_MoveBegin( server.UserManagementService.ServerUser );
                        }
                        else if( EntitySystemWorld.Instance.IsClientOnly() )
                        {
                            //client
                            tryingToMovePiece.Client_MoveTryToBegin();
                        }
                        else
                        {
                            //single mode
                            tryingToMovePiece.Single_MoveBegin();
                        }

                        Vec2 cursorPosition;
                        GetGameAreaCursorPosition( out cursorPosition );
                        tryingToMovePieceOffset = tryingToMovePiece.Position.ToVec2() - cursorPosition;
                        return true;
                    }
                }
                break;
            }

            return base.OnMouseDown( button );
        }
Beispiel #6
0
        public void ServerOrSingle_MoveFinish()
        {
            if (!serverOrSingle_Moving)
            {
                return;
            }

            serverOrSingle_Moving     = false;
            server_movingByUser       = null;
            serverOrSingle_movingTime = 0;

            bool putToDestinationPlace = false;

            //check for destination place
            {
                Vec2 destination = JigsawPuzzleManager.Instance.GetPieceDestinationPosition(Index);
                if (Math.Abs(Position.X - destination.X) < .2f &&
                    Math.Abs(Position.Y - destination.Y) < .2f)
                {
                    //move to destination place

                    //"Position" will be send to clients from JigsawPuzzlePiece.OnSetTransform() method
                    Position = new Vec3(destination.X, destination.Y, 0);

                    putToDestinationPlace = true;
                }
            }

            //check for complete puzzle
            bool completePuzzle = true;

            {
                foreach (Entity entity in Map.Instance.Children)
                {
                    JigsawPuzzlePiece piece = entity as JigsawPuzzlePiece;
                    if (piece != null)
                    {
                        Vec2 destination = JigsawPuzzleManager.Instance.GetPieceDestinationPosition(
                            piece.Index);
                        if (Math.Abs(piece.Position.X - destination.X) > .01f ||
                            Math.Abs(piece.Position.Y - destination.Y) > .01f)
                        {
                            completePuzzle = false;
                            break;
                        }
                    }
                }
            }

            if (EntitySystemWorld.Instance.IsServer())
            {
                SendDataWriter writer = BeginNetworkMessage(typeof(JigsawPuzzlePiece),
                                                            (ushort)NetworkMessages.MoveFinishToClient);
                writer.Write(putToDestinationPlace);
                writer.Write(completePuzzle);
                EndNetworkMessage();
            }

            if (EntitySystemWorld.Instance.IsServer() || EntitySystemWorld.Instance.IsSingle())
            {
                //play sounds
                if (putToDestinationPlace)
                {
                    ClientOrSingle_SoundPlay("JigsawPuzzleGame\\PutToDestinationPlace.ogg");
                }
                if (completePuzzle)
                {
                    ClientOrSingle_SoundPlay("JigsawPuzzleGame\\CompletePuzzle.ogg");
                }
            }
        }