using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Audio; namespace Colto1 { class Animal { Level1 parent; Texture2D animalImage; Texture2D animalImageLeft; public int direction = 1; public Boolean jump = false; //Vector2 location=new Vector2(100, 100); public Rectangle location = new Rectangle(300, 450, 100, 150); public Vector2 speed = new Vector2(0, 0); public Animal(Level1 parent) { this.parent = parent; } public void LoadContent(ContentManager manager, string filename, string leftfilename) { animalImage = manager.Load(filename); animalImageLeft = manager.Load(leftfilename); } public void setSpeedX(float value) { if (value > 0) { direction = 1; } if (value < 0) { direction = -1; } speed.X = value; } public void Update() { // location += new Vector2(1, 0); //if (location.Y > parent.floorup || jump) { location.X += (int)speed.X; location.Y += (int)speed.Y; //} if (location.Y <= parent.floorup) { if (jump) { speed.Y += parent.jumpg; if (location.Y > parent.floorup) { jump = false; speed.Y = 0; } if (speed.X < 0 && location.X - parent.xs[0] < parent.jumpdistance) { jump = false; location.X = parent.xs[0]; speed.X = 0; speed.Y = 0; } if (speed.X > 0 && parent.xs[2]-location.X < parent.jumpdistance) { jump = false; location.X = parent.xs[2]; speed.X = 0; speed.Y = 0; } if (((parent.xs[1] - location.X < parent.jumpdistance) && (parent.xs[1] - location.X>0) && speed.X > 0) || (location.X - parent.xs[1] < parent.jumpdistance && (location.X - parent.xs[1]>0) && speed.X < 0) ) { jump = false; location.X = parent.xs[1]; // speed.X = 0; speed.Y = 0; } } else { cancelJump(); if (location.X < (parent.xs[0] + parent.xs[1]) / 2) { location.X = parent.xs[0]; } else if (location.X >= (parent.xs[1] + parent.xs[2]) / 2) { location.X = parent.xs[2]; } else { location.X = parent.xs[1]; } } } if (location.X < parent.left) { location.X = parent.left; speed.X = 0; cancelJump(); } if (location.X > parent.right) { location.X = parent.right; speed.X = 0; cancelJump(); } // location.Y += (int)speed.Y; if (location.Y > parent.floor) { location.Y = parent.floor; speed.X=0; speed.Y=0; } if (location.Y < 100) { location.Y = 100; } } public void cancelJump() { if (jump) { jump = false; speed.X = 0; speed.Y = 0; } } public void Draw(SpriteBatch batch) { Rectangle drawLocation = new Rectangle( location.X-location.Width/2, location.Y-location.Height/2, location.Width, location.Height ); // batch.Draw(animalImage, location, Color.White); if (direction < 0) { batch.Draw(animalImageLeft, drawLocation, Color.White); } else { batch.Draw(animalImage, drawLocation, Color.White); } } } }