-
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 all commits
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,84 @@ | ||
package world | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type LayerViewer interface { | ||
ViewLayer() *ViewLayer | ||
} | ||
|
||
type Layer struct { | ||
nameTag string | ||
visibility VisibilityLevel | ||
} | ||
|
||
// 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 overwrites the public name tag of the viewer and allows this ViewLayer to view a different name tag. | ||
// Leaving the name tag empty reverts this behaviour. | ||
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 overwritten name tag of the viewer. | ||
func (v *ViewLayer) NameTag(viewer LayerViewer) string { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
return v.viewers[viewer].nameTag | ||
} | ||
|
||
// ViewVisibility overwrites the public visibility of the viewer and allows this ViewLayer to view | ||
// this viewer as (in)visible depending on the VisibilityLevel. | ||
func (v *ViewLayer) ViewVisibility(viewer LayerViewer, level VisibilityLevel) { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
|
||
l := v.viewers[viewer] | ||
l.visibility = level | ||
v.viewers[viewer] = l | ||
} | ||
|
||
// Visibility returns the visibility of the viewer. | ||
func (v *ViewLayer) Visibility(viewer LayerViewer) VisibilityLevel { | ||
v.viewerMu.Lock() | ||
defer v.viewerMu.Unlock() | ||
return v.viewers[viewer].visibility | ||
} | ||
|
||
// 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package world | ||
|
||
type VisibilityLevel struct { | ||
visibility | ||
} | ||
|
||
// PublicVisibility is the default visibility level where the entity is (in)visible | ||
// depending on how it already is publicly viewed. | ||
func PublicVisibility() VisibilityLevel { | ||
return VisibilityLevel{0} | ||
} | ||
|
||
// EnforceInvisible is the visibility level where the entity is always invisible to the viewer. | ||
func EnforceInvisible() VisibilityLevel { | ||
return VisibilityLevel{1} | ||
} | ||
|
||
// EnforceVisible is the visibility level where the entity is always visible to the viewer. | ||
func EnforceVisible() VisibilityLevel { | ||
return VisibilityLevel{2} | ||
} | ||
|
||
type visibility uint8 | ||
|
||
// EnforceVisibility returns whether metadata should be changed. | ||
func (v visibility) EnforceVisibility() bool { | ||
return v > 0 | ||
} |
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