Browse Source

basic Player motions and poses

GitOrigin-RevId: 88a4e7a208
master
Colin McMillen 4 years ago
parent
commit
fdd0eb60c4
  1. 1
      Jumpy.Shared/Jumpy.Shared.projitems
  2. 23
      Jumpy.Shared/JumpyGame.cs
  3. 73
      Jumpy.Shared/Player.cs

1
Jumpy.Shared/Jumpy.Shared.projitems

@ -12,5 +12,6 @@
<Compile Include="$(MSBuildThisFileDirectory)IDisplay.cs" />
<Compile Include="$(MSBuildThisFileDirectory)KeyboardInput.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JumpyGame.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Player.cs" />
</ItemGroup>
</Project>

23
Jumpy.Shared/JumpyGame.cs

@ -8,12 +8,13 @@ namespace Jumpy {
public class JumpyGame : Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D playerTexture;
SpriteFont font;
KeyboardInput keyboardInput = new KeyboardInput();
bool fullScreen = false;
IDisplay display;
Player player;
public JumpyGame() {
graphics = new GraphicsDeviceManager(this);
IsMouseVisible = true;
@ -33,7 +34,8 @@ namespace Jumpy {
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font");
playerTexture = Content.Load<Texture2D>("player");
player = new Player(Content.Load<Texture2D>("player"));
}
// Called once per game. Unloads all game content.
@ -44,6 +46,7 @@ namespace Jumpy {
// Updates the game world.
protected override void Update(GameTime gameTime) {
keyboardInput.Update();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
List<Keys> keysDown = keyboardInput.NewKeysDown();
if (keysDown.Contains(Keys.F12)) {
@ -51,11 +54,13 @@ namespace Jumpy {
display.SetFullScreen(fullScreen);
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
if (gamePadState.Buttons.Start == ButtonState.Pressed ||
keysDown.Contains(Keys.Escape)) {
Exit();
}
player.Update(gameTime, gamePadState);
base.Update(gameTime);
}
@ -63,18 +68,8 @@ namespace Jumpy {
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
const int spriteSize = 144;
int frameNum = gameTime.TotalGameTime.Milliseconds / 250;
if (frameNum == 3) {
frameNum = 1;
}
int sourceX = spriteSize * frameNum + spriteSize * 3;
int sourceY = spriteSize * 0;
Rectangle source = new Rectangle(sourceX, sourceY, spriteSize, spriteSize);
Vector2 position = new Vector2(100, 100);
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
spriteBatch.Begin();
spriteBatch.Draw(playerTexture, position, source, Color.White, 0f, spriteCenter, Vector2.One, SpriteEffects.FlipHorizontally, 0f);
player.Draw(gameTime, spriteBatch);
spriteBatch.End();
// spriteBatch.DrawString(font, "hello world", new Vector2(100, 100), Color.Black);

73
Jumpy.Shared/Player.cs

@ -0,0 +1,73 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace Jumpy {
class Player {
enum Facing { Left, Right };
enum Pose { Walking, Standing, Crouching, Stretching };
private const int spriteSize = 144;
private const int spriteWidth = 20;
private const int moveSpeed = 600;
// TODO: stop assuming 1920x1080.
private Vector2 position = new Vector2(200, 1080 - spriteSize / 2);
private Facing facing = Facing.Right;
private Pose pose = Pose.Standing;
private Texture2D texture;
public Player(Texture2D texture) {
this.texture = texture;
}
public void Update(GameTime gameTime, GamePadState gamePadState) {
Vector2 leftStick = gamePadState.ThumbSticks.Left;
if (leftStick.X < -0.5) {
facing = Facing.Left;
pose = Pose.Walking;
position.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
} else if (leftStick.X > 0.5) {
facing = Facing.Right;
pose = Pose.Walking;
position.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
} else if (leftStick.Y < -0.5) {
pose = Pose.Crouching;
} else if (leftStick.Y > 0.5) {
pose = Pose.Stretching;
} else {
pose = Pose.Standing;
}
position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), 1920 - spriteWidth);
}
private Point spritePosition(Pose pose, GameTime time) {
switch (pose) {
case Pose.Walking:
int frameNum = (time.TotalGameTime.Milliseconds / 125) % 4;
if (frameNum == 3) {
frameNum = 1;
}
return new Point(spriteSize * frameNum + spriteSize * 6, 0);
case Pose.Crouching:
return new Point(spriteSize * 7, spriteSize * 2);
case Pose.Stretching:
return new Point(spriteSize * 1, spriteSize * 2);
case Pose.Standing:
default:
return new Point(spriteSize * 7, 0);
}
}
public void Draw(GameTime time, SpriteBatch spriteBatch) {
Point source = spritePosition(pose, time);
Rectangle textureSource = new Rectangle(source.X, source.Y, spriteSize, spriteSize);
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
SpriteEffects effect = facing == Facing.Right ?
SpriteEffects.FlipHorizontally : SpriteEffects.None;
spriteBatch.Draw(texture, position, textureSource, Color.White, 0f, spriteCenter,
Vector2.One, effect, 0f);
}
}
}
Loading…
Cancel
Save