sync my shit
This commit is contained in:
+14
-2
@@ -1,13 +1,25 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import ecs "shoal/src"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
Transform :: struct {
|
GridActorComponent :: struct {
|
||||||
position: rl.Vector2,
|
position: rl.Vector2,
|
||||||
|
size: int,
|
||||||
|
speed: f32,
|
||||||
|
can_move: bool,
|
||||||
}
|
}
|
||||||
|
GridActor: ecs.Storage(GridActorComponent)
|
||||||
|
|
||||||
Weapon :: struct {
|
WeaponComponent :: struct {
|
||||||
dmg: i32,
|
dmg: i32,
|
||||||
rpm: f32,
|
rpm: f32,
|
||||||
cooldown: bool,
|
cooldown: bool,
|
||||||
}
|
}
|
||||||
|
Weapon: ecs.Storage(WeaponComponent)
|
||||||
|
|
||||||
|
EnemyComponent :: struct {}
|
||||||
|
Enemy: ecs.Storage(EnemyComponent)
|
||||||
|
|
||||||
|
PlayerComponent :: struct {}
|
||||||
|
Player: ecs.Storage(PlayerComponent)
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
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() {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,20 +1 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import rl "vendor:raylib"
|
|
||||||
|
|
||||||
Entity :: union {
|
|
||||||
Player,
|
|
||||||
Enemy,
|
|
||||||
}
|
|
||||||
|
|
||||||
Player :: struct {
|
|
||||||
transform: Transform,
|
|
||||||
sprite: rl.Texture2D,
|
|
||||||
camera: rl.Camera2D,
|
|
||||||
weapon: Weapon,
|
|
||||||
}
|
|
||||||
|
|
||||||
Enemy :: struct {
|
|
||||||
transform: Transform,
|
|
||||||
sprite: rl.Texture2D,
|
|
||||||
}
|
|
||||||
|
|||||||
+35
-62
@@ -1,82 +1,65 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
import ecs "shoal/src"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
World :: struct {
|
|
||||||
entities: map[rl.Vector2]Entity,
|
|
||||||
}
|
|
||||||
|
|
||||||
world: World
|
|
||||||
player: Player
|
|
||||||
enemy: Enemy
|
|
||||||
camera: rl.Camera2D
|
camera: rl.Camera2D
|
||||||
CELL_SIZE :: 20
|
CELL_SIZE :: 20
|
||||||
|
|
||||||
|
app: ecs.App
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
fmt.println("hello world")
|
ecs.add_plugin(&app, ecs.DefaultPlugin)
|
||||||
rl.InitWindow(800, 600, "grimm gameplay prototype")
|
ecs.add_system(&app, ecs.STARTUP, setup_entities)
|
||||||
defer rl.CloseWindow()
|
ecs.add_system(&app, ecs.UPDATE, player_move)
|
||||||
|
ecs.add_system(&app, ecs.UPDATE, run_particle_system)
|
||||||
player = Player {
|
ecs.add_system(&app, ecs.UPDATE, run_timers)
|
||||||
transform = Transform{position = {4, 8}},
|
ecs.add_system(&app, ecs.DRAW, draw_player)
|
||||||
camera = rl.Camera2D{target = {0, 0}, zoom = 1.5},
|
ecs.add_system(&app, ecs.UPDATE, update_grid_actor)
|
||||||
weapon = Weapon{dmg = 1, rpm = 450, cooldown = false},
|
ecs.run(&app)
|
||||||
}
|
|
||||||
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() {
|
setup_entities :: proc() {
|
||||||
|
player := ecs.spawn_entity(&app)
|
||||||
|
ecs.add_component(player, &Player)
|
||||||
|
ecs.add_component(player, &Weapon)
|
||||||
|
ecs.add_component(player, &GridActor)
|
||||||
|
ecs.add_component(player, &ecs.Sprite)
|
||||||
|
ecs.add_component(player, &ecs.Transform)
|
||||||
|
}
|
||||||
|
|
||||||
|
player_move :: proc() {
|
||||||
|
e, player := ecs.single(&Player)
|
||||||
|
grid_actor := ecs.get_component(e, &GridActor)
|
||||||
|
|
||||||
if rl.IsKeyPressed(rl.KeyboardKey.W) {
|
if rl.IsKeyPressed(rl.KeyboardKey.W) {
|
||||||
move(&player.transform, 0, -1)
|
move(grid_actor, 0, -1)
|
||||||
}
|
}
|
||||||
if rl.IsKeyPressed(rl.KeyboardKey.A) {
|
if rl.IsKeyPressed(rl.KeyboardKey.A) {
|
||||||
move(&player.transform, -1, 0)
|
move(grid_actor, -1, 0)
|
||||||
}
|
}
|
||||||
if rl.IsKeyPressed(rl.KeyboardKey.S) {
|
if rl.IsKeyPressed(rl.KeyboardKey.S) {
|
||||||
move(&player.transform, 0, 1)
|
move(grid_actor, 0, 1)
|
||||||
}
|
}
|
||||||
if rl.IsKeyPressed(rl.KeyboardKey.D) {
|
if rl.IsKeyPressed(rl.KeyboardKey.D) {
|
||||||
move(&player.transform, 1, 0)
|
move(grid_actor, 1, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rl.IsKeyPressed(rl.KeyboardKey.SPACE) {
|
|
||||||
move(&enemy.transform, 1, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
run_timers()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
draw :: proc() {
|
draw_player :: proc() {
|
||||||
|
e, player := ecs.single(&Player)
|
||||||
|
transform := ecs.get_component(e, &ecs.Transform)
|
||||||
rl.DrawRectangle(
|
rl.DrawRectangle(
|
||||||
i32(player.transform.position.x * CELL_SIZE),
|
i32(transform.translation.x),
|
||||||
i32(player.transform.position.y * CELL_SIZE),
|
i32(transform.translation.y),
|
||||||
CELL_SIZE,
|
CELL_SIZE,
|
||||||
CELL_SIZE,
|
CELL_SIZE,
|
||||||
rl.WHITE,
|
rl.WHITE,
|
||||||
)
|
)
|
||||||
rl.DrawRectangle(
|
}
|
||||||
i32(enemy.transform.position.x * CELL_SIZE),
|
|
||||||
i32(enemy.transform.position.y * CELL_SIZE),
|
|
||||||
CELL_SIZE,
|
|
||||||
CELL_SIZE,
|
|
||||||
rl.BLUE,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
draw_grid :: proc() {
|
||||||
for x in 0 ..< 26 {
|
for x in 0 ..< 26 {
|
||||||
for y in 0 ..< 26 {
|
for y in 0 ..< 26 {
|
||||||
rl.DrawRectangleLines(
|
rl.DrawRectangleLines(
|
||||||
@@ -88,14 +71,4 @@ draw :: proc() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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(player.transform, {f32(pos_x), f32(pos_y)})
|
|
||||||
}
|
|
||||||
|
|
||||||
run_particle_system()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Submodule
+1
Submodule general/shoal added at de088f69e6
+37
-12
@@ -3,39 +3,64 @@ package main
|
|||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:math"
|
import "core:math"
|
||||||
import "core:math/rand"
|
import "core:math/rand"
|
||||||
|
import ecs "shoal/src"
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
move :: proc(t: ^Transform, x, y: f32) {
|
can_move_signal := ecs.Signal {
|
||||||
next_position := Transform {
|
callbacks = {reset_grid_actor_can_move},
|
||||||
|
}
|
||||||
|
|
||||||
|
move :: proc(t: ^GridActorComponent, x, y: f32) {
|
||||||
|
next_position := GridActorComponent {
|
||||||
position = {t.position.x + x, t.position.y + y},
|
position = {t.position.x + x, t.position.y + y},
|
||||||
}
|
}
|
||||||
if e, occupied := world.entities[next_position.position]; !occupied {
|
|
||||||
delete_key(&world.entities, t.position)
|
for i, v in ecs.slice(&GridActor) {
|
||||||
defer world.entities[t.position] = e
|
if v.position != next_position.position {
|
||||||
t.position = next_position.position
|
t.position = next_position.position
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shoot :: proc(t: Transform, pos: rl.Vector2) {
|
reset_grid_actor_can_move :: proc(args: ..any) {
|
||||||
|
t.can_move = true
|
||||||
|
}
|
||||||
|
|
||||||
|
update_grid_actor :: proc() {
|
||||||
|
for e, grid_actor in ecs.slice(&GridActor) {
|
||||||
|
transform := ecs.get_component(e, &ecs.Transform)
|
||||||
|
transform.translation = grid_actor.position * CELL_SIZE
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
shoot :: proc(t: ecs.TransformComponent, weapon: ^WeaponComponent, pos: rl.Vector2) {
|
||||||
pos_centered := center_to_grid(pos)
|
pos_centered := center_to_grid(pos)
|
||||||
t_centered := center_to_grid(t.position)
|
t_centered := center_to_grid(t.translation)
|
||||||
spray: rl.Vector2 = {
|
spray: rl.Vector2 = {
|
||||||
pos.x + rand.float32_range(0, CELL_SIZE),
|
pos.x + rand.float32_range(0, CELL_SIZE),
|
||||||
pos.y + rand.float32_range(0, CELL_SIZE),
|
pos.y + rand.float32_range(0, CELL_SIZE),
|
||||||
}
|
}
|
||||||
line_particle := LineParticle {
|
line_particle := LineParticle {
|
||||||
pos1 = t.position * CELL_SIZE + 10,
|
pos1 = t.translation * CELL_SIZE + 10,
|
||||||
pos2 = spray,
|
pos2 = spray,
|
||||||
tint = rl.WHITE,
|
tint = rl.WHITE,
|
||||||
lifetime = 0.2,
|
lifetime = 0.2,
|
||||||
}
|
}
|
||||||
|
|
||||||
if player.weapon.cooldown == true {return}
|
if weapon.cooldown == true {return}
|
||||||
player.weapon.cooldown = true
|
weapon.cooldown = true
|
||||||
start_timer(60 / player.weapon.rpm, proc() {player.weapon.cooldown = false})
|
start_timer(60 / weapon.rpm, reset_weapon_cooldown)
|
||||||
spawn_particle(line_particle)
|
spawn_particle(line_particle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reset_weapon_cooldown :: proc() {
|
||||||
|
for i, v in ecs.slice(&Player) {
|
||||||
|
weapon := ecs.get_component(i, &Weapon)
|
||||||
|
weapon.cooldown = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
center_to_grid :: proc(pos: rl.Vector2) -> rl.Vector2 {
|
center_to_grid :: proc(pos: rl.Vector2) -> rl.Vector2 {
|
||||||
pos_x, pos_y: f32
|
pos_x, pos_y: f32
|
||||||
pos_x = pos.x + CELL_SIZE / 2
|
pos_x = pos.x + CELL_SIZE / 2
|
||||||
|
|||||||
Reference in New Issue
Block a user