Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace WindowsGame1
- {
- public class Game1 : Game
- {
- static void Main(string[] args)
- {
- using (Game1 game = new Game1())
- {
- game.Run();
- }
- }
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- KeyboardState keyboard, lastKeyboard;
- MouseState mouse, lastMouse;
- GamePadState gamepad, lastGamepad;
- int gameWidth = 160;
- int gameHeight = 144;
- int gameScale = 4;
- Texture2D spriteSheet;
- Texture2D background;
- Texture2D fontSheet;
- Dictionary<string, Rectangle> srcRectLookup;
- //string dataFolder = @"E:\coding\ano sekai\data\";
- string gameState;
- int invw, invh;
- string[,] inventory;
- int invx, invy;
- string itemB, itemA;
- int playerx, playery;
- string playerSrcRect;
- bool usingGamepad;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = gameWidth * gameScale;
- graphics.PreferredBackBufferHeight = gameHeight * gameScale;
- Content.RootDirectory = "Content";
- IsMouseVisible = true;
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- spriteSheet = Content.Load<Texture2D>("sprite_sheet");
- background = Content.Load<Texture2D>("background");
- fontSheet = Content.Load<Texture2D>("zla_font");
- {
- Color black = new Color(0x00, 0x00, 0x00);
- Color[] colorData = new Color[fontSheet.Width * fontSheet.Height];
- fontSheet.GetData<Color>(colorData);
- for (int c = 0; c < fontSheet.Width * fontSheet.Height; c++)
- if (colorData[c] == black) colorData[c] = Color.Transparent;
- fontSheet.SetData<Color>(colorData);
- }
- srcRectLookup = new Dictionary<string, Rectangle>();
- srcRectLookup.Add("link_u", new Rectangle(0, 0, 16, 16));
- srcRectLookup.Add("link_d", new Rectangle(0, 17, 16, 16));
- srcRectLookup.Add("link_l", new Rectangle(0, 34, 16, 16));
- srcRectLookup.Add("link_r", new Rectangle(0, 51, 16, 16));
- srcRectLookup.Add("inv_cursor", new Rectangle(128, 0, 40, 16));
- srcRectLookup.Add("item_grass", new Rectangle(128, 17, 24, 16));
- srcRectLookup.Add("item_flowers", new Rectangle(153, 17, 24, 16));
- srcRectLookup.Add("item_dirt_path", new Rectangle(178, 17, 24, 16));
- srcRectLookup.Add("item_tall_grass", new Rectangle(203, 17, 24, 16));
- srcRectLookup.Add("item_fence", new Rectangle(128, 34, 24, 16));
- invw = 4;
- invh = 5;
- inventory = new string[invw, invh];
- inventory[0, 0] = "item_grass";
- inventory[0, 1] = "item_flowers";
- inventory[0, 2] = "item_dirt_path";
- inventory[1, 0] = "item_tall_grass";
- inventory[1, 1] = "item_fence";
- invx = 0;
- invy = 0;
- playerx = 120;
- playery = 36;
- playerSrcRect = "link_d";
- gameState = "main";
- usingGamepad = false;
- }
- protected override void Update(GameTime gameTime)
- {
- keyboard = Keyboard.GetState();
- mouse = Mouse.GetState();
- gamepad = GamePad.GetState(PlayerIndex.One);
- if (keyboard.IsKeyDown(Keys.Escape)) this.Exit();
- if (gameState == "main")
- {
- if (!IsActive) goto DONE_PLAYER;
- if ((keyboard.IsKeyDown(Keys.Enter) && !lastKeyboard.IsKeyDown(Keys.Enter)) ||
- (gamepad.IsButtonDown(Buttons.Start) && !lastGamepad.IsButtonDown(Buttons.Start)))
- {
- gameState = "inventory";
- goto DONE_PLAYER;
- }
- int dx = 0;
- int dy = 0;
- if (keyboard.IsKeyDown(Keys.Up) || gamepad.IsButtonDown(Buttons.DPadUp))
- {
- dy--;
- }
- if (keyboard.IsKeyDown(Keys.Down) || gamepad.IsButtonDown(Buttons.DPadDown))
- {
- dy++;
- }
- if (keyboard.IsKeyDown(Keys.Left) || gamepad.IsButtonDown(Buttons.DPadLeft))
- {
- dx--;
- }
- if (keyboard.IsKeyDown(Keys.Right) || gamepad.IsButtonDown(Buttons.DPadRight))
- {
- dx++;
- }
- if (dx != 0 || dy != 0)
- {
- playerx += dx;
- playery += dy;
- if (dx == 0 && dy < 0) playerSrcRect = "link_u";
- if (dx == 0 && dy > 0) playerSrcRect = "link_d";
- if (dx < 0 && dy == 0) playerSrcRect = "link_l";
- if (dx > 0 && dy == 0) playerSrcRect = "link_r";
- }
- }
- else if (gameState == "inventory")
- {
- if (!IsActive) goto DONE_PLAYER;
- if ((keyboard.IsKeyDown(Keys.Enter) && !lastKeyboard.IsKeyDown(Keys.Enter)) ||
- (gamepad.IsButtonDown(Buttons.Start) && !lastGamepad.IsButtonDown(Buttons.Start)))
- {
- gameState = "main";
- goto DONE_PLAYER;
- }
- if ((keyboard.IsKeyDown(Keys.Up) && !lastKeyboard.IsKeyDown(Keys.Up)) ||
- (gamepad.IsButtonDown(Buttons.DPadUp) && !lastGamepad.IsButtonDown(Buttons.DPadUp)))
- {
- invy = Math.Max(0, invy - 1);
- }
- if ((keyboard.IsKeyDown(Keys.Down) && !lastKeyboard.IsKeyDown(Keys.Down)) ||
- (gamepad.IsButtonDown(Buttons.DPadDown) && !lastGamepad.IsButtonDown(Buttons.DPadDown)))
- {
- invy = Math.Min(invh - 1, invy + 1);
- }
- if ((keyboard.IsKeyDown(Keys.Left) && !lastKeyboard.IsKeyDown(Keys.Left)) ||
- (gamepad.IsButtonDown(Buttons.DPadLeft) && !lastGamepad.IsButtonDown(Buttons.DPadLeft)))
- {
- invx = Math.Max(0, invx - 1);
- }
- if ((keyboard.IsKeyDown(Keys.Right) && !lastKeyboard.IsKeyDown(Keys.Right)) ||
- (gamepad.IsButtonDown(Buttons.DPadRight) && !lastGamepad.IsButtonDown(Buttons.DPadRight)))
- {
- invx = Math.Min(invw - 1, invx + 1);
- }
- if ((keyboard.IsKeyDown(Keys.Z) && !lastKeyboard.IsKeyDown(Keys.Z)) ||
- (gamepad.IsButtonDown(Buttons.X) && !lastGamepad.IsButtonDown(Buttons.X)))
- {
- string temp = itemB;
- itemB = inventory[invx, invy];
- inventory[invx, invy] = temp;
- }
- if ((keyboard.IsKeyDown(Keys.X) && !lastKeyboard.IsKeyDown(Keys.X)) ||
- (gamepad.IsButtonDown(Buttons.A) && !lastGamepad.IsButtonDown(Buttons.A)))
- {
- string temp = itemA;
- itemA = inventory[invx, invy];
- inventory[invx, invy] = temp;
- }
- }
- DONE_PLAYER:
- if (gamepad.IsButtonDown(Buttons.DPadUp) ||
- gamepad.IsButtonDown(Buttons.DPadDown) ||
- gamepad.IsButtonDown(Buttons.DPadLeft) ||
- gamepad.IsButtonDown(Buttons.DPadRight) ||
- gamepad.IsButtonDown(Buttons.X) ||
- gamepad.IsButtonDown(Buttons.A) ||
- gamepad.IsButtonDown(Buttons.Start))
- {
- usingGamepad = true;
- }
- if (keyboard.IsKeyDown(Keys.Up) ||
- keyboard.IsKeyDown(Keys.Down) ||
- keyboard.IsKeyDown(Keys.Left) ||
- keyboard.IsKeyDown(Keys.Right) ||
- keyboard.IsKeyDown(Keys.Z) ||
- keyboard.IsKeyDown(Keys.X) ||
- keyboard.IsKeyDown(Keys.Enter))
- {
- usingGamepad = false;
- }
- lastKeyboard = keyboard;
- lastMouse = mouse;
- lastGamepad = gamepad;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- //GraphicsDevice.Clear(new Color(0xF8, 0xF8, 0xF8));
- Matrix scaleMatrix = Matrix.CreateScale(gameScale, gameScale, 1);
- Matrix transMatrix = Matrix.CreateTranslation(0, 0, 0);
- if (gameState == "inventory") transMatrix = Matrix.CreateTranslation(0, -128, 0);
- spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, transMatrix * scaleMatrix);
- {
- spriteBatch.Draw(background, Vector2.Zero, new Color(0x80, 0x80, 0xFF));
- {
- if (usingGamepad)
- {
- DrawText(spriteBatch, "\xF3", new Vector2(0, 128), Color.Black, Color.White);
- DrawText(spriteBatch, "\xF1", new Vector2(40, 128), Color.Black, Color.White);
- }
- else
- {
- DrawText(spriteBatch, "\xF2", new Vector2(0, 128), Color.Black, Color.White);
- DrawText(spriteBatch, "\xF3", new Vector2(40, 128), Color.Black, Color.White);
- }
- if (itemB != null)
- spriteBatch.Draw(spriteSheet, new Vector2(8, 128), srcRectLookup[itemB], Color.White);
- if (itemA != null)
- spriteBatch.Draw(spriteSheet, new Vector2(48, 128), srcRectLookup[itemA], Color.White);
- for (int y = 0; y < invh; y++)
- {
- for (int x = 0; x < invw; x++)
- {
- if (inventory[x, y] != null)
- spriteBatch.Draw(spriteSheet, new Vector2(8 + x * 40, 152 + y * 24), srcRectLookup[inventory[x, y]], Color.White);
- }
- }
- spriteBatch.Draw(spriteSheet, new Vector2(invx * 40, 152 + invy * 24), srcRectLookup["inv_cursor"], Color.White);
- if (inventory[invx, invy] != null)
- DrawText(spriteBatch, inventory[invx, invy], new Vector2(0, background.Height - 8), Color.White, Color.Black);
- }
- spriteBatch.Draw(spriteSheet, new Vector2(playerx, playery), srcRectLookup[playerSrcRect], Color.White);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- void DrawText(SpriteBatch spriteBatch, string text, Vector2 topLeft, Color fg, Color bg)
- {
- Vector2 pos = topLeft;
- Rectangle srcRect = new Rectangle(0, 0, 8, 8);
- Rectangle bgRect = new Rectangle(279, 63, 8, 8);
- for (int t = 0; t < text.Length; t++)
- {
- int c = text[t];
- if (c == '\n')
- {
- pos.X = topLeft.X;
- pos.Y += 8;
- continue;
- }
- spriteBatch.Draw(fontSheet, pos, bgRect, bg);
- srcRect.X = (c % 16) * 9;
- srcRect.Y = (c / 16) * 9;
- spriteBatch.Draw(fontSheet, pos, srcRect, fg);
- pos.X += 8;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement