-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: RestartFU <[email protected]>
- Loading branch information
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package session | ||
|
||
import "github.com/df-mc/dragonfly/server/world" | ||
|
||
type LayerViewer interface { | ||
ViewLayer() *world.ViewLayer | ||
} | ||
|
||
func (s *Session) ViewLayer() *world.ViewLayer { | ||
return s.viewLayer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package world | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type LayerViewer interface { | ||
ViewLayer() *ViewLayer | ||
} | ||
|
||
type Layer struct { | ||
nameTag string | ||
invisible bool | ||
} | ||
|
||
// ViewLayer is a view layer that can be used to add a layer to the view of a player. | ||
type ViewLayer struct { | ||
viewerMu sync.RWMutex | ||
viewers map[LayerViewer]Layer | ||
} | ||
|
||
// NewViewLayer returns a new ViewLayer. | ||
func NewViewLayer() *ViewLayer { | ||
return &ViewLayer{ | ||
viewers: map[LayerViewer]Layer{}, | ||
} | ||
} | ||
|
||
// Viewers returns all viewers of the view layer. | ||
func (v *ViewLayer) Viewers() []LayerViewer { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
viewers := make([]LayerViewer, 0, len(v.viewers)) | ||
for viewer := range v.viewers { | ||
viewers = append(viewers, viewer) | ||
} | ||
return viewers | ||
} | ||
|
||
// ViewNameTag adds a name tag to a viewer. | ||
func (v *ViewLayer) ViewNameTag(viewer LayerViewer, nameTag string) { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
|
||
l := v.viewers[viewer] | ||
l.nameTag = nameTag | ||
v.viewers[viewer] = l | ||
} | ||
|
||
// NameTag returns the name tag of a viewer. | ||
func (v *ViewLayer) NameTag(viewer LayerViewer) string { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
return v.viewers[viewer].nameTag | ||
} | ||
|
||
// ViewVisible makes a viewer be visible. | ||
func (v *ViewLayer) ViewVisible(viewer LayerViewer) { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
|
||
l := v.viewers[viewer] | ||
l.invisible = false | ||
v.viewers[viewer] = l | ||
} | ||
|
||
// ViewInvisible makes a viewer be invisible. | ||
func (v *ViewLayer) ViewInvisible(viewer LayerViewer) { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
|
||
l := v.viewers[viewer] | ||
l.invisible = true | ||
v.viewers[viewer] = l | ||
} | ||
|
||
// Invisible returns the invisibility of a viewer. | ||
func (v *ViewLayer) Invisible(viewer LayerViewer) bool { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
return v.viewers[viewer].invisible | ||
} | ||
|
||
// Close closes the view layer. | ||
func (v *ViewLayer) Close() error { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
for viewer := range v.viewers { | ||
delete(v.viewers, viewer) | ||
} | ||
return nil | ||
} |