summaryrefslogtreecommitdiff
path: root/internal/pacman/pacman.go
blob: cb663af7224317f816d89ddcb6d05e1ee3b2fa3b (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package pacman

import (
	"time"

	"github.com/ChausseBenjamin/pacgo/internal/render"
)

type Direction uint8

const (
	UP Direction = iota
	DOWN
	LEFT
	RIGHT
)

type Pacman struct {
	renderer *render.Renderer

	bright      bool
	x           float64
	y           float64
	dir         Direction
	refreshRate int // times/second the position is updated

	vSpeed     float64 // tiles per second
	hSpeed     float64 // tiles per second
	moveTicker *time.Ticker
	moveDone   chan bool

	blinkRate   float64 // stateChanges per second
	blinkTicker *time.Ticker
	blinkDone   chan bool
}

func (p *Pacman) blink() {
	p.bright = !p.bright
}

func (p *Pacman) drawInstruction() render.DrawInstruction {
	return render.DrawInstruction{
		X:       int(p.x),
		Y:       int(p.y),
		Content: p.Icon(),
	}
}

// clear will remove the pacman from the screen. This is useful in two cases:
// - Remove pacman's trail/previous position
// - Remove pacman from the screen on death/level change/etc.
// This function will not update pacman's position. This means it must be
// called before updating the position.
func (p *Pacman) clearInstruction() render.ClearInstruction {
	return render.ClearInstruction{
		X:    int(p.x),
		Y:    int(p.y),
		Size: 1,
	}
}

// move will update pacman's position constantly
// based on the direction it is facing.
func (p *Pacman) move() {
	p.renderer.Push(p.clearInstruction())
	switch p.dir {
	case UP:
		p.y -= (float64(p.vSpeed) * float64(p.refreshRate)) / 1000
	case DOWN:
		p.y += (float64(p.vSpeed) * float64(p.refreshRate)) / 1000
	case LEFT:
		p.x -= (float64(p.hSpeed) * float64(p.refreshRate)) / 1000
	case RIGHT:
		p.x += (float64(p.hSpeed) * float64(p.refreshRate)) / 1000
	}
	p.renderer.Push(p.drawInstruction())
}

func (p *Pacman) Pos() (float64, float64) {
	return p.x, p.y
}

func (p *Pacman) Redirect(dir Direction) {
	p.dir = dir
}

func (p *Pacman) Icon() string {
	icns := map[Direction]map[bool]string{
		UP:    {true: "", false: "󰬧"},
		DOWN:  {true: "", false: "󰬭"},
		LEFT:  {true: "", false: "󰬫"},
		RIGHT: {true: "", false: "󰬩"},
	}

	return icns[p.dir][p.bright]
}

func (p *Pacman) Start() {
	p.blinkDone = make(chan bool)
	p.moveDone = make(chan bool)
	// fmt.Println(time.Duration(1/p.blinkRate) * time.Second)
	blinkTicker := time.NewTicker(time.Duration(float64(time.Second) / p.blinkRate))
	moveTicker := time.NewTicker(time.Duration(float64(time.Second) / float64(p.refreshRate)))

	go func() {
		for {
			select {
			case <-blinkTicker.C:
				p.blink()
			case <-moveTicker.C:
				p.move()
			case <-p.blinkDone:
				return
			case <-p.moveDone:
				return
			}
		}
	}()
}

func (p *Pacman) Stop() {
	p.blinkTicker.Stop()
	p.moveTicker.Stop()
	p.blinkDone <- true
	p.moveDone <- true
}

func NewPacman(x float64, y float64, rdr *render.Renderer) *Pacman {
	p := &Pacman{x: x, y: y}
	p.dir = RIGHT
	p.refreshRate = 200
	p.vSpeed = 0.1
	p.hSpeed = 0.2
	p.blinkRate = 3
	p.renderer = rdr
	go p.blink()
	go p.move()
	return p
}

// // Icons
// pub const PACMAN_UP_ON    :char = '';
// pub const PACMAN_DOWN_ON  :char = '';
// pub const PACMAN_LEFT_ON  :char = '';
// pub const PACMAN_RIGHT_ON :char = '';
// pub const GHOST_ON        :char = '󰊠';

// pub const PACMAN_UP_OFF    :char = '󰬧';
// pub const PACMAN_DOWN_OFF  :char = '󰬭';
// pub const PACMAN_LEFT_OFF  :char = '󰬩';
// pub const PACMAN_RIGHT_OFF :char = '󰬫';
// pub const GHOST_OFF        :char = '󱙝';