]> jxnshi.xyz Git - they-gone.git/commitdiff
Changed slice for bounded array master
authorjxnshi <jxnshi@cock.li>
Wed, 12 Mar 2025 13:42:51 +0000 (14:42 +0100)
committerjxnshi <jxnshi@cock.li>
Wed, 12 Mar 2025 13:42:51 +0000 (14:42 +0100)
assets/assets.go
assets/player.png
assets/tiles.png [new file with mode: 0644]
bounded_array.go [new file with mode: 0644]
main.go
tile.go [new file with mode: 0644]
tools/tile-edit/go.mod [new file with mode: 0644]
tools/tile-edit/main.go [new file with mode: 0644]

index 66ad5a2634b4df81b1d5da6b3a81a92dbbada913..0c7fb7fe884a1e6dfaf1bd20bd0f9caea67be470 100644 (file)
@@ -4,3 +4,6 @@ import _ "embed"
 
 //go:embed player.png
 var Player []byte
+
+//go:embed tiles.png
+var Tiles []byte
index 518a87a2b5a606602b32be439e98ae3af7690a68..7a4c9703035374129b22fff118ecaf13c7ea804e 100644 (file)
Binary files a/assets/player.png and b/assets/player.png differ
diff --git a/assets/tiles.png b/assets/tiles.png
new file mode 100644 (file)
index 0000000..187ba38
Binary files /dev/null and b/assets/tiles.png differ
diff --git a/bounded_array.go b/bounded_array.go
new file mode 100644 (file)
index 0000000..6fb4ae4
--- /dev/null
@@ -0,0 +1,33 @@
+package main
+
+import "errors"
+
+const BoundedArrayLen int = 100
+
+type BoundedArrayError struct {
+    Err error
+}
+
+func (err *BoundedArrayError) Error() string {
+    return err.Err.Error()
+}
+
+type BoundedArray[T any] struct {
+    Data [BoundedArrayLen]T
+    Len  int
+}
+
+func (array *BoundedArray[T]) Append(elem T) error {
+    if array.Len >= BoundedArrayLen {
+        return errors.New("Bounded array full.")
+    }
+
+    array.Data[array.Len] = elem
+    array.Len += 1
+
+    return nil
+}
+
+func (array *BoundedArray[T]) GetData() []T {
+    return array.Data[:array.Len]
+}
diff --git a/main.go b/main.go
index f6fb20bf48372769b1a8cf2ec79761db11e303dd..a79016c3a5ea7bb883b3f222eb9f7a3ce913c757 100644 (file)
--- a/main.go
+++ b/main.go
@@ -6,64 +6,70 @@ import (
     "they-gone/assets"
 )
 
