From 4b7358a1d02d8d2660c1a954c2885f660bdee16b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 18 Jul 2024 12:23:16 +0800 Subject: [PATCH] :tada: Initial Commit of Basic things like movement of player --- .idea/.gitignore | 8 ++++ .idea/CodingLand.iml | 9 ++++ .idea/modules.xml | 8 ++++ go.mod | 5 +++ go.sum | 2 + pkg/internal/common/vector2d.go | 5 +++ pkg/internal/entities/player.go | 38 ++++++++++++++++ pkg/main.go | 77 +++++++++++++++++++++++++++++++++ 8 files changed, 152 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/CodingLand.iml create mode 100644 .idea/modules.xml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/internal/common/vector2d.go create mode 100644 pkg/internal/entities/player.go create mode 100644 pkg/main.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/CodingLand.iml b/.idea/CodingLand.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/CodingLand.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..71aad19 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..283bf20 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.solsynth.dev/highland/codingland + +go 1.22.5 + +require github.com/veandco/go-sdl2 v0.4.40 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bc27727 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/veandco/go-sdl2 v0.4.40 h1:fZv6wC3zz1Xt167P09gazawnpa0KY5LM7JAvKpX9d/U= +github.com/veandco/go-sdl2 v0.4.40/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY= diff --git a/pkg/internal/common/vector2d.go b/pkg/internal/common/vector2d.go new file mode 100644 index 0000000..92ab136 --- /dev/null +++ b/pkg/internal/common/vector2d.go @@ -0,0 +1,5 @@ +package common + +type Vector2D struct { + X, Y float64 +} diff --git a/pkg/internal/entities/player.go b/pkg/internal/entities/player.go new file mode 100644 index 0000000..527ba65 --- /dev/null +++ b/pkg/internal/entities/player.go @@ -0,0 +1,38 @@ +package entities + +import ( + "git.solsynth.dev/highland/codingland/pkg/internal/common" + "github.com/veandco/go-sdl2/sdl" +) + +const ( + Acceleration = 0.5 + Friction = 0.9 +) + +type Player struct { + Position common.Vector2D + Size common.Vector2D + Velocity common.Vector2D +} + +func (p *Player) Move(deltaX, deltaY float64) { + p.Velocity.X += deltaX * Acceleration + p.Velocity.Y += deltaY * Acceleration + + p.Position.X += p.Velocity.X + p.Position.Y += p.Velocity.Y + + p.Velocity.X *= Friction + p.Velocity.Y *= Friction +} + +func (p *Player) Draw(pen *sdl.Renderer) { + pen.SetDrawColor(255, 255, 255, 255) + pen.FillRect(&sdl.Rect{ + X: int32(p.Position.X), + Y: int32(p.Position.Y), + W: int32(p.Size.X), + H: int32(p.Size.Y), + }) +} diff --git a/pkg/main.go b/pkg/main.go new file mode 100644 index 0000000..a3c3ae0 --- /dev/null +++ b/pkg/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "git.solsynth.dev/highland/codingland/pkg/internal/common" + "git.solsynth.dev/highland/codingland/pkg/internal/entities" + "log" + "runtime" + + "github.com/veandco/go-sdl2/sdl" +) + +const ( + windowWidth = 640 + windowHeight = 480 + playerSize = 50 +) + +func init() { + runtime.LockOSThread() +} + +func main() { + if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil { + log.Fatalf("Failed to initialize SDL: %s", err) + } + defer sdl.Quit() + + window, err := sdl.CreateWindow("CodingLand", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, sdl.WINDOW_SHOWN) + if err != nil { + log.Fatalf("Failed to create window: %s", err) + } + defer window.Destroy() + + renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC) + if err != nil { + log.Fatalf("Failed to create renderer: %s", err) + } + defer renderer.Destroy() + + player := &entities.Player{ + Position: common.Vector2D{X: windowWidth / 2, Y: windowHeight / 2}, + Size: common.Vector2D{X: playerSize, Y: playerSize}, + } + + running := true + for running { + for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { + switch event.(type) { + case *sdl.QuitEvent: + running = false + } + } + + keys := sdl.GetKeyboardState() + if keys[sdl.SCANCODE_UP] == 1 || keys[sdl.SCANCODE_W] == 1 { + player.Move(0, -1) + } + if keys[sdl.SCANCODE_DOWN] == 1 || keys[sdl.SCANCODE_S] == 1 { + player.Move(0, 1) + } + if keys[sdl.SCANCODE_LEFT] == 1 || keys[sdl.SCANCODE_A] == 1 { + player.Move(-1, 0) + } + if keys[sdl.SCANCODE_RIGHT] == 1 || keys[sdl.SCANCODE_D] == 1 { + player.Move(1, 0) + } + + player.Move(0, 0) + + renderer.SetDrawColor(0, 0, 0, 255) + renderer.Clear() + + player.Draw(renderer) + + renderer.Present() + } +}