Skip to content

Commit

Permalink
termui v2
Browse files Browse the repository at this point in the history
  • Loading branch information
George-Spanos committed Apr 14, 2023
1 parent f566e8a commit 5d0042e
Show file tree
Hide file tree
Showing 568 changed files with 252,049 additions and 497 deletions.
2 changes: 1 addition & 1 deletion assets/enemies/teenage-wyvern.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name":"Teenage Wyvern",
"description": "A wyvern with a bit of a temper.",
"health": 100,
"health": 10,
"attack": 15,
"defense": 0,
"stance": "Normal",
Expand Down
15 changes: 12 additions & 3 deletions combat/combat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package combat

import "fmt"
import (
"fmt"
"sync"
)

const (
Won = 1
Expand Down Expand Up @@ -40,9 +43,14 @@ type PlayerAction struct {
}

func (playerAction *PlayerAction) Execute(round *Round) {
playerAction.Action.Execute(round.Player, &playerAction.Target.Combatant)
// guard has no target
if playerAction.Target == nil {
playerAction.Action.Execute(round.Player, nil)
} else {
playerAction.Action.Execute(round.Player, &playerAction.Target.Combatant)
}
}
func Start(player *Combatant, enemies ...*Enemy) *Combat {
func Start(wg *sync.WaitGroup, player *Combatant, enemies ...*Enemy) *Combat {
rounds := make([]Round, 0)
playerActionChan := make(chan PlayerAction)
updateUi := make(chan bool)
Expand All @@ -64,6 +72,7 @@ func Start(player *Combatant, enemies ...*Enemy) *Combat {
if combat.Status != Playing {
close(combat.UpdateUi)
close(combat.PlayerActionChan)
wg.Done()
} else {
combat.StartNewRound()
}
Expand Down
177 changes: 8 additions & 169 deletions example.go
Original file line number Diff line number Diff line change
@@ -1,179 +1,18 @@
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

// import (
// "errors"
// "fmt"
// "log"
// "strings"

// "github.com/jroimartin/gocui"
// )

// const delta = 0.2

// type HelpWidget struct {
// name string
// x, y int
// w, h int
// body string
// }

// func NewHelpWidget(name string, x, y int, body string) *HelpWidget {
// lines := strings.Split(body, "\n")

// w := 0
// for _, l := range lines {
// if len(l) > w {
// w = len(l)
// }
// }
// h := len(lines) + 1
// w = w + 1

// return &HelpWidget{name: name, x: x, y: y, w: w, h: h, body: body}
// }

// func (w *HelpWidget) Layout(g *gocui.Gui) error {
// v, err := g.SetView(w.name, w.x, w.y, w.x+w.w, w.y+w.h)
// if err != nil {
// if err != gocui.ErrUnknownView {
// return err
// }
// fmt.Fprint(v, w.body)
// }
// return nil
// }
// "os"

// type StatusbarWidget struct {
// name string
// x, y int
// w int
// val float64
// }

// func NewStatusbarWidget(name string, x, y, w int) *StatusbarWidget {
// return &StatusbarWidget{name: name, x: x, y: y, w: w}
// }

// func (w *StatusbarWidget) SetVal(val float64) error {
// if val < 0 || val > 1 {
// return errors.New("invalid value")
// }
// w.val = val
// return nil
// }

// func (w *StatusbarWidget) Val() float64 {
// return w.val
// }

// func (w *StatusbarWidget) Layout(g *gocui.Gui) error {
// v, err := g.SetView(w.name, w.x, w.y, w.x+w.w, w.y+2)
// if err != nil && err != gocui.ErrUnknownView {
// return err
// }
// v.Clear()

// rep := int(w.val * float64(w.w-1))
// fmt.Fprint(v, strings.Repeat("▒", rep))
// return nil
// }

// type ButtonWidget struct {
// name string
// x, y int
// w int
// label string
// handler func(g *gocui.Gui, v *gocui.View) error
// }
// termui "useless_dragon/termui/v2"

// func NewButtonWidget(name string, x, y int, label string, handler func(g *gocui.Gui, v *gocui.View) error) *ButtonWidget {
// return &ButtonWidget{name: name, x: x, y: y, w: len(label) + 1, label: label, handler: handler}
// }

// func (w *ButtonWidget) Layout(g *gocui.Gui) error {
// v, err := g.SetView(w.name, w.x, w.y, w.x+w.w, w.y+2)
// if err != nil {
// if err != gocui.ErrUnknownView {
// return err
// }
// if _, err := g.SetCurrentView(w.name); err != nil {
// return err
// }
// if err := g.SetKeybinding(w.name, gocui.KeyEnter, gocui.ModNone, w.handler); err != nil {
// return err
// }
// fmt.Fprint(v, w.label)
// }
// return nil
// }
// tea "github.com/charmbracelet/bubbletea"
// )

// func main() {
// g, err := gocui.NewGui(gocui.OutputNormal)
// if err != nil {
// log.Panicln(err)
// }
// defer g.Close()

// g.Highlight = true
// g.SelFgColor = gocui.ColorRed

// help := NewHelpWidget("help", 1, 1, helpText)
// status := NewStatusbarWidget("status", 1, 7, 50)
// butdown := NewButtonWidget("butdown", 52, 7, "DOWN", statusDown(status))
// butup := NewButtonWidget("butup", 58, 7, "UP", statusUp(status))
// g.SetManager(help, status, butdown, butup)

// if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
// log.Panicln(err)
// p := tea.NewProgram(termui.InitialModel())
// if _, err := p.Run(); err != nil {
// fmt.Printf("Alas, there's been an error: %v", err)
// os.Exit(1)
// }
// if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, toggleButton); err != nil {
// log.Panicln(err)
// }

// if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
// log.Panicln(err)
// }
// }

// func quit(g *gocui.Gui, v *gocui.View) error {
// return gocui.ErrQuit
// }

// func toggleButton(g *gocui.Gui, v *gocui.View) error {
// nextview := "butdown"
// if v != nil && v.Name() == "butdown" {
// nextview = "butup"
// }
// _, err := g.SetCurrentView(nextview)
// return err
// }

// func statusUp(status *StatusbarWidget) func(g *gocui.Gui, v *gocui.View) error {
// return func(g *gocui.Gui, v *gocui.View) error {
// return statusSet(status, delta)
// }
// }

// func statusDown(status *StatusbarWidget) func(g *gocui.Gui, v *gocui.View) error {
// return func(g *gocui.Gui, v *gocui.View) error {
// return statusSet(status, -delta)
// }
// }

// func statusSet(sw *StatusbarWidget, inc float64) error {
// val := sw.Val() + inc
// if val < 0 || val > 1 {
// return nil
// }
// return sw.SetVal(val)
// }

// const helpText = `KEYBINDINGS
// Tab: Move between buttons
// Enter: Push button
// ^C: Exit`
15 changes: 13 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@ module useless_dragon
go 1.20

require (
github.com/jroimartin/gocui v0.5.0
github.com/charmbracelet/bubbletea v0.23.2
github.com/charmbracelet/lipgloss v0.7.1
golang.org/x/term v0.7.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
46 changes: 41 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4=
github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbletea v0.23.2 h1:vuUJ9HJ7b/COy4I30e8xDVQ+VRDUEFykIjryPfgsdps=
github.com/charmbracelet/bubbletea v0.23.2/go.mod h1:FaP3WUivcTM0xOKNmhciz60M6I+weYLF76mr1JyI7sM=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.14.0/go.mod h1:kG/pF1E7fh949Xhe156crRUrHNyK221IuGO7Ez60Uc8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
26 changes: 18 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@ import (
"fmt"
"log"
"os"
"sync"
"useless_dragon/combat"
"useless_dragon/config"
"useless_dragon/setup"
"useless_dragon/termui"

tea "github.com/charmbracelet/bubbletea"
)

func main() {
wg := &sync.WaitGroup{}
config.Parse()
player := parsePlayer()
encounters := setup.ParseEncounters()
gcui := termui.Create()
defer gcui.Close()
for _, enemies := range encounters {
c := combat.Start(player, enemies...)
err := termui.RenderCombat(gcui, c)
if err != nil {
gcui.Close()
panic(err)
wg.Add(len(encounters))
updaterChan := make(chan *combat.Combat)
p := tea.NewProgram(termui.InitialModel(updaterChan))
go func(wg *sync.WaitGroup) {
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}(wg)
for i, enemies := range encounters {
c := combat.Start(wg, player, enemies...)
log.Println("to start combat", i)
updaterChan <- c
log.Println("combat", i, "ended")
}
wg.Wait()
}
func parsePlayer() *combat.Combatant {
dir, err := os.Getwd()
Expand Down
Loading

0 comments on commit 5d0042e

Please sign in to comment.