Started refactoring

This commit is contained in:
Mootfrost777 2022-07-06 12:47:50 +03:00
commit c0acfb2266
29 changed files with 668 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

25
Pacman_refactored.sln Normal file
View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32413.511
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pacman_refactored", "Pacman_refactored\Pacman_refactored.csproj", "{8C511935-ED5F-4850-8EF4-32058A50A89E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8C511935-ED5F-4850-8EF4-32058A50A89E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C511935-ED5F-4850-8EF4-32058A50A89E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C511935-ED5F-4850-8EF4-32058A50A89E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C511935-ED5F-4850-8EF4-32058A50A89E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {304ACC20-0C3E-47DF-944D-1ACAC4A8B857}
EndGlobalSection
EndGlobal

65
Pacman_refactored/.gitignore vendored Normal file
View file

@ -0,0 +1,65 @@
#OS junk files
[Tt]humbs.db
*.DS_Store
#Visual Studio files
*.pidb
*.userprefs
*.[Oo]bj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.lock
*.ide
*.ide-shm
*.ide-wal
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad
#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*
#Project files
[Bb]uild/
#Subversion files
.svn
# Office Temp Files
~$*
#monodroid private beta
monodroid*.msi
/CompiledContent/Android/IgnoreMe.dll
/CompiledContent/OSX/IgnoreMe.dll
/CompiledContent/Windows8/IgnoreMe.dll
/CompiledContent/Windows/IgnoreMe.dll
*.cachefile
*.dll
*.VC.db
*.VC.opendb
artifacts/

View file

@ -0,0 +1,50 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pacman_refactored.Classes.Interfaces;
using System.Runtime.CompilerServices;
namespace Pacman_refactored.Classes
{
public abstract class Entity : IMovable, IRotatable, IAnimate
{
public abstract Texture2D Texture { get; set; }
public abstract Vector2 Position { get; set; }
public abstract Direction Direction { get; set; }
public abstract float Rotation { get; set; }
public abstract int[] StartPosition { get; set; }
public abstract float Speed { get; set; }
public abstract bool IsAlive { get; set; }
public abstract int CellSize { get; set; }
public abstract Rectangle SourceRect { get; set; }
public abstract Rectangle Boundingbox { get; set; }
public abstract int TextureNumber { get; set; }
public abstract int TextureCount { get; set; }
public virtual void Update(GameTime gameTime)
{
Boundingbox = new Rectangle((int)Position.X, (int)Position.Y, (int)CellSize, (int)CellSize);
IMovable.DirectionMove(Direction, Position, Speed, Game1.Map);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
Rotation = IRotatable.Rotate(Direction, Rotation);
if (Direction != Direction.None)
{
IAnimate.Animate(spriteBatch, Texture, TextureNumber, TextureCount, CellSize, Position, Rotation);
}
else
{
SourceRect = new Rectangle(new Point(0, 0), new Point(CellSize));
spriteBatch.Draw(Texture, Position, SourceRect, Color.White, Rotation, new Vector2(CellSize / 2), 1, SpriteEffects.None, 0);
}
}
}
}

View file

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Pacman_refactored.Classes
{
public enum Direction
{
Up,
Down,
Left,
Right,
None
}
public enum GameState
{
Game,
Menu,
GameOver,
NextLevel,
HowToPlay,
Exit
}
public enum GhostType
{
Blinky,
Pinky,
Inky,
Clyde
}
}

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pacman_refactored.Classes.Interfaces;
namespace Pacman_refactored.Classes
{
public class Ghost : Entity, IAnimate, IMovable
{
public override Texture2D Texture { get; set; }
public override Vector2 Position { get; set; }
public override Direction Direction { get; set; }
public override int[] StartPosition { get; set; }
public override float Speed { get; set; }
public override bool IsAlive { get; set; }
public override int CellSize { get; set; }
public override Rectangle SourceRect { get; set; }
public override Rectangle Boundingbox { get; set; }
public override int TextureNumber { get; set; }
public override int TextureCount { get; set; }
public GhostType GhostType { get; set; }
public Ghost(Texture2D texture, int[] startPosition, float speed, int cellSize, GhostType ghostType)
{
Texture = texture;
TextureNumber = 0;
TextureCount = Texture.Width / CellSize;
CellSize = cellSize;
StartPosition = startPosition;
Position = new Vector2(startPosition[0] * CellSize, startPosition[1] * CellSize);
Direction = Direction.None;
Speed = speed;
GhostType = ghostType;
}
}
}

View file

@ -0,0 +1,30 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using System;
namespace Pacman_refactored.Classes.Interfaces
{
public interface IAnimate
{
const int FramesCooldown = 10;
static void Animate(SpriteBatch spriteBatch, Texture2D texture,
int textureNumber, int textureCount,
int cellSize, Vector2 position,
float rotation)
{
if (textureNumber == textureCount)
{
textureNumber = 0;
}
rotation = rotation * (float)Math.PI / 180;
Rectangle sourceRect = new Rectangle(new Point(0, cellSize * textureNumber), new Point(cellSize));
spriteBatch.Draw(texture, position, sourceRect, Color.White, rotation, new Vector2(cellSize / 2), 1, SpriteEffects.None, 0);
textureNumber++;
}
}
}

View file

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Pacman_refactored.Classes.Interfaces
{
public interface IBoostable
{
}
}

View file

@ -0,0 +1,64 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace Pacman_refactored.Classes.Interfaces
{
public interface IControl
{
/// <summary>
/// Get direction from input device data.
/// </summary>
/// <returns></returns>
static Direction GetDirection()
{
GamePadState gamepadState = GamePad.GetState(0);
KeyboardState keyboardState = Keyboard.GetState();
bool stickUp = false;
bool stickDown = false;
bool stickLeft = false;
bool stickRight = false;
if (Math.Abs(gamepadState.ThumbSticks.Left.Y) > Math.Abs(gamepadState.ThumbSticks.Left.X) &&
Math.Abs(gamepadState.ThumbSticks.Left.Y) > 0.25f)
{
if (gamepadState.ThumbSticks.Left.Y > 0) stickUp = true;
else stickDown = true;
}
else if (Math.Abs(gamepadState.ThumbSticks.Left.X) > Math.Abs(gamepadState.ThumbSticks.Left.Y) &&
Math.Abs(gamepadState.ThumbSticks.Left.X) > 0.25f)
{
if (gamepadState.ThumbSticks.Left.X > 0) stickRight = true;
else stickLeft = true;
}
if (keyboardState.IsKeyDown(Keys.W) ||
gamepadState.DPad.Up == ButtonState.Pressed ||
stickUp)
{
return Direction.Up;
}
else if (keyboardState.IsKeyDown(Keys.S) ||
gamepadState.DPad.Down == ButtonState.Pressed ||
stickDown)
{
return Direction.Down;
}
else if (keyboardState.IsKeyDown(Keys.A) ||
gamepadState.DPad.Left == ButtonState.Pressed ||
stickLeft)
{
return Direction.Left;
}
else if (keyboardState.IsKeyDown(Keys.D) ||
gamepadState.DPad.Right == ButtonState.Pressed ||
stickRight)
{
return Direction.Right;
}
return Direction.None;
}
}
}

View file

@ -0,0 +1,97 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace Pacman_refactored.Classes.Interfaces
{
public interface IMovable
{
/// <summary>
/// Move an entity in a given direction.
/// </summary>
/// <param name="direction"></param>
/// <param name="position"></param>
/// <param name="speed"></param>
/// <param name="map"></param>
static void DirectionMove(Direction direction, Vector2 position, float speed,
Map map)
{
// Move entity
if (direction == Direction.Up && map.Matrix[(int)(position.Y / map.CellSize - 0.5), (int)position.X / map.CellSize] == 0)
{
position.Y -= speed;
}
else if (direction == Direction.Right && map.Matrix[(int)position.Y / map.CellSize, (int)(position.X / map.CellSize + 0.5)] == 0)
{
position.X += speed;
}
else if (direction == Direction.Down && map.Matrix[(int)(position.Y / map.CellSize + 0.5), (int)position.X / map.CellSize] == 0)
{
position.Y += speed;
}
else if (direction == Direction.Left && map.Matrix[(int)position.Y / map.CellSize, (int)(position.X / map.CellSize - 0.5)] == 0)
{
position.X -= speed;
}
// Teleporters handling
foreach (var linkedTeleports in map.Teleports)
{
for (int i = 0; i < linkedTeleports.Count; i++)
{
if (linkedTeleports[i][0] == (int)position.Y / 24 &&
linkedTeleports[i][1] == (int)position.X / 24)
{
if (i == 0)
{
position = new Vector2(linkedTeleports[1][0] * map.CellSize, linkedTeleports[1][1] * map.CellSize);
}
else
{
position = new Vector2(linkedTeleports[0][0] * map.CellSize, linkedTeleports[0][1] * map.CellSize);
}
}
}
}
// Check wall collision
CheckWallCollision(direction, position, map);
}
/// <summary>
/// Check if entity collides with wall.
/// </summary>
/// <param name="direction"></param>
/// <param name="position"></param>
/// <param name="map"></param>
/// <returns name="direction"></returns>
static Direction CheckWallCollision(Direction direction, Vector2 position, Map map)
{
// Check if entity hits into wall
if (direction == Direction.Up && map.Matrix[(int)(position.Y / map.CellSize - 0.5), (int)position.X / map.CellSize] != 0)
{
position.Y = (int)(position.Y / map.CellSize) * map.CellSize + map.CellSize / 2;
direction = Direction.None;
}
else if (direction == Direction.Right && map.Matrix[(int)position.Y / map.CellSize, (int)(position.X / map.CellSize + 0.5)] != 0)
{
position.X = (int)(position.X / map.CellSize) * map.CellSize + map.CellSize / 2;
direction = Direction.None;
}
else if (direction == Direction.Down && map.Matrix[(int)(position.Y / map.CellSize + 0.5), (int)position.X / map.CellSize] != 0)
{
position.Y = (int)(position.Y / map.CellSize) * map.CellSize + map.CellSize / 2;
direction = Direction.None;
}
else if (direction == Direction.Left && map.Matrix[(int)position.Y / map.CellSize, (int)(position.X / map.CellSize - 0.5)] != 0)
{
position.X = (int)(position.X / map.CellSize) * map.CellSize + map.CellSize / 2;
direction = Direction.None;
}
return direction;
}
}
}

View file

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Pacman_refactored.Classes.Interfaces
{
public interface IRotatable
{
static float Rotate(Direction direction, float currentAngle)
{
float angle = 0;
switch (direction)
{
case Direction.Up:
angle = 90;
break;
case Direction.Down:
angle = 270;
break;
case Direction.Left:
angle = 180;
break;
case Direction.Right:
angle = 0;
break;
default:
angle = currentAngle;
break;
}
return angle;
}
}
}

View file

@ -0,0 +1,17 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace Pacman_refactored.Classes
{
public class Map
{
private Texture2D _texture;
public int[,] Matrix;
public List<List<int[]>> Teleports;
public int CellSize = 24;
}
}

View file

@ -0,0 +1,42 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pacman_refactored.Classes.Interfaces;
namespace Pacman_refactored.Classes
{
public class Pacman : Entity, IMovable, IRotatable, IAnimate, IControl
{
public override Texture2D Texture { get; set; }
public override Vector2 Position { get; set; }
public override Direction Direction { get; set; }
public override float Rotation { get; set; }
public override int[] StartPosition { get; set; }
public override float Speed { get; set; }
public override bool IsAlive { get; set; }
public override int CellSize { get; set; }
public override Rectangle SourceRect { get; set; }
public override Rectangle Boundingbox { get; set; }
public override int TextureNumber { get; set; }
public override int TextureCount { get; set; }
public Pacman(Texture2D texture, int[] startPosition, float speed, int cellSize)
{
Texture = texture;
StartPosition = startPosition;
Position = new Vector2(startPosition[0] * cellSize, startPosition[1] * cellSize);
Speed = speed;
CellSize = cellSize;
IsAlive = true;
TextureNumber = 0;
TextureCount = Texture.Width / cellSize;
Direction = Direction.Up;
}
}
}

View file

@ -0,0 +1,15 @@
#----------------------------- Global Properties ----------------------------#
/outputDir:bin/$(Platform)
/intermediateDir:obj/$(Platform)
/platform:DesktopGL
/config:
/profile:Reach
/compress:False
#-------------------------------- References --------------------------------#
#---------------------------------- Content ---------------------------------#

View file

@ -0,0 +1,52 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Pacman_refactored
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}

BIN
Pacman_refactored/Icon.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

BIN
Pacman_refactored/Icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

View file

@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="Icon.ico" />
<None Remove="Icon.bmp" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico" />
<EmbeddedResource Include="Icon.bmp" />
</ItemGroup>
<ItemGroup>
<MonoGameContentReference Include="Content\Content.mgcb" />
</ItemGroup>
<ItemGroup>
<TrimmerRootAssembly Include="Microsoft.Xna.Framework.Content.ContentTypeReader" Visible="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.0.1641" />
</ItemGroup>
<ItemGroup>
<Folder Include="Classes\UI\" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,14 @@
using System;
namespace Pacman_refactored
{
public static class Program
{
[STAThread]
static void Main()
{
using (var game = new Game1())
game.Run();
}
}
}

View file

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="Pacman_refactored"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on and is
is designed to work with. Uncomment the appropriate elements and Windows will
automatically selected the most compatible environment. -->
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">permonitorv2,permonitor</dpiAwareness>
</windowsSettings>
</application>
</assembly>