~johanvandegriff/ruegolike

ref: f2dd2fa0ed66a17e5f9ae7829aa1ce22f4e94452 ruegolike/main.go -rw-r--r-- 3.4 KiB
f2dd2fa0Johan Vandegriff tweaked the chars displayed and the movement keys, updated roadmap 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main

import (
	"fmt"
	"math/rand"
	"os"
	"time"

	"github.com/gdamore/tcell"
)

const width, height, depth = 48, 16, 32
const offsetX, offsetY = 1, 2
const debug = false

func main() {
	rand.Seed(time.Now().UnixNano())

	// fmt.Println(NewLevel())

	dungeon, explored, playerPos := Generate()
	// time.Sleep(1 * time.Second)

	tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
	s, e := tcell.NewScreen()
	// fmt.Println(s, e)
	if e != nil {
		fmt.Fprintf(os.Stderr, "%v\n", e)
		os.Exit(1)
	}

	e = s.Init()
	if e != nil {
		fmt.Fprintf(os.Stderr, "%v\n", e)
		os.Exit(1)
	}

	style1 := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorBlack)
	// style2 := tcell.StyleDefault.Foreground(tcell.ColorDarkSlateGray).Background(tcell.ColorBlack)
	// style1 := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorRed)
	// style2 := tcell.StyleDefault.Foreground(tcell.ColorDarkGray).Background(tcell.ColorBlack)
	// style3 := tcell.StyleDefault.Foreground(tcell.ColorWhite).Background(tcell.ColorDarkGray)

	// invert := tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite)

	var visible [height][width]bool
	s.Clear()
	for {
		//player movement
		ev := s.PollEvent()
		switch ev := ev.(type) {
		// case *tcell.EventResize:
		// 	s.Sync()
		case *tcell.EventKey:
			if ev.Key() == tcell.KeyEscape {
				s.Fini()
				os.Exit(0)
			}
			EmitStr(s, 0, 0, style1, fmt.Sprintf("%c", ev.Rune()))
			EmitStr(s, 2, 0, style1, fmt.Sprintf("%s                   ", ev.Name()))
			processMoveEvent(ev.Name(), ev.Rune(), &playerPos, dungeon, s, style1)
		}

		Display(s, playerPos, &visible, &explored[playerPos.z], dungeon.GetLevel(playerPos.z))
	}
}

func processMoveEvent(evName string, evRune rune, playerPos *Position, dungeon *Dungeon, s tcell.Screen, style1 tcell.Style) {
	var deltaX, deltaY int
	if evRune == '1' || evRune == ';' {
		deltaX, deltaY = -1, 1
	} else if evRune == '2' || evName == "Down" {
		deltaX, deltaY = 0, 1
	} else if evRune == '3' || evRune == '\'' {
		deltaX, deltaY = 1, 1
	} else if evRune == '4' || evName == "Left" {
		deltaX, deltaY = -1, 0
	} else if evRune == '5' {
		// deltaX, deltaY = 0, 0 //TODO run
	} else if evRune == '6' || evName == "Right" {
		deltaX, deltaY = 1, 0
	} else if evRune == '7' || evRune == 'p' {
		deltaX, deltaY = -1, -1
	} else if evRune == '8' || evName == "Up" {
		deltaX, deltaY = 0, -1
	} else if evRune == '9' || evRune == '[' {
		deltaX, deltaY = 1, -1
	}
	if deltaX != 0 || deltaY != 0 {
		newPlayerX := playerPos.x + deltaX
		newPlayerY := playerPos.y + deltaY

		if newPlayerX >= 0 && newPlayerX < width &&
			newPlayerY >= 0 && newPlayerY < height &&
			!dungeon.GetTile(Position{newPlayerX, newPlayerY, playerPos.z}).IsSolid() {
			playerPos.x = newPlayerX
			playerPos.y = newPlayerY
			EmitStr(s, 15, 0, style1, "    ")
		} else {
			EmitStr(s, 15, 0, style1, "oof!")
		}
	} else if playerPos.z < depth-1 && (evRune == '.' || evRune == '>') && (debug || dungeon.GetChar(*playerPos) == '>') {
		playerPos.z++
		newPos := dungeon.GetLevel(playerPos.z).FindChar('<')
		playerPos.x = newPos.x
		playerPos.y = newPos.y
	} else if playerPos.z > 0 && (evRune == '.' || evRune == '<') && (debug || dungeon.GetChar(*playerPos) == '<') {
		playerPos.z--
		newPos := dungeon.GetLevel(playerPos.z).FindChar('>')
		playerPos.x = newPos.x
		playerPos.y = newPos.y
	}
	//TODO fast travel command (or click mouse)
}