initial commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Transform :: struct {
|
||||
position: rl.Vector2,
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package ecs
|
||||
|
||||
import "core:fmt"
|
||||
|
||||
startup_systems: [dynamic]proc()
|
||||
update_systems: [dynamic]proc()
|
||||
fixed_update_systems: [dynamic]proc()
|
||||
|
||||
|
||||
App :: struct {}
|
||||
|
||||
Component :: union {
|
||||
Transform,
|
||||
}
|
||||
|
||||
Transform :: struct {}
|
||||
|
||||
STARTUP :: 0
|
||||
UPDATE :: 1
|
||||
FIXED_UPDATE :: 2
|
||||
|
||||
add_system :: proc(schedule: int, system: proc()) {
|
||||
switch schedule {
|
||||
case 0:
|
||||
append(&startup_systems, system)
|
||||
case 1:
|
||||
append(&update_systems, system)
|
||||
case 2:
|
||||
append(&fixed_update_systems, system)
|
||||
}
|
||||
}
|
||||
|
||||
ping :: proc() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
Entity :: union {
|
||||
Player,
|
||||
Enemy,
|
||||
}
|
||||
|
||||
Player :: struct {
|
||||
transform: Transform,
|
||||
sprite: rl.Texture2D,
|
||||
camera: rl.Camera2D,
|
||||
}
|
||||
|
||||
Enemy :: struct {
|
||||
transform: Transform,
|
||||
sprite: rl.Texture2D,
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "ecs"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
World :: struct {
|
||||
entities: map[rl.Vector2]Entity,
|
||||
}
|
||||
|
||||
world: World
|
||||
player: Player
|
||||
enemy: Enemy
|
||||
camera: rl.Camera2D
|
||||
CELL_SIZE :: 20
|
||||
|
||||
main :: proc() {
|
||||
ecs.ping()
|
||||
fmt.println("hello world")
|
||||
rl.InitWindow(800, 600, "grimm gameplay prototype")
|
||||
defer rl.CloseWindow()
|
||||
|
||||
player = Player {
|
||||
transform = Transform{position = {4, 8}},
|
||||
camera = rl.Camera2D{target = {0, 0}, zoom = 1.5},
|
||||
}
|
||||
world.entities[player.transform.position] = player
|
||||
enemy = Enemy {
|
||||
transform = Transform{position = {8, 8}},
|
||||
}
|
||||
world.entities[enemy.transform.position] = enemy
|
||||
|
||||
for !rl.WindowShouldClose() {
|
||||
update()
|
||||
rl.BeginDrawing()
|
||||
rl.ClearBackground(rl.PURPLE)
|
||||
rl.BeginMode2D(player.camera)
|
||||
draw()
|
||||
rl.EndMode2D()
|
||||
rl.EndDrawing()
|
||||
}
|
||||
}
|
||||
|
||||
update :: proc() {
|
||||
if rl.IsKeyPressed(rl.KeyboardKey.W) {
|
||||
move(&player.transform, 0, -1)
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeyboardKey.A) {
|
||||
move(&player.transform, -1, 0)
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeyboardKey.S) {
|
||||
move(&player.transform, 0, 1)
|
||||
}
|
||||
if rl.IsKeyPressed(rl.KeyboardKey.D) {
|
||||
move(&player.transform, 1, 0)
|
||||
}
|
||||
|
||||
if rl.IsKeyPressed(rl.KeyboardKey.SPACE) {
|
||||
move(&enemy.transform, 1, 0)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
draw :: proc() {
|
||||
rl.DrawRectangle(
|
||||
i32(player.transform.position.x * CELL_SIZE),
|
||||
i32(player.transform.position.y * CELL_SIZE),
|
||||
CELL_SIZE,
|
||||
CELL_SIZE,
|
||||
rl.WHITE,
|
||||
)
|
||||
rl.DrawRectangle(
|
||||
i32(enemy.transform.position.x * CELL_SIZE),
|
||||
i32(enemy.transform.position.y * CELL_SIZE),
|
||||
CELL_SIZE,
|
||||
CELL_SIZE,
|
||||
rl.BLUE,
|
||||
)
|
||||
|
||||
|
||||
for x in 0 ..< 26 {
|
||||
for y in 0 ..< 26 {
|
||||
rl.DrawRectangleLines(
|
||||
i32(x) * CELL_SIZE,
|
||||
i32(y) * CELL_SIZE,
|
||||
CELL_SIZE,
|
||||
CELL_SIZE,
|
||||
rl.BLANK,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if rl.IsMouseButtonDown(rl.MouseButton.LEFT) {
|
||||
pos := rl.GetScreenToWorld2D(rl.GetMousePosition(), player.camera)
|
||||
pos_x := int(pos.x / CELL_SIZE) * CELL_SIZE
|
||||
pos_y := int(pos.y / CELL_SIZE) * CELL_SIZE
|
||||
shoot({f32(pos_x), f32(pos_y)}, player.transform)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
move :: proc(t: ^Transform, x, y: f32) {
|
||||
next_position := Transform {
|
||||
position = {t.position.x + x, t.position.y + y},
|
||||
}
|
||||
if e, occupied := world.entities[next_position.position]; !occupied {
|
||||
delete_key(&world.entities, t.position)
|
||||
defer world.entities[t.position] = e
|
||||
t.position = next_position.position
|
||||
}
|
||||
}
|
||||
|
||||
shoot :: proc(pos: rl.Vector2, t: Transform) {
|
||||
pos_centered := center_to_grid(pos)
|
||||
t_centered := center_to_grid(t.position)
|
||||
rl.DrawLine(
|
||||
i32(t.position.x) * CELL_SIZE + (CELL_SIZE / 2),
|
||||
i32(t.position.y) * CELL_SIZE + (CELL_SIZE / 2),
|
||||
i32(pos_centered.x),
|
||||
i32(pos_centered.y),
|
||||
rl.WHITE,
|
||||
)
|
||||
}
|
||||
|
||||
center_to_grid :: proc(pos: rl.Vector2) -> rl.Vector2 {
|
||||
pos_x, pos_y: f32
|
||||
pos_x = pos.x + CELL_SIZE / 2
|
||||
pos_y = pos.y + CELL_SIZE / 2
|
||||
return rl.Vector2{pos_x, pos_y}
|
||||
}
|
||||
Reference in New Issue
Block a user