Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
graphnode committed Jul 4, 2022
0 parents commit 9ee2a7b
Show file tree
Hide file tree
Showing 22 changed files with 392 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.png filter=lfs diff=lfs merge=lfs -text
*.dll filter=lfs diff=lfs merge=lfs -text
*.ico filter=lfs diff=lfs merge=lfs -text
51 changes: 51 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: .NET

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

env:
VERSION: 0.1.${{ github.run_number }}

jobs:
build:
runs-on: windows-latest

permissions:
contents: write

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
lfs: true

- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build and Publish
run: dotnet publish --configuration Release -p:Version=${{ env.VERSION }} --self-contained --no-restore -o publish

- name: Archive Release
uses: thedoctor0/zip-release@main
with:
type: zip
directory: publish
filename: roguelike-${{ env.VERSION }}.zip

- name: Publish Release
uses: ncipollo/release-action@v1
with:
tag: ${{ env.VERSION }}
commit: master
artifacts: publish/roguelike-${{ env.VERSION }}.zip
generateReleaseNotes: true
token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
obj/
bin/
publish/
*.user
13 changes: 13 additions & 0 deletions .idea/.idea.Roguelike/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.Roguelike/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.Roguelike/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.Roguelike/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.Roguelike/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/16x16-RogueYun-AgmEdit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions Assets/AfterglowTheme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Black": "#151515",
"Blue": "#6c99ba",
"BrightBlack": "#505050",
"BrightBlue": "#6c99ba",
"BrightCyan": "#7dd5cf",
"BrightGreen": "#7e8d50",
"BrightMagenta": "#9e4e85",
"BrightRed": "#ac4142",
"BrightWhite": "#f5f5f5",
"BrightYellow": "#e5b566",
"Cyan": "#7dd5cf",
"Green": "#7e8d50",
"Magenta": "#9e4e85",
"Red": "#ac4142",
"White": "#d0d0d0",
"Yellow": "#e5b566",
"Background": "#202020"
}
3 changes: 3 additions & 0 deletions Assets/icon.ico
Git LFS file not shown
3 changes: 3 additions & 0 deletions Assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Engine/Content.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Roguelike;

public static partial class Engine
{
public static string GetAssetPath(string path)
{
return Path.Combine("Assets", path);
}

}
31 changes: 31 additions & 0 deletions Engine/Extensions/ColorEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Globalization;
using Raylib_cs;

namespace Roguelike.Extensions;

public static class ColorEx
{
public static string ToHexString(this Color c)
{
return $"#{c.r:X2}{c.g:X2}{c.b:X2}";
}

/// <summary>
/// Implicitly converts hex string to color.
/// </summary>
/// <param name="str">The hex string to convert.</param>
/// <returns>A Color instance.</returns>
public static Color FromHexString(string str)
{
var hex = str.Replace("#", string.Empty);
const NumberStyles numberStyle = NumberStyles.HexNumber;

var r = byte.Parse(hex[..2], numberStyle);
var g = byte.Parse(hex[2..4], numberStyle);
var b = byte.Parse(hex[4..6], numberStyle);

return new Color(r, g, b, byte.MaxValue);
}

public static Color DARKESTGRAY = new Color(32, 32, 32, (int)byte.MaxValue);
}
21 changes: 21 additions & 0 deletions Engine/Tileset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Numerics;
using Raylib_cs;

namespace Roguelike;

public class Tileset
{
internal readonly Texture2D Texture;
internal readonly Vector2 TileSize;

public Tileset(Texture2D texture, Vector2 tileSize)
{
this.Texture = texture;
this.TileSize = tileSize;
}

public Rectangle GetRect(uint tileX, uint tileY)
{
return new Rectangle(tileX * TileSize.X, tileY * TileSize.Y, TileSize.X, TileSize.Y);
}
}
44 changes: 44 additions & 0 deletions Engine/Utility/ColorPalette.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Raylib_cs;
using Roguelike.Extensions;

namespace Roguelike.Utility;

#pragma warning disable IL2026

