コード例 #1
0
ファイル: Bubbles.cs プロジェクト: Qwerty042/LocalGameJam3.1
        public void Update(GameTime gameTime)
        {
            foreach (KeyValuePair <string, Pipe> pipe in pipes)
            {
                double elapsedTime = gameTime.TotalGameTime.TotalSeconds - pipe.Value.LastBubbleSpawn;
                if (pipe.Value.FlowVelocity != 0)
                {
                    if (elapsedTime > (1 / (pipe.Value.FlowVelocity * bubbleDensity)))
                    {
                        Bubble bubble = new Bubble(pipe.Value.Waypoints[0], pipe.Key, 1, new Vector2(rand.Next(-offsetRange, offsetRange + 1), rand.Next(-offsetRange, offsetRange + 1)), pipe.Value.BubbleColor);
                        BubblesList.Add(bubble);
                        pipe.Value.LastBubbleSpawn = gameTime.TotalGameTime.TotalSeconds;
                    }
                }
            }

            List <Bubble> deadBubbles = new List <Bubble>();

            foreach (Bubble bubble in BubblesList)
            {
                Pipe    pipe      = pipes[bubble.PathName];
                Vector2 direction = Vector2.Normalize(pipe.Waypoints[bubble.NextWaypointIndex] - bubble.Pos);
                bubble.Pos += direction * pipe.FlowVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (bubble.PathName == "turbineWaterRightPath")
                {
                    if ((bubble.Pos.X > 1030 && bubble.Pos.X < 1120) || (bubble.Pos.X > 2070 && bubble.Pos.X < 2160))
                    {
                        bubble.BubbleColor = new Color(0, 0, 0, 0);
                    }
                    else
                    {
                        bubble.BubbleColor = waterColor;
                    }
                }

                if (Math.Abs(Vector2.Dot(direction, Vector2.Normalize(pipe.Waypoints[bubble.NextWaypointIndex] - bubble.Pos)) + 1) < 0.1f)
                {
                    bubble.Pos = pipe.Waypoints[bubble.NextWaypointIndex];
                    bubble.NextWaypointIndex++;
                    if (bubble.NextWaypointIndex >= pipe.Waypoints.Length)
                    {
                        deadBubbles.Add(bubble);
                    }
                }
            }

            foreach (var bubble in deadBubbles)
            {
                BubblesList.Remove(bubble);
            }
        }
コード例 #2
0
ファイル: Bubbles.cs プロジェクト: Qwerty042/LocalGameJam3.1
        public Bubbles()
        {
            pipes = new Dictionary <string, Pipe>
            {
                { "pumpLeftPath", new Pipe(pumpLeftPath, 400f, waterColor) },
                { "pumpRightPath", new Pipe(pumpRightPath, 400f, waterColor) },
                { "coreSteamLeftPath", new Pipe(coreSteamLeftPath, 400f, steamColor) },
                { "coreSteamRightPath", new Pipe(coreSteamRightPath, 400f, steamColor) },
                { "turbineSteamLeftPath", new Pipe(turbineSteamLeftPath, 400f, steamColor) },
                { "turbineSteamRightPath", new Pipe(turbineSteamRightPath, 400f, steamColor) },
                { "turbineWaterLeftPath", new Pipe(turbineWaterLeftPath, 400f, waterColor) },
                { "turbineWaterRightPath", new Pipe(turbineWaterRightPath, 400f, waterColor) },
            };
            BubblesList = new List <Bubble>();

            //start pipes with some bubbles in them already
            for (int i = 0; i < 175; i++)
            {
                foreach (KeyValuePair <string, Pipe> pipe in pipes)
                {
                    Bubble bubble = new Bubble(pipe.Value.Waypoints[0], pipe.Key, 1, new Vector2(rand.Next(-offsetRange, offsetRange + 1), rand.Next(-offsetRange, offsetRange + 1)), pipe.Value.BubbleColor);
                    BubblesList.Add(bubble);
                }

                List <Bubble> deadBubbles = new List <Bubble>();

                foreach (Bubble bubble in BubblesList)
                {
                    Pipe    pipe      = pipes[bubble.PathName];
                    Vector2 direction = Vector2.Normalize(pipe.Waypoints[bubble.NextWaypointIndex] - bubble.Pos);
                    bubble.Pos += direction * pipe.FlowVelocity * 0.04f;

                    if (bubble.PathName == "turbineWaterRightPath")
                    {
                        if ((bubble.Pos.X > 1030 && bubble.Pos.X < 1120) || (bubble.Pos.X > 2070 && bubble.Pos.X < 2160))
                        {
                            bubble.BubbleColor = new Color(0, 0, 0, 0);
                        }
                        else
                        {
                            bubble.BubbleColor = waterColor;
                        }
                    }

                    if (Math.Abs(Vector2.Dot(direction, Vector2.Normalize(pipe.Waypoints[bubble.NextWaypointIndex] - bubble.Pos)) + 1) < 0.1f)
                    {
                        bubble.Pos = pipe.Waypoints[bubble.NextWaypointIndex];
                        bubble.NextWaypointIndex++;
                        if (bubble.NextWaypointIndex >= pipe.Waypoints.Length)
                        {
                            deadBubbles.Add(bubble);
                        }
                    }
                }

                foreach (var bubble in deadBubbles)
                {
                    BubblesList.Remove(bubble);
                }
            }
        }