-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of a view layer #844
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also remove the view layer instance from the viewers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
for viewer := range v.viewers { | ||
delete(v.viewers, viewer) | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should one viewer leaving close the entire view layer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because they left?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if multiple sessions has the same view layer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 session = 1 view layer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then why do view layers store multiple sessions at the moment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
every player has it's own layer, so that each player can be different, only to a certain player