diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-10-22 21:08:43 +0300 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-10-22 21:09:12 +0300 |
commit | 8b6b1f1ac0e44ee3a1a2be0ee1fbf48617d0552b (patch) | |
tree | 3ae47f372d31f3f3fb6c8ced193a92d5f30b088a | |
parent | 03d6ee47fa09aa25e68b150631cd41e7783f2284 (diff) | |
download | rulmarc-8b6b1f1ac0e44ee3a1a2be0ee1fbf48617d0552b.tar.gz rulmarc-8b6b1f1ac0e44ee3a1a2be0ee1fbf48617d0552b.tar.bz2 rulmarc-8b6b1f1ac0e44ee3a1a2be0ee1fbf48617d0552b.zip |
feat: quick scaffolding for game window
Init: Created initial NPC data structures
UI: Created basic ncurses TUI for development
Types: Created basic helper types
-rw-r--r-- | main.go | 78 | ||||
-rw-r--r-- | menu/menu.go | 22 | ||||
-rw-r--r-- | mknpcs.go | 18 | ||||
-rw-r--r-- | utils/types/menus.go | 10 | ||||
-rw-r--r-- | utils/types/npcs.go | 12 | ||||
-rw-r--r-- | utils/types/types.go | 20 | ||||
-rw-r--r-- | utils/window_helper.go | 6 | ||||
-rw-r--r-- | utils/window_helper/window_helper.go | 13 |
8 files changed, 158 insertions, 21 deletions
@@ -1,10 +1,82 @@ package main import ( - "gitlab.com/stvnliu/ai_game/menu" + "time" + + . "github.com/gbin/goncurses" + "gitlab.com/stvnliu/ai_game/menu" + . "gitlab.com/stvnliu/ai_game/utils/types" + // "gitlab.com/stvnliu/ai_game/utils/window_helper" +) + +const ( + INPUT_PROMPT_LENGTH = 40 + INPUT_PROMPT_HEIGHT = 10 ) +func InputPrompt(scr *Window) string { + my, mx := scr.MaxYX() + w, err := NewWindow(INPUT_PROMPT_HEIGHT, INPUT_PROMPT_LENGTH, (my/2)-(INPUT_PROMPT_HEIGHT/2), (mx/2)-(INPUT_PROMPT_LENGTH/2)) + if err != nil { + panic("Oh no sth went wrong in input!!") + } + w.Box(0, 0) + Echo(true) + msg := "Game name: " + w.MovePrint(0, 1, " New game information ") + w.MovePrint(2, 2, msg) + w.Move(2, 2+len(msg)) + input, err := w.GetString(16) // character input box + if err != nil { + panic("Oh no sth went wrong in input 2!!") + } + w.MovePrint(3, 2, input) + w.Refresh() + Echo(false) + for { + ch := w.GetChar() + switch Key(ch) { + case 'q': + return input + } + } +} +func NewGame(scr *Window) { + //_, _ := scr.MaxYX() + game_name := InputPrompt(scr) + // create new game state + // println("Creating new game %v...", game_name) + game := Game{ + SaveGame: game_name, + LastSaved: time.Now(), + } + my_npcs := MakeNpcs() + game.DataStored.Npcs = my_npcs + scr.MovePrintf(1, 2, "Created new game \"%v\"!", game.SaveGame) + for i:=0; i<len(game.DataStored.Npcs); i++ { + scr.MovePrintf(2+i, 2, "Initialising \"%v\"...", game.DataStored.Npcs[i].Name) + scr.MovePrintf(3+i, 2, "Found NPC query string!") + scr.Refresh() + + } + // println(game.DataStored.Npcs[0].Name) +} +func Continue(scr *Window) { + // recover state from last save? +} +func Exit(scr *Window) { + // save game state? +} func main() { - menu_items := []string{"New game", "Continue", "Exit"} - menu.CreateMenu(menu_items) + scr, err := Init() + if err != nil { + println("Something went wrong with Ncurses! Aborting!") + return + } + menu_items := []GameMenuItem{ + {Name: "New game!", Operation: NewGame}, + {Name: "Continue!", Operation: Continue}, + {Name: "Exit!", Operation: Exit}, + } + menu.CreateMenu(scr, menu_items) } diff --git a/menu/menu.go b/menu/menu.go index 51bb89d..8b7ac16 100644 --- a/menu/menu.go +++ b/menu/menu.go @@ -2,7 +2,8 @@ package menu import ( . "github.com/gbin/goncurses" - "log" + . "gitlab.com/stvnliu/ai_game/utils/types" + // "log" ) const ( @@ -10,12 +11,8 @@ const ( WIDTH = 30 ) -func CreateMenu(menu []string) { +func CreateMenu(stdscr *Window, menu []GameMenuItem) { var active int - stdscr, err := Init() - if err != nil { - log.Fatal(err) - } defer End() Raw(true) @@ -28,7 +25,7 @@ func CreateMenu(menu []string) { y, x := 2, (mx/2)-(WIDTH/2) win, _ := NewWindow(HEIGHT, WIDTH, y, x) - win.Keypad(true) + win.Keypad(true) stdscr.Print("Use arrow keys to go up and down, Press enter to select") stdscr.Refresh() @@ -55,7 +52,8 @@ func CreateMenu(menu []string) { case KEY_RETURN, KEY_ENTER, Key('\r'): stdscr.MovePrintf(my-2, 0, "Choice #%d: %s selected", active, - menu[active]) + menu[active].Name) + menu[active].Operation(stdscr) stdscr.ClearToEOL() stdscr.Refresh() default: @@ -69,16 +67,16 @@ func CreateMenu(menu []string) { } } -func printmenu(w *Window, menu []string, active int) { +func printmenu(w *Window, menu []GameMenuItem, active int) { y, x := 2, 2 w.Box(0, 0) - for i, s := range menu { + for i, item := range menu { if i == active { w.AttrOn(A_REVERSE) - w.MovePrint(y+i, x, s) + w.MovePrint(y+i, x, item.Name) w.AttrOff(A_REVERSE) } else { - w.MovePrint(y+i, x, s) + w.MovePrint(y+i, x, item.Name) } } w.Refresh() diff --git a/mknpcs.go b/mknpcs.go new file mode 100644 index 0000000..5dd24c1 --- /dev/null +++ b/mknpcs.go @@ -0,0 +1,18 @@ +package main + +import ( + . "gitlab.com/stvnliu/ai_game/utils/types" +) + +func MakeNpcs() []Npc { + npcs := []Npc {} + helper01 := Npc{ + Name: "Helper01_NPC", + Ai: NpcAi{ + PromptCharacterString: "You are a new helper assisting new players of a role-playing game set in $SCENE$, in a village called $VILLAGE$. With the information immediately preceeding, output only what you would say to a new player who just arrived in the village to provide helpful guidance.", + QueryFromTableName: "helper", + }, + } + npcs = append(npcs, helper01) + return npcs +} diff --git a/utils/types/menus.go b/utils/types/menus.go new file mode 100644 index 0000000..62288d5 --- /dev/null +++ b/utils/types/menus.go @@ -0,0 +1,10 @@ +package types + +import ( + . "github.com/gbin/goncurses" +) + +type GameMenuItem struct { + Name string + Operation func(*Window) +} diff --git a/utils/types/npcs.go b/utils/types/npcs.go new file mode 100644 index 0000000..00230d7 --- /dev/null +++ b/utils/types/npcs.go @@ -0,0 +1,12 @@ +package types + +type NpcAi struct { + PromptCharacterString string + QueryFromTableName string +} +type Npc struct { + Name string + Ai NpcAi +} + + diff --git a/utils/types/types.go b/utils/types/types.go new file mode 100644 index 0000000..ea8cbab --- /dev/null +++ b/utils/types/types.go @@ -0,0 +1,20 @@ +package types + +import "time" + + +type Data struct { + Npcs []Npc +} +type Game struct { + SaveGame string + LastSaved time.Time + DataStored Data +} + +func (game Game) NewGame(game_name string, data Data) Game { + game.SaveGame = game_name + game.LastSaved = time.Now() + game.DataStored = data + return game +} diff --git a/utils/window_helper.go b/utils/window_helper.go deleted file mode 100644 index b11bd02..0000000 --- a/utils/window_helper.go +++ /dev/null @@ -1,6 +0,0 @@ -package window -import ( - . "github.com/gbin/goncurses" -) -func createWindow(scr Screen) Window {} -} diff --git a/utils/window_helper/window_helper.go b/utils/window_helper/window_helper.go new file mode 100644 index 0000000..bb06c66 --- /dev/null +++ b/utils/window_helper/window_helper.go @@ -0,0 +1,13 @@ +package window_helper +import ( + . "github.com/gbin/goncurses" +) + +func CreateMenu(win *Window, menu_string []string) { + x, y := 2, 2 + win.Clear() + win.Box(0, 0) + for i, str := range menu_string { + win.MovePrint(y+i, x, str) + } +} |