diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cbf3f46 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 y-hatano-github + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9be105c --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +![gocandle](gocandle.gif) + +# What is this? +Fire effect written in Golang. +Seeing fire make you feel calm. + +## How to get and run the code. +### 1st step +Get the code. +``` +go get -d github.com/y-hatano-github/go-candle +``` +### 2nd step +Change directory to the source code directory. +``` +cd $GOPATH/src/github.com/y-hatano-github/go-candle +``` +### 3rd step +Run the code. +``` +go run main.go +``` + +## Key bindings +[esc / CTRL+C] - exit
+ +## Win/Mac/Linux binary +https://github.com/y-hatano-github/go-candle/releases/ diff --git a/go-candle b/go-candle new file mode 100644 index 0000000..af841b2 Binary files /dev/null and b/go-candle differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dc09568 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/y-hatano-github/go-candle + +go 1.12 + +require ( + github.com/mattn/go-runewidth v0.0.7 // indirect + github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..71af854 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= +github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317 h1:hhGN4SFXgXo61Q4Sjj/X9sBjyeSa2kdpaOzCO+8EVQw= +github.com/nsf/termbox-go v0.0.0-20190817171036-93860e161317/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= diff --git a/gocandle.gif b/gocandle.gif new file mode 100644 index 0000000..c32550b Binary files /dev/null and b/gocandle.gif differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..8bdd9b5 --- /dev/null +++ b/main.go @@ -0,0 +1,125 @@ +package main + +import ( + "github.com/nsf/termbox-go" + "math/rand" + // "strconv" + "time" +) + +const WIDTH = 16 +const HEIGHT = 16 +const FLUCTUATION = 5 + +func main() { + var m [WIDTH][HEIGHT]int + for y := 0; y < HEIGHT; y++ { + for x := 0; x < WIDTH; x++ { + m[x][y] = 0 + } + } + + colors := ([]termbox.Attribute{ + 0, + 197, + 203, + 209, + 215, + 221, + 227, + 228, + 229, + 230, + 231}) + + rand.Seed(time.Now().UnixNano()) + + err := termbox.Init() + if err != nil { + panic(err) + } + defer termbox.Close() + termbox.SetOutputMode(termbox.Output256) + termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) + + ch := make(chan termbox.Event) + go keyEvent(ch) + +loop: + for { + + select { + case ev := <-ch: + switch ev.Type { + case termbox.EventKey: + if ev.Key == termbox.KeyCtrlC || ev.Key == termbox.KeyEsc { + break loop + } + } + break + default: + termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) + fire(&m, colors) + drawFire(m, colors) + termbox.Flush() + } + } +} + +func fire(m *[WIDTH][HEIGHT]int, colors []termbox.Attribute) { + for x := 0; x < WIDTH; x++ { + for y := 0; y < HEIGHT; y++ { + + if y < HEIGHT-2 { + if x == WIDTH-1 { + m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1] + m[x][y+2]) / 5 + } else if x == 0 { + m[x][y] = (m[x][y] + m[x][y+1] + m[x+1][y+1] + m[x][y+2]) / 5 + } else { + m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1] + m[x+1][y+1] + m[x][y+2]) / 5 + } + } else if y < HEIGHT-1 { + if x == WIDTH-1 { + m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1]) / 5 + } else if x == 0 { + m[x][y] = (m[x][y] + m[x][y+1] + m[x+1][y+1]) / 5 + } else if x < 3 || x > WIDTH-4 { + m[x][y] = (m[x][y] + m[x-1][y+1] + m[x][y+1] + m[x+1][y+1]) / 5 + } + } else { + if x < 3 || x > WIDTH-4 { + m[x][y] = m[x][y] / 5 + } + } + if rand.Intn(FLUCTUATION) == 1 { + m[x][y] = m[x][y] - 1 + } + if m[x][y] < 0 { + m[x][y] = 0 + } + } + } + + for x := 5; x < WIDTH-5; x++ { + m[x][HEIGHT-1] = rand.Intn(3) + len(colors) - 3 + + } + for x := 4; x < WIDTH-4; x++ { + m[x][HEIGHT-2] = rand.Intn(3) + len(colors) - 3 + } + +} + +func keyEvent(ch chan termbox.Event) { + for { + ch <- termbox.PollEvent() + } +} + +func drawFire(m [WIDTH][HEIGHT]int, colors []termbox.Attribute) { + for x := 0; x < WIDTH; x++ { + for y := 0; y < HEIGHT; y++ { + termbox.SetCell(x+2, y+2, ' ', termbox.ColorDefault, colors[m[x][y]]) + } + } +}