public class ColorPalette
{
private Dictionary<string, Color> _colors = new();

public Color this[string key] => _colors.TryGetValue(key, out var value) ? value : default;

public static ColorPalette LoadFromJson(string assetPath)
{
var path = Engine.GetAssetPath(assetPath);
var jsonString = File.ReadAllText(path);

var options = new JsonSerializerOptions();
options.Converters.Add(new ColorJsonConverter());

var colors = JsonSerializer.Deserialize<Dictionary<string, Color>>(jsonString, options) ?? new Dictionary<string, Color>();

return new ColorPalette
{
_colors = colors
};
}
}

internal class ColorJsonConverter : JsonConverter<Color>
{
public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return ColorEx.FromHexString(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToHexString());
}
}
86 changes: 86 additions & 0 deletions Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Numerics;
using System.Reflection;
using Raylib_cs;
using Roguelike.Utility;
using static Raylib_cs.Raylib;

namespace Roguelike;

internal class Game
{
private static Vector2 TileSize => new(16, 16);

private readonly Tileset _tileset;
private readonly ColorPalette _palette;

private Vector2 _playerPosition;

private const float PlayerWalkSpeed = 4f; // in tiles per second
private double _playerLastMove;

private Game()
{
_tileset = new Tileset(LoadTexture(Engine.GetAssetPath("16x16-RogueYun-AgmEdit.png")), TileSize);
_palette = ColorPalette.LoadFromJson("AfterglowTheme.json");
}

private void Update()
{
if (_playerLastMove + (1f/PlayerWalkSpeed) < GetTime())
{
var playerMove = new Vector2();

if (IsKeyDown(KeyboardKey.KEY_LEFT) || IsKeyDown(KeyboardKey.KEY_A))
playerMove.X = -1;
if (IsKeyDown(KeyboardKey.KEY_RIGHT) || IsKeyDown(KeyboardKey.KEY_D))
playerMove.X = 1;
if (IsKeyDown(KeyboardKey.KEY_UP) || IsKeyDown(KeyboardKey.KEY_W))
playerMove.Y = -1;
if (IsKeyDown(KeyboardKey.KEY_DOWN) || IsKeyDown(KeyboardKey.KEY_S))
playerMove.Y = 1;

if (playerMove.X != 0 || playerMove.Y != 0)
{
_playerPosition += new Vector2(playerMove.X * TileSize.X, playerMove.Y * TileSize.Y);
_playerLastMove = GetTime();
}
}

if (IsKeyPressed(KeyboardKey.KEY_ESCAPE))
Environment.Exit(0);
}

private void Draw()
{
DrawTextureRec(_tileset.Texture, _tileset.GetRect(0, 4), _playerPosition, _palette["Yellow"]);
}

private static void Main()
{
var title = "Roguelike";
var version = Assembly.GetEntryAssembly()?.GetName().Version;
title = version == null ? title : $"{title} v{version.ToString(3)}";

InitWindow(800, 640, title);

var iconImg = LoadImage(Engine.GetAssetPath("icon.png"));

SetWindowIcon(iconImg);

SetTargetFPS(60);

var game = new Game();

while (!WindowShouldClose())
{
game.Update();

BeginDrawing();
ClearBackground(game._palette["Background"]);
game.Draw();
EndDrawing();
}

CloseWindow();
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/graphnode/roguelike/.NET)](https://github.com/graphnode/roguelike/actions)
[![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/graphnode/roguelike?include_prereleases)](https://github.com/graphnode/roguelike/releases/latest)

# RoguelikeDev Does The Complete Roguelike Tutorial 2022

![RoguelikeDev Does The Complete Roguelike Tutorial](README/roguelikedev-logo.png)

## Information
Implementation of [/r/roguelikedev](https://reddit.com/r/roguelikedev/)'s annual [Roguelike Tutorial](https://old.reddit.com/r/roguelikedev/comments/vhfsda/roguelikedev_does_the_complete_roguelike_tutorial/) using C# and [Raylib-cs](https://github.com/ChrisDill/Raylib-cs).

## Download
[Download the latest release here.](https://github.com/graphnode/roguelike/releases/latest)
3 changes: 3 additions & 0 deletions README/roguelikedev-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9ee2a7b

Please sign in to comment.