コード例 #1
0
        public MapLayer(int[] map, int width, int height, int layer)
        {
            Layer      = layer;
            Map        = new int[width, height];
            Bouncers   = new List <Node> [width, height];
            Walls      = new List <Rail> [width, height];
            Water      = new List <Rectangle> [width, height];
            Teleports  = new List <KeyValuePair <Rectangle, List <Rectangle> > > [width, height];
            Elevations = new Elevation[width, height];
            var i = 0;

            for (int r = 0; r < height; r++)
            {
                for (var c = 0; c < width; c++)
                {
                    Map[c, r]        = map[i];
                    Bouncers[c, r]   = WorldHelpers.AddBouncerNodes(WorldHelpers.GetBouncers(map[i]), layer);
                    Walls[c, r]      = WorldHelpers.AddWallRails(WorldHelpers.GetLineSegments(map[i]), layer);
                    Water[c, r]      = WorldHelpers.GetWater(map[i]);
                    Teleports[c, r]  = WorldHelpers.GetTeleports(map[i]);
                    Elevations[c, r] = WorldHelpers.GetElevation(map[i]);
                    if (Layer == 2 && Elevations[c, r] != Elevation.Flat)
                    {
                        throw new Exception("Yeah nah we can only have elevations on layer 1");
                    }
                    i++;
                }
            }
            Width  = width;
            Height = height;
        }
コード例 #2
0
 private void UpdateTileDefs()
 {
     Rails        = WorldHelpers.AddWallRails(WorldHelpers.GetLineSegments(CurrentTile), 1);
     SpecialRails = WorldHelpers.AddWallRails(WorldHelpers.GetLineSpecialSegments(CurrentTile, new Point(-1, -1)), 1);
     Bouncers     = WorldHelpers.AddBouncerNodes(WorldHelpers.GetBouncers(CurrentTile), 1);
     Water        = WorldHelpers.GetWater(CurrentTile);
     Teleporters  = WorldHelpers.GetTeleporters(CurrentTile);
     Elevation    = WorldHelpers.GetElevation(CurrentTile);
     Connections  = WorldHelpers.GetSideConnections(CurrentTile);
 }
