コード例 #1
0
ファイル: TowerSprite.cs プロジェクト: emikos/TSAUMDHG
        public ProjectileSprite Update(GameTime gameTime, Rectangle clientBounds, List<UnitSprite> spriteList, List<MapSprite> pathList)
        {
            targetSprite = null;
            ProjectileSprite projectile = null;
            int maxDistance = 0;
            float result;
            Vector2 spriteVector;
            Vector2 normalizedPosition = new Vector2(0, 0);
            int targetIndex = 0;
            //normalizedPosition.Normalize();

            //if(reloadRate % gameTime.
            if (reloadTimer <= 0.0f)
            {
                for (int index = 0; index < spriteList.Count; index++)
                {
                    result = Vector2.Distance(position, spriteList[index].GetPosition);

                    if (result <= range && spriteList[index].GetPosition.X > 0 && spriteList[index].GetPosition.Y > 0)
                    {
                        for (int counter = maxDistance; counter < pathList.Count; counter++)
                        {
                            if (!spriteList[index].IsDead() &&
                                pathList[counter].GetPosition.X == (spriteList[index].GetPosition.X - (spriteList[index].GetPosition.X % pathList[counter].GetFrameSize.X)) &&
                                pathList[counter].GetPosition.Y == (spriteList[index].GetPosition.Y - (spriteList[index].GetPosition.Y % pathList[counter].GetFrameSize.Y)))
                            {
                                maxDistance = counter;
                                targetSprite = spriteList[index];
                                targetIndex = index;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                reloadTimer -= gameTime.ElapsedGameTime.Milliseconds;
            }

            //Update animation frame
            if (targetSprite != null)
            {
                spriteVector = targetSprite.GetPosition;
                spriteVector.X += (targetSprite.GetFrameSize.X / 4);
                //spriteVector.Y += (targetSprite.GetFrameSize.Y / 4);
                normalizedPosition = position;
                //spriteVector.Normalize();
                normalizedPosition.Normalize();

                //float perpDot = normalizedPosition.X * spriteVector.Y - normalizedPosition.Y * spriteVector.X;
                //float dotproduct;// = Vector2.Dot(spriteVector, normalizedPosition);
                //double angle = (float)Math.Atan2(perpDot, Vector2.Dot(normalizedPosition, spriteVector));
                //-(float)Math.Atan2((normalizedPosition.X - spriteVector.X), (normalizedPosition.Y - spriteVector.Y));
                //double angle = Math.Acos((float)dotproduct);
                //position.Normalize();
                //v2.Normalize();
                //float dotproduct = Vector2.Dot(spriteVector, position);
                //double angle = Math.Acos((float)dotproduct);
                //float angle = -(float)Math.Atan2((position.X - spriteVector.X), (position.Y - spriteVector.Y));
                float angle = GetDegreeToTarget(position.X + ((frameSize.X) / 8),
                                                position.Y,
                                                spriteVector.X,
                                                spriteVector.Y);

                if (angle < 0)
                {
                    angle = 360 + angle;
                }

                timeSinceLastFrame = 0;

                result = angle / turrentAngle;
                currentFrame.X = (int)Math.Round(result + 1);
                //++currentFrame.X;
                if (currentFrame.X >= base.GetSheetSize.X)
                {
                    currentFrame.X = 0;
                }

                projectile = new ProjectileSprite(projectileTexture,
                    new Vector2(this.position.X + ((frameSize.X) / 8), this.position.Y),
                    new Point(0, 0), new Point(0, 0), Point.Zero, new Point(4, 0), new Vector2(20, 20),
                    Color.White, spriteVector, 50, position, targetIndex, angle, 1f, damage, targetSprite.GetId());

                //if (reloadTimer > 0)
                //{
                //    currentFrame.X = 0;
                //}

                reloadTimer = reloadRate;
            }
            else if (reloadTimer > (reloadRate - 100))
            {
                currentFrame.X = 0;
            }

            return projectile;
            //base.Update(gameTime, clientBounds);
        }
コード例 #2
0
ファイル: SpriteManager.cs プロジェクト: emikos/TSAUMDHG
        protected void UpdateSprites(GameTime gameTime)
        {
            double output = 0;
            Double.TryParse((gameTime.ElapsedGameTime.TotalSeconds).ToString(), out output);
            ProjectileSprite projectile;

            if (releaseTime > unitRelease)
            {
                float scale = (float)rand.NextDouble();

                scale = scale / 2;
                if (scale < 0.25f)
                {
                    scale = 0.25f;
                }

                int speed = (int)Math.Round(1.2 / scale);

                unit = new UnitSprite(
                    unitTextureList["unit"],
                    new Vector2(pathList[0].GetPosition.X, pathList[0].GetPosition.Y),
                    new Point((int)Math.Floor(177 * .35f), (int)Math.Floor(139 * .35f)), Point.Zero, new Point(0, 0),
                    new Point(1, 2), new Vector2(speed, speed), 120, new Vector2(177 / 2, 139 / 2),
                    new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble()),
                    scale, (int)Math.Round(400 * scale), (int)Math.Round(50 * scale), rand.Next(), new Point(0, (int)Math.Round((double)mapList[0].GetFrameSize.Y / 4) * -1),
                    healthTextureList);
                spriteList.Add(unit);

                releaseTime = 0f;
            }
            else
            {
                releaseTime += (float)output;
            }

            for (int counter = 0; counter < spriteList.Count; counter++)
            {
                spriteList[counter].Update(gameTime, Game.Window.ClientBounds, pathList);
                if (spriteList[counter].IsDone())
                {
                    unitsMissed += 1;
                    spriteList.Remove(spriteList[counter]);
                }
            }

            foreach (TowerSprite t in towerList)
            {
                projectile = t.Update(gameTime, Game.Window.ClientBounds, spriteList, pathList);

                if (projectile != null)
                {
                    projectileList.Add(projectile);
                }
            }
            foreach (ProjectileSprite p in projectileList)
            {
                p.Update(gameTime);
                if (p.GetPosition.Equals(p.target))
                {
                    if (p.GetTargetIndex() < spriteList.Count &&
                        p.GetTargetId() == spriteList[p.GetTargetIndex()].GetId())
                    {
                        spriteList[p.GetTargetIndex()].Hit(p.GetDamage());
                        if (spriteList[p.GetTargetIndex()].IsDead())
                        {
                            credits += spriteList[p.GetTargetIndex()].points;
                            TextId++;
                            textList.Add(TextId.ToString(), new TextSprite(fontList["arial"], spriteList[p.GetTargetIndex()].GetPosition, new Vector2(0, -2), 10, spriteList[p.GetTargetIndex()].points.ToString(), Color.White));
                            spriteList.Remove(spriteList[p.GetTargetIndex()]);
                        }
                    }

                    removeProjectileList.Add(p);
                }
            }

            foreach (ProjectileSprite p in removeProjectileList)
            {
                acidHit.Play();
                projectileList.Remove(p);
            }

            removeProjectileList.Clear();

            textList["credits"].SetText("Credits: " + credits);
            textList["escaped"].SetText("Escaped: " + unitsMissed);

            battleMenuSprite.Update(gameTime, Game.Window.ClientBounds);

            // Update player
            if (!battleMenuSprite.IsExtended() && !battleMenuSprite.IsScrolling())
            {
                if (battleMenuSprite.activeSelection >= 0)
                {
                    TowerSprite tower = battleMenuSprite.towerList[battleMenuSprite.activeSelection];
                    player.frameSize = tower.frameSize;
                    if (player.rangeSprite == null || !player.rangeSprite.IsSameRange(tower.range))
                    {
                        player.SetRange(tower.CloneTower());
                        player.collisionOffset = tower.collisionOffset;
                    }

                    player.Update(gameTime, Game.Window.ClientBounds, pathList);
                }
                else if (!player.towerActive)
                {
                    player.ResetFrameSize();
                    player.Update(gameTime, Game.Window.ClientBounds, pathList);
                    if (towerMenuSprite.IsExtended())
                    {
                        towerMenuSprite.active = true;
                    }
                    towerMenuSprite.Update(gameTime, Game.Window.ClientBounds);
                }
                else
                {
                    towerMenuSprite.Update(gameTime, Game.Window.ClientBounds);
                }

            }
            else if (battleMenuSprite.IsExtended() && !battleMenuSprite.IsScrolling() && battleMenuSprite.activeSelectionCursor.RequestTower())
            {
                battleMenuSprite.activeSelection = battleMenuSprite.selectedTower;
            }

            for (int counter = 0; counter < textList.Count; counter++)
            {
                textList.ElementAt(counter).Value.Update(gameTime, Game.Window.ClientBounds);

                if (textList.ElementAt(counter).Value.IsGone())
                {
                    textList.Remove(textList.ElementAt(counter).Key);
                }
            }
        }