-const mapSize = 100
-
-type Tile uint8
+const mapSize int = 100
 
 type App struct {
     Camera   rl.Camera2D
-    Entities []Entity
-}
-
-func moveVector() rl.Vector2 {
-    var vector rl.Vector2
-
-    if rl.IsKeyDown(rl.KeyA) { vector.X -= 1 }
-    if rl.IsKeyDown(rl.KeyD) { vector.X += 1 }
-
-    if rl.IsKeyDown(rl.KeyW) { vector.Y -= 1 }    
-    if rl.IsKeyDown(rl.KeyS) { vector.Y += 1 }
+    Entities BoundedArray[Entity]
 
-    return vector
+    BackgroundTiles   [mapSize * mapSize]Tile
+    MiddlegroundTiles [mapSize * mapSize]Tile
+    ForegroundTiles   [mapSize * mapSize]Tile
 }
 
 func main() {
     const windowWidth int = 800
     const windowHeight int = 600
 
-    const playerSpeed float32 = 3
+    const playerSpeed float32 = 5
 
-    rl.InitWindow(int32(windowWidth), int32(windowHeight), "They left.")
+    rl.InitWindow(int32(windowWidth), int32(windowHeight), "They gone.")
     defer rl.CloseWindow()
 
     rl.SetTargetFPS(30)
 
     playerImage := rl.LoadImageFromMemory(".png", assets.Player, int32(len(assets.Player)))
+    tilesImage := rl.LoadImageFromMemory(".png", assets.Tiles, int32(len(assets.Tiles)))
 
     playerTexture := rl.LoadTextureFromImage(playerImage)
     defer rl.UnloadTexture(playerTexture)
 
+    tilesTexture := rl.LoadTextureFromImage(tilesImage)
+    defer rl.UnloadTexture(tilesTexture)
+
     rl.UnloadImage(playerImage)
+    rl.UnloadImage(tilesImage)
 
-    playerSize := rl.Vector2{ 300, 300 }
+    playerSize := rl.Vector2{ 48, 104 }
 
     app := App{
         Camera: rl.Camera2D{
-            Offset: rl.Vector2{ float32(windowWidth) / 2, float32(windowHeight) - playerSize.Y / 2 },
+            Offset: rl.Vector2{ float32(windowWidth) / 2, (float32(windowHeight) + playerSize.Y) / 2 },
             Zoom: 1,
         },
-        Entities: make([]Entity, 0),
+    }
+
+    for i := 0; i < mapSize * mapSize; i += 1 {
+        app.BackgroundTiles[i] = 1
     }
 
     {
         player := NewEntity(playerTexture, rl.Vector2{}, playerSize)
-        app.Entities = append(app.Entities, player)
+        _ = app.Entities.Append(player)
     }
 
-    player := &app.Entities[0]
+    player := &app.Entities.GetData()[0]
 
     for !rl.WindowShouldClose() {
-        moveVec := moveVector()
+        var moveVec rl.Vector2
+
+        if rl.IsKeyDown(rl.KeyA) { moveVec.X -= 1 }
+        if rl.IsKeyDown(rl.KeyD) { moveVec.X += 1 }
+
+        if rl.IsKeyDown(rl.KeyW) { moveVec.Y -= 1 }    
+        if rl.IsKeyDown(rl.KeyS) { moveVec.Y += 1 }
+
+        moveVec = rl.Vector2Normalize(moveVec)
 
         if moveVec.X > 0 { player.Flip = false }
         if moveVec.X < 0 { player.Flip = true }
@@ -71,6 +77,11 @@ func main() {
         player.Position.X += playerSpeed * moveVec.X
         player.Position.Y += playerSpeed * moveVec.Y
 
+        app.Camera.Target = player.Position
+
+        // TODO: Remove before release
+        app.Camera.Zoom += rl.GetMouseWheelMove() * 0.1
+
         rl.BeginDrawing()
 
         rl.ClearBackground(rl.Black)
@@ -78,7 +89,18 @@ func main() {
         {
             rl.BeginMode2D(app.Camera)
 
-            for _, entity := range app.Entities {
+            for i := 0; i < mapSize * mapSize; i += 1 {
+                x := i % mapSize
+                y := int(i / mapSize)
+
+                backgroundTile := app.BackgroundTiles[i]
+                middlegroundTile:= app.MiddlegroundTiles[i]
+
+                DrawTile(tilesTexture, backgroundTile, x, y)
+                DrawTile(tilesTexture, middlegroundTile, x, y)
+            }
+
+            for _, entity := range app.Entities.GetData() {
                 DrawEntity(entity)
             }
 
diff --git a/tile.go b/tile.go
new file mode 100644 (file)
index 0000000..1240567
--- /dev/null
+++ b/tile.go
@@ -0,0 +1,33 @@
+package main
+
+import rl "github.com/gen2brain/raylib-go/raylib"
+
+type Tile uint8
+
+const TileSize int = 32
+
+func DrawTile(tileMap rl.Texture2D, tile Tile, x int, y int) {
+    tileMapSize := int(tileMap.Width) / TileSize
+    scale := 2
+
+    srcRect := rl.Rectangle{
+        X: float32(int(tile) % tileMapSize * TileSize),
+        Y: float32(int(tile) / tileMapSize * TileSize),
+        Width: float32(TileSize),
+        Height: float32(TileSize),
+    }
+
+    dstRect := rl.Rectangle{
+        X: float32(x * TileSize * scale),
+        Y: float32(y * TileSize * scale),
+        Width: float32(TileSize * scale),
+        Height: float32(TileSize * scale),
+    }
+
+    origin := rl.Vector2{
+        X: float32(TileSize / 2),
+        Y: float32(TileSize / 2),
+    }
+
+    rl.DrawTexturePro(tileMap, srcRect, dstRect, origin, 0, rl.RayWhite)
+}
diff --git a/tools/tile-edit/go.mod b/tools/tile-edit/go.mod
new file mode 100644 (file)
index 0000000..64f7e42
--- /dev/null
@@ -0,0 +1,3 @@
+module tile-edit
+
+go 1.24.0
diff --git a/tools/tile-edit/main.go b/tools/tile-edit/main.go
new file mode 100644 (file)
index 0000000..ea65f2f
--- /dev/null
@@ -0,0 +1,28 @@
+package main
+
+import rl "github.com/gen2brain/raylib-go/raylib"
+
+func main() {
+    rl.InitWindow(800, 600, "Tile Edit")
+    defer rl.CloseWindow()
+
+    rl.SetTargetFPS(60)
+
+    camera := rl.Camera2D{
+        Zoom: 1,
+    }
+
+    for !rl.WindowShouldClose() {
+        rl.BeginDrawing()
+
+        rl.ClearBackground(rl.Black)
+
+        {
+            rl.BeginMode2D(camera)
+
+            rl.EndMode2D()
+        }
+
+        rl.EndDrawing()
+    }
+}