public void Update(StaticStructure[] ssa) { oldV = velocity; velocity = Vector2.Zero; oldP = position; keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.A)) { velocity.X = -1; // Going Left } if (keyboardState.IsKeyDown(Keys.D)) { velocity.X = 1; // Going Right } if (keyboardState.IsKeyDown(Keys.Space) || jumping){ velocity.Y = -1; // Move up if (!jumping) jumping = true; } if (jumping) { velocity.Y *= jumpSpeed; jumpSpeed -= 1; } else { velocity.Y = 0; jumpSpeed = 20f; } if (velocity.X != 0) // Player is moving (i.e. Has W or A held down) { if (speed >= maxSpeed) { speed = maxSpeed; } else { speed += .3f; } velocity.X *= speed; } else { speed *= friction; // Apply friction in the correct direction // i.e. Going left, slow down while going left // // Without this, the player would always start moving right // when friction was applied, because friction is always a positive number // // This code simply checks the direction the player was moving in prior to stopping movement if (oldV.X < 0) { velocity.X = -speed; } else if (oldV.X > 0){ velocity.X = speed; } } position += velocity; bounds = new Rectangle((int)position.X, (int)position.Y, t.Width, t.Height); if (isColliding(ssa)) { position = oldP; bounds = this.Bounds; velocity.X = 0; } }
private void loadLevels() { String line, type; short lvl; string[] structs; StaticStructure[] ssa; Texture2D t; int ii; System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); StreamReader sr = new StreamReader(a.GetManifestResourceStream("NOTPOPPROTOTYPE.lvls.txt")); line = sr.ReadLine(); // Get the next line in the document do{ if( line.Contains('!') ){ // If the line contains '!' then it's the beginning of a new level if (line.Length == 1) { break; } lvl = (short)Char.GetNumericValue(line.ElementAt<char>(1)); ssa = new StaticStructure[MAX_STRUCTS_PER_LEVEL]; ii = 0; while( sr.Peek() != '!' ){ line = sr.ReadLine(); structs = line.Split(':'); type = structs[0]; StaticStructure ss = new StaticStructure(); for (int yy = 0; yy < structs.Length; yy++){ if ( yy == 0 ){ t = Content.Load<Texture2D>(type); if (type == "hWall"){ ss = new hWall(t); } else if(type == "vWall"){ ss = new vWall(t); } else{ Console.WriteLine("error in lvls.txt"); } } else if ( yy == 1 ){ ss.X = Convert.ToInt32(structs[yy]); } else if ( yy == 2 ){ ss.Y = Convert.ToInt32(structs[yy]); } else{ Console.WriteLine("More then needed in lvls.txt"); } } ssa[ii] = ss; ii++; } Levels[lvl - 1] = new Level(ssa); } } while ((line = sr.ReadLine()) != null); }
public bool isColliding(StaticStructure[] ssa) { foreach (StaticStructure ss in ssa) { if (ss == null) { break; } if (bounds.Intersects(ss.Bounds)) { if (velocity.Y < 0) // Player is moving up { velocity.Y = 0; // Stop the player from moving up } else if (velocity.Y > 0 && bounds.Bottom > ss.Bounds.Top){ // Moving down jumping = false; grounded = true; } return true; } } return false; }
public Level(StaticStructure[] s) { structs = s; }