initial
This commit is contained in:
commit
41aaa1fffe
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
.vscode/launch.json
vendored
Normal file
7
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": []
|
||||||
|
}
|
||||||
2183
Cargo.lock
generated
Normal file
2183
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[package]
|
||||||
|
name = "hellorust"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rltk = { version = "0.8.7" }
|
||||||
|
specs = "0.18.0"
|
||||||
|
specs-derive = "0.4.1"
|
||||||
12
resources/backing.fs
Normal file
12
resources/backing.fs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
uniform sampler2D screenTexture;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 col = texture(screenTexture, TexCoords).rgb;
|
||||||
|
FragColor = vec4(col, 1.0);
|
||||||
|
}
|
||||||
11
resources/backing.vs
Normal file
11
resources/backing.vs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec2 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoords;
|
||||||
|
|
||||||
|
out vec2 TexCoords;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
TexCoords = aTexCoords;
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
|
||||||
|
}
|
||||||
17
resources/console_no_bg.fs
Normal file
17
resources/console_no_bg.fs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
in vec3 ourBackground;
|
||||||
|
|
||||||
|
// texture sampler
|
||||||
|
uniform sampler2D texture1;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 original = texture(texture1, TexCoord);
|
||||||
|
if (original.r < 0.1f || original.g < 0.1f || original.b < 0.1f) discard;
|
||||||
|
vec4 fg = original * vec4(ourColor, 1.f);
|
||||||
|
FragColor = fg;
|
||||||
|
}
|
||||||
17
resources/console_no_bg.vs
Normal file
17
resources/console_no_bg.vs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec3 bColor;
|
||||||
|
layout (location = 3) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec3 ourBackground;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
ourBackground = bColor;
|
||||||
|
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
|
||||||
|
}
|
||||||
16
resources/console_with_bg.fs
Normal file
16
resources/console_with_bg.fs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
in vec3 ourBackground;
|
||||||
|
|
||||||
|
// texture sampler
|
||||||
|
uniform sampler2D texture1;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 original = texture(texture1, TexCoord);
|
||||||
|
vec4 fg = original.r > 0.1f || original.g > 0.1f || original.b > 0.1f ? original * vec4(ourColor, 1.f) : vec4(ourBackground, 1.f);
|
||||||
|
FragColor = fg;
|
||||||
|
}
|
||||||
17
resources/console_with_bg.vs
Normal file
17
resources/console_with_bg.vs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
layout (location = 2) in vec3 bColor;
|
||||||
|
layout (location = 3) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec3 ourBackground;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
ourBackground = bColor;
|
||||||
|
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
|
||||||
|
}
|
||||||
BIN
resources/example_tiles.jpg
Normal file
BIN
resources/example_tiles.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
BIN
resources/example_tiles.xcf
Normal file
BIN
resources/example_tiles.xcf
Normal file
Binary file not shown.
BIN
resources/mltest.xp
Normal file
BIN
resources/mltest.xp
Normal file
Binary file not shown.
BIN
resources/nyan.xp
Normal file
BIN
resources/nyan.xp
Normal file
Binary file not shown.
26
resources/scanlines.fs
Normal file
26
resources/scanlines.fs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec2 TexCoords;
|
||||||
|
|
||||||
|
uniform sampler2D screenTexture;
|
||||||
|
uniform vec3 screenSize;
|
||||||
|
uniform bool screenBurn;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 col = texture(screenTexture, TexCoords).rgb;
|
||||||
|
float scanLine = mod(gl_FragCoord.y, 2.0) * 0.25;
|
||||||
|
vec3 scanColor = col.rgb - scanLine;
|
||||||
|
|
||||||
|
if (col.r < 0.1f && col.g < 0.1f && col.b < 0.1f) {
|
||||||
|
if (screenBurn) {
|
||||||
|
float dist = (1.0 - distance(vec2(gl_FragCoord.x / screenSize.x, gl_FragCoord.y / screenSize.y), vec2(0.5,0.5))) * 0.2;
|
||||||
|
FragColor = vec4(0.0, dist, dist, 1.0);
|
||||||
|
} else {
|
||||||
|
FragColor = vec4(0.0, 0.0, 0.0, 1.0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FragColor = vec4(scanColor, 1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
resources/scanlines.vs
Normal file
11
resources/scanlines.vs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec2 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoords;
|
||||||
|
|
||||||
|
out vec2 TexCoords;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
TexCoords = aTexCoords;
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
|
||||||
|
}
|
||||||
BIN
resources/terminal8x8.jpg
Normal file
BIN
resources/terminal8x8.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
BIN
resources/vga8x16.jpg
Normal file
BIN
resources/vga8x16.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
123
src/main.rs
Normal file
123
src/main.rs
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
use rltk::{GameState, Rltk, VirtualConsole, VirtualKeyCode, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
use specs_derive::Component;
|
||||||
|
use std::{cmp::{max, min}, sync::TryLockResult};
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
ecs: World,
|
||||||
|
}
|
||||||
|
impl GameState for State {
|
||||||
|
fn tick(&mut self, ctx: &mut Rltk) {
|
||||||
|
ctx.cls();
|
||||||
|
|
||||||
|
// self.run_systems();
|
||||||
|
player_input(self, ctx);
|
||||||
|
|
||||||
|
let positions = self.ecs.read_storage::<Position>();
|
||||||
|
let renderables = self.ecs.read_storage::<Renderable>();
|
||||||
|
|
||||||
|
for (pos, render) in (&positions, &renderables).join() {
|
||||||
|
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Player {}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Position {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Renderable {
|
||||||
|
glyph: rltk::FontCharType,
|
||||||
|
fg: RGB,
|
||||||
|
bg: RGB,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||||
|
let mut positions = ecs.write_storage::<Position>();
|
||||||
|
let mut players = ecs.write_storage::<Player>();
|
||||||
|
|
||||||
|
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||||
|
pos.x = min(79, max(0, pos.x + delta_x));
|
||||||
|
pos.y = min(49, max(0, pos.y + delta_y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||||
|
match ctx.key {
|
||||||
|
None => {}
|
||||||
|
Some(key) => match key {
|
||||||
|
VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
enum TileType {
|
||||||
|
Wall, Floor
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||||
|
(y as usize * 80) + x as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_map() -> Vec<TileType> {
|
||||||
|
let mut map = vec![TileType::Floor; 80*50];
|
||||||
|
|
||||||
|
for x in 0..80 {
|
||||||
|
map[xy_idx(x, 0)] = TileType::Wall;
|
||||||
|
map[xy_idx(x, 49)] = TileType::Wall;
|
||||||
|
}
|
||||||
|
for y in 0..50 {
|
||||||
|
map[xy_idx(0, y)] = TileType::Wall;
|
||||||
|
map[xy_idx(79, y)] = TileType::Wall;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut rng = rltk::RandomNumberGenerator::new();
|
||||||
|
|
||||||
|
for _i in 0..400 {
|
||||||
|
let x = rng.roll_dice(1, 79);
|
||||||
|
let y = rng.roll_dice(1, 49);
|
||||||
|
let idx = xy_idx(x, y);
|
||||||
|
if idx != xy_idx(40, 25) {
|
||||||
|
map[idx] = TileType::Wall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
map
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> rltk::BError {
|
||||||
|
use rltk::RltkBuilder;
|
||||||
|
let context = RltkBuilder::simple80x50()
|
||||||
|
.with_title("Rougelike Tutorial")
|
||||||
|
.build()?;
|
||||||
|
let mut gs = State { ecs: World::new() };
|
||||||
|
gs.ecs.register::<Position>();
|
||||||
|
gs.ecs.register::<Renderable>();
|
||||||
|
gs.ecs.register::<Player>();
|
||||||
|
gs.ecs.insert(new_map());
|
||||||
|
|
||||||
|
gs.ecs
|
||||||
|
.create_entity()
|
||||||
|
.with(Position { x: 40, y: 35 })
|
||||||
|
.with(Renderable {
|
||||||
|
glyph: rltk::to_cp437('@'),
|
||||||
|
fg: RGB::named(rltk::YELLOW),
|
||||||
|
bg: RGB::named(rltk::BLACK),
|
||||||
|
})
|
||||||
|
.with(Player {})
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Main loop runner
|
||||||
|
rltk::main_loop(context, gs)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user