コード例 #3
0
ファイル: Player.cs プロジェクト: pixelwhipped/MiniGolfMayhem
        public void Update(GameTime gameTime, Map map)
        {
            try
            {
                if (State == PlayerState.Done)
                {
                    return;
                }
                if (WaterPosition != null)
                {
                    return;
                }

                LastPosistion = Node.WorldPosition - Node.Velocity * 5;
                if (Teleporting && TeleportDelay > TimeSpan.Zero)
                {
                    TeleportDelay -= gameTime.ElapsedGameTime;
                    return;
                }
                if ((State == PlayerState.Ready || State == PlayerState.Bounced) && Velocity.Length() > 0.01f)
                {
                    State    = State == PlayerState.Bounced ? PlayerState.Bounced : PlayerState.Started;
                    IdleTime = TimeSpan.Zero;
                }
                if ((State == PlayerState.Started || State == PlayerState.Bounced) && Velocity.Length() < 0.09f)
                {
                    IdleTime += gameTime.ElapsedGameTime;
                }
                else
                {
                    IdleTime = TimeSpan.Zero;
                }
                if (State == PlayerState.Started || State == PlayerState.Bounced)
                {
                    GolfBall.Update(gameTime);
                }
                if (IdleTime > new TimeSpan(0, 0, 0, 3))
                {
                    State    = PlayerState.Finished;
                    Velocity = Vector2.Zero;
                }
                if (State == PlayerState.Ready)
                {
                    Power.Update(gameTime.ElapsedGameTime);
                    return;
                }

                if (map.GetTile(this, 1) == 0 && map.GetTile(this, 2) == 0) //Out of map
                {
                    Node         = new Node(map.StartWorldCenter, 2.5f, 1, Vector2.Zero, Vector2.Zero, map.StartLayer);
                    State        = PlayerState.Finished;
                    PreviousTile = map.GetPlayerTile(this);
                    LastTile     = PreviousTile;
                }


                OnTile = map.GetTile(this, Layer);


                CurrentTile = map.GetPlayerTile(this);
                var tileToWorldTranslation = new Point(CurrentTile.X * map.TileSet.TileWidth, CurrentTile.Y * map.TileSet.TileWidth);
                var pointOnTile            = new Vector2(Node.WorldPosition.X - tileToWorldTranslation.X, Node.WorldPosition.Y - tileToWorldTranslation.Y);

                var walls = map.GetWalls(this);
                if (OnTile == 53 || OnTile == 54 || OnTile == 62 || OnTile == 63)
                {
                    walls =
                        walls.Union(
                            WorldHelpers.AddWallRails(
                                WorldHelpers.GetLineSpecialSegments(OnTile,
                                                                    new Point((int)(Node.WorldPosition.X - tileToWorldTranslation.X),
                                                                              (int)(Node.WorldPosition.Y - tileToWorldTranslation.Y))), Layer)).ToList();
                }
                Node.Update(gameTime);

                walls = map.GetWalls(this);
                if (OnTile == 53 || OnTile == 54 || OnTile == 62 || OnTile == 63)
                {
                    walls =
                        walls.Union(
                            WorldHelpers.AddWallRails(
                                WorldHelpers.GetLineSpecialSegments(OnTile,
                                                                    new Point((int)(Node.WorldPosition.X - tileToWorldTranslation.X),
                                                                              (int)(Node.WorldPosition.Y - tileToWorldTranslation.Y))), Layer)).ToList();
                }

                if (map.EndLayer == Layer && Vector2.Distance(map.EndWorldCenter, Node.WorldPosition) < 5 + Node.Radius)
                {
                    if (Node.Velocity.Length() < 3.5f)
                    {
                        Node  = new Node(map.StartWorldCenter, 2.5f, 1, Vector2.Zero, Vector2.Zero, map.StartLayer);
                        State = PlayerState.Done;
                        Game.Sounds.Hole.Play();
                    }
                }

                #region Elevations

                LastImidiateLayer = Layer;
                // Layer = WorldHelpers.LayerMod()
                if (LastTile != CurrentTile)
                {
                    BouncePlayed =
                        !(OnTile == 53 && !(pointOnTile.Y > 23) || OnTile == 54 && !(pointOnTile.Y > 72) ||
                          OnTile == 62 && !(pointOnTile.X > 72) || OnTile == 63 && !(pointOnTile.X < 23));
                    PreviousLayer = LastLayer;
                    LastLayer     = Layer;
                    //Need to test elevation vertical horizontal
                    if (Layer == 1 && map.GetElevation(LastTile) != Elevation.Flat)
                    {
                        if (WorldHelpers.IsTopLevel(map.Layer2.GetTile(CurrentTile.X, CurrentTile.Y)) &&
                            PreviousLayer != 2)
                        {
                            Layer = 2;
                        }
                    }
                    else if (Layer == 2 && map.GetElevation(CurrentTile) != Elevation.Flat)
                    {
                        Layer = 1;
                    }

                    PreviousTile = LastTile;
                    LastTile     = CurrentTile;
                }
                if (!BouncePlayed && (OnTile == 53 || OnTile == 54 || OnTile == 62 || OnTile == 63))
                {
                    switch (OnTile)
                    {
                    case 53:
                    {
                        if (pointOnTile.Y < 23)
                        {
                            BouncePlayed = true;
                            Game.Sounds.DropBounce.Play();
                        }
                        break;
                    }

                    case 54:
                    {
                        if (pointOnTile.Y > 72)
                        {
                            BouncePlayed = true;
                            Game.Sounds.DropBounce.Play();
                        }
                        break;
                    }

                    case 62:
                    {
                        if (pointOnTile.X > 72)
                        {
                            BouncePlayed = true;
                            Game.Sounds.DropBounce.Play();
                        }
                        break;
                    }

                    case 63:
                    {
                        if (pointOnTile.X < 23)
                        {
                            BouncePlayed = true;
                            Game.Sounds.DropBounce.Play();
                        }
                        break;
                    }
                    }
                }
                if (Layer == 1)
                {
                    Node.Acceleration += WorldHelpers.GetForce(map.Layer1.GetTile(CurrentTile.X, CurrentTile.Y),
                                                               pointOnTile);
                }


                #endregion

                Node.DetectCollisions(map.GetBouncers(this), walls, tileToWorldTranslation);
                Node.DetectCollisions(map.Players.Where(p => p.State != PlayerState.Done));
                if (
                    map.GetWater(this)
                    .Any(
                        r =>
                        r.Contains(pointOnTile)))
                {
                    WaterPosition            = Node.WorldPosition;
                    Sprites.Splash.OnFinish += SplashFinished;
                    Sprites.Splash.Animation = AnimationState.Play;
                    Game.Sounds.Splash.Play();
                }
                var ballTileRect = new Rectangle((int)(Node.WorldPosition.X - tileToWorldTranslation.X),
                                                 (int)(Node.WorldPosition.Y - tileToWorldTranslation.Y), (int)GolfBall.Width, (int)GolfBall.Height);
                var rects = map.GetTeleports(this);
                if (!Teleporting && rects.Any())
                {
                    var exits = rects.Where(r => r.Key.Intersects(ballTileRect)).SelectMany(r => r.Value).ToList();
                    if (exits.Any())
                    {
                        var v = Golf.Random.Next(exits.Count);
                        var r = exits[v];
                        //  r = new Rectangle(39, 46, 6, 10);
                        Teleport = new Rectangle(r.X + tileToWorldTranslation.X, r.Y + tileToWorldTranslation.Y,
                                                 r.Width,
                                                 r.Height);

                        Node.Velocity      = WorldHelpers.GetTeleporterVelocity(68, r.Center); //will need to get tile type
                        Node.WorldPosition = new Vector2(Teleport.Center.X, Teleport.Center.Y);
                        TeleportDelay      = new TimeSpan(0, 0, 0, 0, 500);
                        Teleporting        = true;
                        Game.Sounds.Bouncing.Play();
                    }
                }
                else
                {
                    Teleporting =
                        Teleport.Intersects(new Rectangle(ballTileRect.X + tileToWorldTranslation.X,
                                                          ballTileRect.Y + tileToWorldTranslation.Y, (int)GolfBall.Width, (int)GolfBall.Height));
                }
            }
            catch (Exception)
            {
                //Somthing bad happened reset
                Node =
                    new Node(
                        new Vector2(Math.Max(map.Start.X, 0) * map.TileSet.TileWidth,
                                    Math.Max(map.Start.Y, 0) * map.TileSet.TileHeight) +
                        WorldHelpers.GetStartOffset(map.StartSide), 2.5f, 1, Vector2.Zero, Vector2.Zero, map.StartLayer);
                State        = PlayerState.Finished;
                PreviousTile = map.GetPlayerTile(this);
                LastTile     = PreviousTile;
            }
        }