Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using XRpgLibrary.Controls;
- using XRpgLibrary.AI;
- //temporary until image is added to entity
- using Microsoft.Xna.Framework.Content;
- using XRpgLibrary.SpriteClasses;
- namespace XRpgLibrary.CharacterClasses
- {
- public class Enemy
- {
- static public bool wobblyTest = true;
- Matrix worldMatrix;
- Matrix projectionMatrix;
- BasicEffect basicEffect;
- VertexDeclaration vertexDeclaration;
- VertexBuffer vertexBuffer;
- /*
- BasicEffect shadowBasicEffect;
- VertexDeclaration shadowVertexDeclaration;
- VertexBuffer shadowVertexBuffer;
- int points = 4; //8
- //Texture2D texture;
- */
- enum PrimType
- {
- LineList,
- LineStrip,
- TriangleList,
- TriangleStrip
- };
- public enum Tilt
- {
- None,
- TiltForward,
- TiltForwardReverse,
- TiltBackward,
- TiltBackwardReverse,
- WobblyForward,
- WobblyForwardReverse,
- WobblyBackward,
- WobblyBackwardReverse,
- SideStepBackward,
- SideStepForward,
- Fall,
- Rise,
- SpinTo90,
- SpinTo180,
- SpinTo270,
- SpinTo360,
- FlyAwayTo90,
- FlyAwayTo180,
- FlyAwayTo270,
- FlyAwayTo360,
- SparkleTo90,
- SparkleTo180,
- SparkleTo270,
- SparkleTo360,
- HopUp1,
- HopDown1,
- HopUp2,
- HopDown2
- }
- //status hit
- public bool statusHitOn = false;
- public bool statusHitTurnOn = false;
- public string statusHit;
- public float statusHitInterval;
- public Vector2 statusHitPosition = new Vector2(0, 0);
- bool setPoints = false;
- public Tilt currentTilt = Tilt.None;
- //*public short[] edgeFaceList;
- public ushort[] edgeFaceList;
- public short[] shadowEdgeFaceList;
- private IndexBuffer indexBuffer;
- private IndexBuffer shadowIndexBuffer;
- VertexPositionColorTexture[] gpuVertices;
- VertexPositionColorTexture[] shadowVertices;
- Vector2[] startVertices;
- RasterizerState rasterizerState;
- GraphicsDevice graphics;
- GraphicsDevice shadowGraphics;
- Matrix viewMatrix = Matrix.CreateLookAt(
- new Vector3(0.0f, 0.0f, 1.0f),
- Vector3.Zero,
- Vector3.Up
- );
- //Settings
- int tiltForwardFrames = 5;
- int tiltBackFrames = 4;
- int backFrames = 9;
- int spinFrames = 4;
- int tiltSpeed = 20;
- int riseSpeed = 20;
- int spinSpeed = 20;
- int bits = 8;
- int screenWidth = 398;
- int screenHeight = 224;
- public bool powerSliverFlash = false;
- float powerSliverWait = 0f;
- TimeSpan transitionTimer = TimeSpan.FromSeconds(0.4);
- TimeSpan flyAwayTimer = TimeSpan.Zero;
- TimeSpan hopTimer = TimeSpan.Zero;
- public static TimeSpan flyAwayTime = TimeSpan.FromSeconds(.75);
- TimeSpan sparkleTimer = TimeSpan.Zero;
- public static TimeSpan sparkleTime = TimeSpan.FromSeconds(3.0); //4.0
- TimeSpan sideStepInterval = TimeSpan.FromSeconds(0.2); //0.2
- TimeSpan tiltForwardInterval = TimeSpan.FromSeconds(0.4); //0.2
- TimeSpan tiltSpinInterval = TimeSpan.FromSeconds(0.05); //0.05
- TimeSpan sparkleSpinInterval = TimeSpan.FromSeconds(0.2); //0.05
- TimeSpan tiltWobblyInterval = TimeSpan.FromSeconds(0.4); //0.2
- public TimeSpan tiltFallInterval = TimeSpan.FromSeconds(1.0); //0.5
- public TimeSpan Interval;
- float dipfactor = 4f;//4f;
- float sparkleStartSize = .2f;
- float maxSparkleWidth = 5f;
- float spinDip = 5f;
- public Entity[] targetedBy = new Entity[4];
- public bool[] hasFocus = new bool[4];
- public string[] playerAction = new string[4];
- public Color Glow = Color.White;
- Texture2D texture;
- public bool TakingAction = false;
- public bool TakingReaction = false;
- public static int flyAwayHeight = 100;
- public static float hopHeight = .10f;//.25;
- TimeSpan hopInterval = TimeSpan.FromSeconds(0.12); //0.2
- int sparkleAwayHeight = 200;
- public static int sparkleOffset = 70;
- public bool Visible = true;
- int mAlphaValue = 1;
- int mFadeIncrement = 3;
- double maxFadeDelay = .005; //.035
- double mFadeDelay = .000; //for some reason the source code sets this to the same as maxFadeDelay
- bool TiltFinished = false;
- float hitDelay = 0f;
- Texture2D image;
- public Shadow shadow;
- protected Entity entity;
- public AnimatedSprite sprite;
- private Vector2 position;
- public int positionZ;
- public Vector2 Position
- {
- get { return position; }
- }
- public Entity Entity
- {
- get { return entity; }
- }
- public int Width
- {
- get { return entity.Width*bits; }
- }
- public int Height
- {
- get { return entity.Height*bits; }
- }
- public Vector2 Origin
- {
- get { return new Vector2(Position.X + (Width / 2), Position.Y + (Height / 2)); }
- }
- public Enemy(Entity Entity, int PositionX, int PositionY, int PositionZ, GraphicsDevice Graphics)
- {
- graphics = Graphics;
- entity = Entity;
- image = entity.Sprite;
- //I subtracted 10 here to compensate for writing the reaciton stance //and now I took out the minus 10
- position = new Vector2(PositionX, PositionY);//-10);
- positionZ = PositionZ;
- Initialize();
- shadow = new Shadow(Entity, Graphics, gpuVertices);
- currentTilt = Tilt.Rise;
- transitionTimer = new TimeSpan(tiltFallInterval.Ticks /3 );
- TiltFinished = false;
- for (int i = 0; i < targetedBy.Count(); i++)
- targetedBy[i] = null;
- }
- public void MakeEnemyNormal()
- {
- currentTilt = Tilt.None;
- Entity.PowerPoints.SetCurrent(0);
- Visible = true;
- transitionTimer = TimeSpan.Zero;
- TiltFinished = true;
- ResetPosition();
- }
- private void Initialize()
- {
- InitializeTransform();
- InitializeEffect();
- InitializePoints();
- InitializeTriangleStrip();
- rasterizerState = new RasterizerState();
- rasterizerState.CullMode = CullMode.None;
- }
- private void InitializeEffect()
- {
- vertexDeclaration = new VertexDeclaration(new VertexElement[]
- {
- new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
- new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0),
- new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
- }
- );
- basicEffect = new BasicEffect(graphics);
- basicEffect.VertexColorEnabled = true;
- worldMatrix = Matrix.CreateTranslation(screenWidth / 2f,
- screenHeight / 2f, 0);
- basicEffect.World = worldMatrix;
- basicEffect.View = viewMatrix;
- basicEffect.Projection = projectionMatrix;
- }
- private void InitializeTransform()
- {
- viewMatrix = Matrix.CreateLookAt(
- new Vector3(0.0f, 0.0f, 1.0f),
- Vector3.Zero,
- Vector3.Up
- );
- projectionMatrix = Matrix.CreateOrthographicOffCenter(
- 0,
- screenWidth,
- screenHeight,
- 0,
- 1.0f, 1000.0f);
- }
- private void InitializePoints()
- {
- gpuVertices = new VertexPositionColorTexture[4];
- startVertices = new Vector2[4];
- startVertices[0] = new Vector2(Position.X - (screenWidth / 2), Position.Y - (screenHeight / 2));
- gpuVertices[0].Color = Color.White;
- gpuVertices[0].TextureCoordinate = new Vector2((float)(entity.startingX * bits) / (float)image.Width, (float)(entity.startingY * bits) / (float)image.Height);
- startVertices[1] = new Vector2(Position.X + entity.Width*bits - (screenWidth / 2), Position.Y - (screenHeight / 2));
- gpuVertices[1].Color = Color.White;
- gpuVertices[1].TextureCoordinate = new Vector2((float)(entity.startingX * bits + entity.Width * bits) / (float)image.Width, (float)(entity.startingY * bits) / (float)image.Height);
- startVertices[2] = new Vector2(Position.X - (screenWidth / 2), Position.Y + entity.Height*bits - (screenHeight / 2f));
- gpuVertices[2].Color = Color.White;
- gpuVertices[2].TextureCoordinate = new Vector2((float)(entity.startingX * bits) / (float)image.Width, (float)(entity.startingY * bits + entity.Height * bits) / (float)image.Height);
- startVertices[3] = new Vector2(Position.X + entity.Width*bits - (screenWidth / 2), Position.Y + entity.Height*bits - (screenHeight / 2f));
- gpuVertices[3].Color = Color.White;
- gpuVertices[3].TextureCoordinate = new Vector2((float)(entity.startingX * bits + entity.Width * bits) / (float)image.Width, (float)(entity.startingY * bits + entity.Height * bits) / (float)image.Height);
- gpuVertices[0].Position.X = startVertices[0].X + (((float)entity.Width * (float)bits / 3f));
- gpuVertices[1].Position.X = startVertices[1].X + (((float)-entity.Width * (float)bits / 3f));
- gpuVertices[2].Position.X = startVertices[2].X;
- gpuVertices[3].Position.X = startVertices[3].X;
- gpuVertices[0].Position.Y =startVertices[0].Y +(float)(entity.Height * bits);
- gpuVertices[1].Position.Y = gpuVertices[0].Position.Y;
- gpuVertices[2].Position.Y = startVertices[2].Y;
- gpuVertices[3].Position.Y = startVertices[3].Y;
- gpuVertices[0].Position.Z = 0;
- gpuVertices[1].Position.Z = 0;
- gpuVertices[2].Position.Z = 0;
- gpuVertices[3].Position.Z = 0;
- vertexBuffer = new DynamicVertexBuffer(graphics, typeof(VertexPositionColorTexture), gpuVertices.Length, BufferUsage.WriteOnly);
- vertexBuffer.SetData(0, gpuVertices, 0, gpuVertices.Length, 0);
- }
- private void InitializeTriangleStrip()
- {
- //edgeFaceList = new short[6];
- edgeFaceList = new ushort[6];
- edgeFaceList[0] = 0; // where 0 is the vertex 0 in your vertex array
- edgeFaceList[1] = 1; // 1 is vertex 1
- edgeFaceList[2] = 2; // 2 is vertex 2 , so 1 triangle is formed using vertices 0,1 and 2
- edgeFaceList[3] = 1; // so for the 2nd triangle you will use vertex 0
- edgeFaceList[4] = 2; // vertex 2
- edgeFaceList[5] = 3; // and finally vertex 3, so the 2nd triangle is vertex 0,2 and 3.
- //* indexBuffer = new DynamicIndexBuffer(graphics, typeof(short), edgeFaceList.Length, BufferUsage.WriteOnly); // indices;
- indexBuffer = new DynamicIndexBuffer(graphics, IndexElementSize.SixteenBits, edgeFaceList.Length, BufferUsage.WriteOnly); // indices;
- indexBuffer.SetData(edgeFaceList);
- }
- public void ResetPosition()
- {
- gpuVertices[0].Position.X = startVertices[0].X;
- gpuVertices[1].Position.X = startVertices[1].X;
- gpuVertices[2].Position.X = startVertices[2].X;
- gpuVertices[3].Position.X = startVertices[3].X;
- gpuVertices[0].Position.Y = startVertices[0].Y;
- gpuVertices[1].Position.Y = startVertices[1].Y;
- gpuVertices[2].Position.Y = startVertices[2].Y;
- gpuVertices[3].Position.Y = startVertices[3].Y;
- }
- public void Update(GameTime gameTime)
- {
- if(Entity.chargingAction != null && Entity.chargingAction.name != "Nothing")
- UpdatePowerSliver(gameTime);
- if (hitDelay > 0)
- {
- hitDelay -= (float)gameTime.ElapsedGameTime.TotalSeconds;
- return;
- }
- if (TiltFinished && currentTilt != Tilt.None)
- {
- switch (currentTilt)
- {
- case Tilt.Rise:
- TiltFinished = false;
- dipfactor = 10f;
- Interval = TimeSpan.FromSeconds(0.2);
- currentTilt = Tilt.TiltForward;
- break;
- case Tilt.TiltBackward:
- TiltFinished = false;
- currentTilt = Tilt.TiltBackwardReverse;
- break;
- case Tilt.TiltBackwardReverse:
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyForward;
- }
- else
- {
- TiltFinished = false;
- currentTilt = Tilt.TiltForward;
- }
- break;
- case Tilt.TiltForward:
- TiltFinished = false;
- currentTilt = Tilt.TiltForwardReverse;
- break;
- case Tilt.SideStepBackward:
- TiltFinished = false;
- currentTilt = Tilt.SideStepForward;
- break;
- case Tilt.SideStepForward:
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackward;
- }
- else
- {
- TiltFinished = true;
- currentTilt = Tilt.None;
- }
- break;
- case Tilt.TiltForwardReverse:
- if (entity.IsAlive)
- {
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackward;
- }
- else
- {
- TiltFinished = true;
- currentTilt = Tilt.None;
- }
- }
- else
- {
- TiltFinished = false;
- currentTilt = Tilt.Fall;
- }
- break;
- case Tilt.WobblyForward:
- TiltFinished = false;
- currentTilt = Tilt.WobblyForwardReverse;
- break;
- case Tilt.WobblyForwardReverse:
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackward;
- }
- else
- {
- TiltFinished = true;
- currentTilt = Tilt.None;
- }
- break;
- case Tilt.WobblyBackward:
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackwardReverse;
- break;
- case Tilt.WobblyBackwardReverse:
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyForward;
- }
- else
- {
- TiltFinished = true;
- currentTilt = Tilt.None;
- }
- break;
- case Tilt.SpinTo90:
- TiltFinished = false;
- currentTilt = Tilt.SpinTo180;
- Interval = SpinTimerIncrease();
- break;
- case Tilt.SpinTo180:
- TiltFinished = false;
- currentTilt = Tilt.SpinTo270;
- Interval = SpinTimerIncrease();
- break;
- case Tilt.SpinTo270:
- TiltFinished = false;
- currentTilt = Tilt.SpinTo360;
- Interval = SpinTimerIncrease();
- break;
- case Tilt.SpinTo360:
- TiltFinished = false;
- if (Interval >= TimeSpan.FromSeconds(.4))
- {
- currentTilt = Tilt.None;
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackward;
- }
- else
- {
- TiltFinished = true;
- currentTilt = Tilt.None;
- }
- }
- else
- {
- currentTilt = Tilt.SpinTo90;
- Interval = SpinTimerIncrease();
- }
- break;
- case Tilt.FlyAwayTo90:
- TiltFinished = false;
- currentTilt = Tilt.FlyAwayTo180;
- break;
- case Tilt.FlyAwayTo180:
- TiltFinished = false;
- currentTilt = Tilt.FlyAwayTo270;
- break;
- case Tilt.FlyAwayTo270:
- TiltFinished = false;
- currentTilt = Tilt.FlyAwayTo360;
- break;
- case Tilt.FlyAwayTo360:
- TiltFinished = false;
- if (flyAwayTimer >= flyAwayTime)
- {
- sparkleTimer = TimeSpan.Zero;
- Interval = sparkleSpinInterval;
- currentTilt = Tilt.SparkleTo90;
- sparkleAwayHeight = GetSparkleAwayHeight(flyAwayHeight);
- positionZ = 100;
- }
- else
- {
- currentTilt = Tilt.FlyAwayTo90;
- }
- break;
- case Tilt.SparkleTo90:
- TiltFinished = false;
- currentTilt = Tilt.SparkleTo180;
- break;
- case Tilt.SparkleTo180:
- TiltFinished = false;
- currentTilt = Tilt.SparkleTo270;
- break;
- case Tilt.SparkleTo270:
- TiltFinished = false;
- currentTilt = Tilt.SparkleTo360;
- break;
- case Tilt.SparkleTo360:
- if (sparkleTimer >= sparkleTime)
- {
- //Use Sparkle Animation
- TiltFinished = true;
- currentTilt = Tilt.None;
- Visible = false;
- }
- else
- {
- TiltFinished = false;
- currentTilt = Tilt.SparkleTo90;
- }
- break;
- case Tilt.HopUp1:
- TiltFinished = false;
- currentTilt = Tilt.HopDown1;
- hopTimer = TimeSpan.Zero;
- break;
- case Tilt.HopDown1:
- TiltFinished = false;
- currentTilt = Tilt.HopUp2;
- hopTimer = TimeSpan.Zero;
- break;
- case Tilt.HopUp2:
- TiltFinished = false;
- currentTilt = Tilt.HopDown2;
- hopTimer = TimeSpan.Zero;
- break;
- case Tilt.HopDown2:
- if (entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackward;
- }
- else
- {
- TiltFinished = true;
- currentTilt = Tilt.None;
- }
- break;
- case Tilt.Fall:
- Visible = false;
- break;
- }
- }
- if (!TiltFinished)
- {
- if (currentTilt == Tilt.TiltForward)
- TiltForward(gameTime);
- else if (currentTilt == Tilt.TiltForwardReverse)
- TiltForwardReverse(gameTime);
- else if (currentTilt == Tilt.TiltBackward)
- TiltBackward(gameTime);
- else if (currentTilt == Tilt.TiltBackwardReverse)
- TiltBackwardReverse(gameTime);
- else if (currentTilt == Tilt.WobblyForward)
- WobblyForward(gameTime);
- else if (currentTilt == Tilt.WobblyForwardReverse)
- WobblyForwardReverse(gameTime);
- else if (currentTilt == Tilt.WobblyBackward)
- WobblyBackward(gameTime);
- else if (currentTilt == Tilt.WobblyBackwardReverse)
- WobblyBackwardReverse(gameTime);
- else if (currentTilt == Tilt.Fall)
- Fall(gameTime);
- else if (currentTilt == Tilt.Rise)
- Rise(gameTime);
- else if (currentTilt == Tilt.SideStepBackward)
- SideStepBackward(gameTime);
- else if (currentTilt == Tilt.SideStepForward)
- SideStepForward(gameTime);
- else if (currentTilt == Tilt.SpinTo90)
- {
- SpinTo90(gameTime);
- }
- else if (currentTilt == Tilt.SpinTo180)
- SpinTo180(gameTime);
- else if (currentTilt == Tilt.SpinTo270)
- SpinTo270(gameTime);
- else if (currentTilt == Tilt.SpinTo360)
- SpinTo360(gameTime);
- else if (currentTilt == Tilt.FlyAwayTo90)
- FlyAwayTo90(gameTime);
- else if (currentTilt == Tilt.FlyAwayTo180)
- FlyAwayTo180(gameTime);
- else if (currentTilt == Tilt.FlyAwayTo270)
- FlyAwayTo270(gameTime);
- else if (currentTilt == Tilt.FlyAwayTo360)
- FlyAwayTo360(gameTime);
- else if (currentTilt == Tilt.SparkleTo90)
- SparkleTo90(gameTime);
- else if (currentTilt == Tilt.SparkleTo180)
- SparkleTo180(gameTime);
- else if (currentTilt == Tilt.SparkleTo270)
- SparkleTo270(gameTime);
- else if (currentTilt == Tilt.SparkleTo360)
- SparkleTo360(gameTime);
- else if (currentTilt == Tilt.HopUp1)
- HopUp1(gameTime);
- else if (currentTilt == Tilt.HopDown1)
- HopDown1(gameTime);
- else if (currentTilt == Tilt.HopUp2)
- HopUp2(gameTime);
- else if (currentTilt == Tilt.HopDown2)
- HopDown2(gameTime);
- }
- else if (TiltFinished && currentTilt == Tilt.None && entity.Wobbly())
- {
- TiltFinished = false;
- currentTilt = Tilt.WobblyBackward;
- }
- if (TakingAction || TakingReaction)
- {
- mFadeDelay -= gameTime.ElapsedGameTime.TotalSeconds;
- //If the Fade delays has dropped below zero, then it is time to
- //fade in/fade out the image a little bit more.
- if (mFadeDelay <= 0)
- {
- //Reset the Fade delay
- mFadeDelay = maxFadeDelay;
- //Increment/Decrement the fade value for the image
- mAlphaValue += mFadeIncrement;
- //If the AlphaValue is equal or above the max Alpha value or
- //has dropped below or equal to the min Alpha value, then
- //reverse the fade
- if (mAlphaValue >= 255 || mAlphaValue <= 0)
- {
- mFadeIncrement *= -1;
- }
- }
- if (TakingAction)
- OffensiveColor(gameTime);
- else if (TakingReaction)
- DefensiveColor(gameTime);
- }
- else
- {
- gpuVertices[0].Color = Color.White;
- }
- gpuVertices[1].Color = gpuVertices[0].Color;
- gpuVertices[2].Color = gpuVertices[0].Color;
- gpuVertices[3].Color = gpuVertices[0].Color;
- shadow.Update(gpuVertices);
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- if (Visible && !Entity.StanceActive(Equations.Hide) && !Entity.StanceActive(Equations.Burrow))//Entity.reactionStance != "Hide" && Entity.reactionStance != "Burrow")
- {
- spriteBatch.End();
- basicEffect.Texture = image;
- basicEffect.VertexColorEnabled = true;
- basicEffect.TextureEnabled = true; //Will make colors black instead of red
- graphics.Indices = indexBuffer;
- graphics.SetVertexBuffer(vertexBuffer);
- graphics.RasterizerState = rasterizerState;
- foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
- {
- pass.Apply();
- DrawTriangleStrip();
- }
- spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
- }
- }
- private void DrawTriangleStrip()
- {
- graphics.DrawUserIndexedPrimitives<VertexPositionColorTexture>(
- PrimitiveType.TriangleList,
- gpuVertices,
- 0,
- gpuVertices.Length,
- shortUshortConversion(edgeFaceList),//*edgeFaceList,
- 0,
- 2);
- }
- short[] shortUshortConversion(ushort[] Ushort)
- {
- short[] NewShort = new short[Ushort.Length];
- for(int i = 0; i < NewShort.Length; i++)
- {
- NewShort[i] = Ushort[i] > (ushort)short.MaxValue
- ? short.MaxValue
- : (short)Ushort[i];
- }
- return NewShort;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement