Advertisement
SteelGolem

game1.cs

Jul 10th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.08 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace WindowsGame1
  13. {
  14.     public class Game1 : Game
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             using (Game1 game = new Game1())
  19.             {
  20.                 game.Run();
  21.             }
  22.         }
  23.  
  24.         GraphicsDeviceManager graphics;
  25.         SpriteBatch spriteBatch;
  26.  
  27.         KeyboardState keyboard, lastKeyboard;
  28.         MouseState mouse, lastMouse;
  29.         GamePadState gamepad, lastGamepad;
  30.  
  31.         int gameWidth = 160;
  32.         int gameHeight = 144;
  33.         int gameScale = 4;
  34.  
  35.         Texture2D spriteSheet;
  36.         Texture2D background;
  37.         Texture2D fontSheet;
  38.  
  39.         Dictionary<string, Rectangle> srcRectLookup;
  40.  
  41.         //string dataFolder = @"E:\coding\ano sekai\data\";
  42.  
  43.         string gameState;
  44.  
  45.         int invw, invh;
  46.         string[,] inventory;
  47.         int invx, invy;
  48.         string itemB, itemA;
  49.  
  50.         int playerx, playery;
  51.         string playerSrcRect;
  52.  
  53.         bool usingGamepad;
  54.  
  55.         public Game1()
  56.         {
  57.             graphics = new GraphicsDeviceManager(this);
  58.             graphics.PreferredBackBufferWidth = gameWidth * gameScale;
  59.             graphics.PreferredBackBufferHeight = gameHeight * gameScale;
  60.             Content.RootDirectory = "Content";
  61.             IsMouseVisible = true;
  62.         }
  63.  
  64.         protected override void LoadContent()
  65.         {
  66.             spriteBatch = new SpriteBatch(GraphicsDevice);
  67.  
  68.             spriteSheet = Content.Load<Texture2D>("sprite_sheet");
  69.             background = Content.Load<Texture2D>("background");
  70.             fontSheet = Content.Load<Texture2D>("zla_font");
  71.             {
  72.                 Color black = new Color(0x00, 0x00, 0x00);
  73.                 Color[] colorData = new Color[fontSheet.Width * fontSheet.Height];
  74.                 fontSheet.GetData<Color>(colorData);
  75.                 for (int c = 0; c < fontSheet.Width * fontSheet.Height; c++)
  76.                     if (colorData[c] == black) colorData[c] = Color.Transparent;
  77.                 fontSheet.SetData<Color>(colorData);
  78.             }
  79.  
  80.             srcRectLookup = new Dictionary<string, Rectangle>();
  81.             srcRectLookup.Add("link_u", new Rectangle(0, 0, 16, 16));
  82.             srcRectLookup.Add("link_d", new Rectangle(0, 17, 16, 16));
  83.             srcRectLookup.Add("link_l", new Rectangle(0, 34, 16, 16));
  84.             srcRectLookup.Add("link_r", new Rectangle(0, 51, 16, 16));
  85.             srcRectLookup.Add("inv_cursor", new Rectangle(128, 0, 40, 16));
  86.             srcRectLookup.Add("item_grass", new Rectangle(128, 17, 24, 16));
  87.             srcRectLookup.Add("item_flowers", new Rectangle(153, 17, 24, 16));
  88.             srcRectLookup.Add("item_dirt_path", new Rectangle(178, 17, 24, 16));
  89.             srcRectLookup.Add("item_tall_grass", new Rectangle(203, 17, 24, 16));
  90.             srcRectLookup.Add("item_fence", new Rectangle(128, 34, 24, 16));
  91.  
  92.             invw = 4;
  93.             invh = 5;
  94.             inventory = new string[invw, invh];
  95.             inventory[0, 0] = "item_grass";
  96.             inventory[0, 1] = "item_flowers";
  97.             inventory[0, 2] = "item_dirt_path";
  98.             inventory[1, 0] = "item_tall_grass";
  99.             inventory[1, 1] = "item_fence";
  100.             invx = 0;
  101.             invy = 0;
  102.  
  103.             playerx = 120;
  104.             playery = 36;
  105.             playerSrcRect = "link_d";
  106.  
  107.             gameState = "main";
  108.  
  109.             usingGamepad = false;
  110.         }
  111.  
  112.         protected override void Update(GameTime gameTime)
  113.         {
  114.             keyboard = Keyboard.GetState();
  115.             mouse = Mouse.GetState();
  116.             gamepad = GamePad.GetState(PlayerIndex.One);
  117.  
  118.             if (keyboard.IsKeyDown(Keys.Escape)) this.Exit();
  119.  
  120.             if (gameState == "main")
  121.             {
  122.                 if (!IsActive) goto DONE_PLAYER;
  123.  
  124.                 if ((keyboard.IsKeyDown(Keys.Enter) && !lastKeyboard.IsKeyDown(Keys.Enter)) ||
  125.                     (gamepad.IsButtonDown(Buttons.Start) && !lastGamepad.IsButtonDown(Buttons.Start)))
  126.                 {
  127.                     gameState = "inventory";
  128.                     goto DONE_PLAYER;
  129.                 }
  130.  
  131.                 int dx = 0;
  132.                 int dy = 0;
  133.                 if (keyboard.IsKeyDown(Keys.Up) || gamepad.IsButtonDown(Buttons.DPadUp))
  134.                 {
  135.                     dy--;
  136.                 }
  137.                 if (keyboard.IsKeyDown(Keys.Down) || gamepad.IsButtonDown(Buttons.DPadDown))
  138.                 {
  139.                     dy++;
  140.                 }
  141.                 if (keyboard.IsKeyDown(Keys.Left) || gamepad.IsButtonDown(Buttons.DPadLeft))
  142.                 {
  143.                     dx--;
  144.                 }
  145.                 if (keyboard.IsKeyDown(Keys.Right) || gamepad.IsButtonDown(Buttons.DPadRight))
  146.                 {
  147.                     dx++;
  148.                 }
  149.                 if (dx != 0 || dy != 0)
  150.                 {
  151.                     playerx += dx;
  152.                     playery += dy;
  153.                     if (dx == 0 && dy < 0) playerSrcRect = "link_u";
  154.                     if (dx == 0 && dy > 0) playerSrcRect = "link_d";
  155.                     if (dx < 0 && dy == 0) playerSrcRect = "link_l";
  156.                     if (dx > 0 && dy == 0) playerSrcRect = "link_r";
  157.                 }
  158.             }
  159.             else if (gameState == "inventory")
  160.             {
  161.                 if (!IsActive) goto DONE_PLAYER;
  162.  
  163.                 if ((keyboard.IsKeyDown(Keys.Enter) && !lastKeyboard.IsKeyDown(Keys.Enter)) ||
  164.                     (gamepad.IsButtonDown(Buttons.Start) && !lastGamepad.IsButtonDown(Buttons.Start)))
  165.                 {
  166.                     gameState = "main";
  167.                     goto DONE_PLAYER;
  168.                 }
  169.  
  170.                 if ((keyboard.IsKeyDown(Keys.Up) && !lastKeyboard.IsKeyDown(Keys.Up)) ||
  171.                     (gamepad.IsButtonDown(Buttons.DPadUp) && !lastGamepad.IsButtonDown(Buttons.DPadUp)))
  172.                 {
  173.                     invy = Math.Max(0, invy - 1);
  174.                 }
  175.                 if ((keyboard.IsKeyDown(Keys.Down) && !lastKeyboard.IsKeyDown(Keys.Down)) ||
  176.                     (gamepad.IsButtonDown(Buttons.DPadDown) && !lastGamepad.IsButtonDown(Buttons.DPadDown)))
  177.                 {
  178.                     invy = Math.Min(invh - 1, invy + 1);
  179.                 }
  180.                 if ((keyboard.IsKeyDown(Keys.Left) && !lastKeyboard.IsKeyDown(Keys.Left)) ||
  181.                     (gamepad.IsButtonDown(Buttons.DPadLeft) && !lastGamepad.IsButtonDown(Buttons.DPadLeft)))
  182.                 {
  183.                     invx = Math.Max(0, invx - 1);
  184.                 }
  185.                 if ((keyboard.IsKeyDown(Keys.Right) && !lastKeyboard.IsKeyDown(Keys.Right)) ||
  186.                     (gamepad.IsButtonDown(Buttons.DPadRight) && !lastGamepad.IsButtonDown(Buttons.DPadRight)))
  187.                 {
  188.                     invx = Math.Min(invw - 1, invx + 1);
  189.                 }
  190.                 if ((keyboard.IsKeyDown(Keys.Z) && !lastKeyboard.IsKeyDown(Keys.Z)) ||
  191.                     (gamepad.IsButtonDown(Buttons.X) && !lastGamepad.IsButtonDown(Buttons.X)))
  192.                 {
  193.                     string temp = itemB;
  194.                     itemB = inventory[invx, invy];
  195.                     inventory[invx, invy] = temp;
  196.                 }
  197.                 if ((keyboard.IsKeyDown(Keys.X) && !lastKeyboard.IsKeyDown(Keys.X)) ||
  198.                     (gamepad.IsButtonDown(Buttons.A) && !lastGamepad.IsButtonDown(Buttons.A)))
  199.                 {
  200.                     string temp = itemA;
  201.                     itemA = inventory[invx, invy];
  202.                     inventory[invx, invy] = temp;
  203.                 }
  204.             }
  205.             DONE_PLAYER:
  206.  
  207.             if (gamepad.IsButtonDown(Buttons.DPadUp) ||
  208.                 gamepad.IsButtonDown(Buttons.DPadDown) ||
  209.                 gamepad.IsButtonDown(Buttons.DPadLeft) ||
  210.                 gamepad.IsButtonDown(Buttons.DPadRight) ||
  211.                 gamepad.IsButtonDown(Buttons.X) ||
  212.                 gamepad.IsButtonDown(Buttons.A) ||
  213.                 gamepad.IsButtonDown(Buttons.Start))
  214.             {
  215.                 usingGamepad = true;
  216.             }
  217.             if (keyboard.IsKeyDown(Keys.Up) ||
  218.                 keyboard.IsKeyDown(Keys.Down) ||
  219.                 keyboard.IsKeyDown(Keys.Left) ||
  220.                 keyboard.IsKeyDown(Keys.Right) ||
  221.                 keyboard.IsKeyDown(Keys.Z) ||
  222.                 keyboard.IsKeyDown(Keys.X) ||
  223.                 keyboard.IsKeyDown(Keys.Enter))
  224.             {
  225.                 usingGamepad = false;
  226.             }
  227.  
  228.  
  229.             lastKeyboard = keyboard;
  230.             lastMouse = mouse;
  231.             lastGamepad = gamepad;
  232.  
  233.             base.Update(gameTime);
  234.         }
  235.  
  236.         protected override void Draw(GameTime gameTime)
  237.         {
  238.             //GraphicsDevice.Clear(new Color(0xF8, 0xF8, 0xF8));
  239.  
  240.             Matrix scaleMatrix = Matrix.CreateScale(gameScale, gameScale, 1);
  241.             Matrix transMatrix = Matrix.CreateTranslation(0, 0, 0);
  242.             if (gameState == "inventory") transMatrix = Matrix.CreateTranslation(0, -128, 0);
  243.             spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, transMatrix * scaleMatrix);
  244.             {
  245.                 spriteBatch.Draw(background, Vector2.Zero, new Color(0x80, 0x80, 0xFF));
  246.  
  247.                 {
  248.                     if (usingGamepad)
  249.                     {
  250.                         DrawText(spriteBatch, "\xF3", new Vector2(0, 128), Color.Black, Color.White);
  251.                         DrawText(spriteBatch, "\xF1", new Vector2(40, 128), Color.Black, Color.White);
  252.                     }
  253.                     else
  254.                     {
  255.                         DrawText(spriteBatch, "\xF2", new Vector2(0, 128), Color.Black, Color.White);
  256.                         DrawText(spriteBatch, "\xF3", new Vector2(40, 128), Color.Black, Color.White);
  257.                     }
  258.  
  259.                     if (itemB != null)
  260.                         spriteBatch.Draw(spriteSheet, new Vector2(8, 128), srcRectLookup[itemB], Color.White);
  261.                     if (itemA != null)
  262.                         spriteBatch.Draw(spriteSheet, new Vector2(48, 128), srcRectLookup[itemA], Color.White);
  263.  
  264.                     for (int y = 0; y < invh; y++)
  265.                     {
  266.                         for (int x = 0; x < invw; x++)
  267.                         {
  268.                             if (inventory[x, y] != null)
  269.                                 spriteBatch.Draw(spriteSheet, new Vector2(8 + x * 40, 152 + y * 24), srcRectLookup[inventory[x, y]], Color.White);
  270.                         }
  271.                     }
  272.                     spriteBatch.Draw(spriteSheet, new Vector2(invx * 40, 152 + invy * 24), srcRectLookup["inv_cursor"], Color.White);
  273.  
  274.                     if (inventory[invx, invy] != null)
  275.                         DrawText(spriteBatch, inventory[invx, invy], new Vector2(0, background.Height - 8), Color.White, Color.Black);
  276.                 }
  277.  
  278.                 spriteBatch.Draw(spriteSheet, new Vector2(playerx, playery), srcRectLookup[playerSrcRect], Color.White);
  279.             }
  280.             spriteBatch.End();
  281.  
  282.             base.Draw(gameTime);
  283.         }
  284.  
  285.         void DrawText(SpriteBatch spriteBatch, string text, Vector2 topLeft, Color fg, Color bg)
  286.         {
  287.             Vector2 pos = topLeft;
  288.             Rectangle srcRect = new Rectangle(0, 0, 8, 8);
  289.             Rectangle bgRect = new Rectangle(279, 63, 8, 8);
  290.             for (int t = 0; t < text.Length; t++)
  291.             {
  292.                 int c = text[t];
  293.  
  294.                 if (c == '\n')
  295.                 {
  296.                     pos.X = topLeft.X;
  297.                     pos.Y += 8;
  298.                     continue;
  299.                 }
  300.  
  301.                 spriteBatch.Draw(fontSheet, pos, bgRect, bg);
  302.  
  303.                 srcRect.X = (c % 16) * 9;
  304.                 srcRect.Y = (c / 16) * 9;
  305.                 spriteBatch.Draw(fontSheet, pos, srcRect, fg);
  306.                 pos.X += 8;
  307.             }
  308.         }
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement