Skip to main content

Book

A class in Root.Township, Alta.Pages.

A networked book item that holds pages (recipe books, journals) from a PageCollection. The server keeps the page list and turns pages; clients see the spawned page objects and the page count it sends.

Constructors​

MemberWhat it is
Book()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

68 private and internal fields
MemberWhat it is
private static int _IsFromMiddleAnimator hash for IsFromMiddle.
private static int _IsFrontAnimator hash for IsFront.
private static int _IsInteractingAnimator hash for IsInteracting.
private static int _IsOpenAnimator hash for IsOpen.
private static int _MainTexShader property ID for _MainTex.
private static int _OpenAnimator hash for Open.
private GameObject addHighlightObject shown when a page can be dropped in.
private MethodSyncClass<NewPageArguments> addPageMethod sync for the add-page message.
private Transform backCoverBack cover transform.
private GameObject backObjectBack cover object.
private Animator bookAnimatorThe book's animator.
private LODGroup bookLodGroupLOD group that flairs are added to.
private AnimatedBool bookOpenAnimationNetworked Open animator bool.
private List<PageData> bookPagesServer: the pages in the book, in order.
private bool btDuplicateEditor button for DuplicatePage.
private bool btnCloseEditor button for Close.
private bool btnFillEditor button for Fill.
private bool btnLeftEditor button for TurnLeftDebug.
private bool btnOpenEditor button for Open.
private bool btnRightEditor button for TurnRightDebug.
private uint clientPageCountPage count as last synced; PageCount returns it on clients.
private float closedCoverRotationCover rotation when closed, set to match the art.
private float closeDurationSeconds the close animation takes.
private float closeUpwardsDotLimitThe book closes when its forward vector's upward part drops below this.
private float closeVelocityLimitThe book closes when moved faster than this.
private PageCollection collectionThe PageCollection this book holds. It is the only thing the book saves.
private Image coverGlyphImage for the cover glyph.
private int displayedSpawnIndexWhich spawned page is the one shown, or -1.
private int firstPageIndexIndex in bookPages of the first spawned page.
private Book.FlairPositions flairPositionsCover attach points for flairs, plus the book renderers and letter text.
private Transform frontCoverFront cover transform.
private GameObject frontObjectFront cover object.
private Renderer glyphAndroidRendererAndroid glyph renderer, removed at build time.
private Canvas glyphCanvasCanvas the cover glyph is drawn on.
private Coroutine heldCoroutineServer: the running Held coroutine.
private List<Pickup> hoveringLoose pages in the drop area being watched.
private Queue<PageData> insertingPages waiting to be inserted while the book is open.
private int lastOpenedPagePage the book was on when last closed; it reopens there.
private MethodSync locationChangeMethod sync for the page-location-changed message.
private LOD[] lodsCached LOD array of the book.
private static Logger loggerNLog logger for this class.
private float openDurationSeconds the open animation takes.
private float openUpwardsDotLimitThe book opens when its forward vector's upward part is above this.
private float openVelocityLimitThe book only opens when moving slower than this.
private Transform pageAxisParent transform spawned pages hang from.
private TextRenderer pageCountText showing pages found out of the total.
private AltaEvent pageLocationChangedEvent played when a newly added page opens the book somewhere else.
private TextRenderer pageNumberText showing the current page number.
private NetworkPrefab pagePrefabPrefab spawned for each page shown in the book.
private SoundEffect pageTurningSoundSound played on a page turn.
private NetworkPrefab paperPrefabPrefab of a loose paper page, spawned by DuplicatePage.
private Pickup pickupThe book's pickup.
private PickupArea pickupAreaArea that detects loose pages to add.
private MethodSyncStruct<int> removePageMethod sync for the remove-page message.
private Book.LODRenderers[] renderersCached base renderers per LOD level, used when rebuilding flairs.
private MethodSyncStruct<bool> serverFlipPageMethod sync for client turn-page requests.
private MethodSyncClass<DisplayArguments> setDisplayMethod sync for the page count and number text.
private MethodSyncStruct<bool> setHighlightMethod sync for the add-page highlight.
private MethodSyncStruct<bool> setOpenMethod sync for open and close.
private List<BookPageInstance> spawnedPagesPage entities spawned in the open book, in order.
private GameObject spineObjectSpine object.
private MethodSync syncMethod sync for the initial book sync.
private int turnDirectionDirection of the turn in progress: -1, 0 or 1.
private List<PageInstance> turningPages in the middle of turning.
private MethodSyncClass<TurnPageArguments> turnPageMethod sync for page turns.
private float turnRateSeconds between repeated page turns while input is held.
private float turnSkipProgressWhere a skipped turn starts in its animation, 0 to 1.
private float turnTimeSeconds a page turn takes.

Properties​

MemberWhat it is
PageCollection Collection { get; }The PageCollection that defines which pages this book can hold and their order. A book spawned without one destroys itself.
bool IsAtBack { get; private set; }True when the open book shows its last page.
bool IsAtFront { get; private set; }True when the open book shows its first page.
bool IsChangingOpenState { get; private set; }True while the open or close animation runs. Page turns are refused then.
bool IsInserting { get; }True while pages are queued to be animated into the open book.
bool IsOpen { get; private set; }Whether the book is open. The server opens it while held facing up and slow, and closes it otherwise.
int PageCount { get; }Number of pages in the book. On clients this is the count last sent by the server.
Pickup Pickup { get; }The book's Pickup. Holding it blocks locomotion and lets the holder turn pages.
1 private and internal property
MemberWhat it is
private int IBuildRendererBakeable.SortingOrder { get; }Bake order for this renderer bakeable: 5.

Methods​

MemberWhat it is
bool AddNewPage(PageData page)Server: adds a page in collection order and animates it in if the book is open. False on clients or if the collection lacks the page.
void BookRemovePage(PageData pageData)Removes a page from the book's list and refreshes the open spread and page display. Called when a page is pulled out.
void Close()Closes the book if it is open and not already opening or closing. The server closes it when it is dropped or tilted away.
PaperPageInstance DuplicatePage(PageData pageData, ref bool isDuplicate)Server: spawns a loose paper copy of a page at the book. Always a duplicate if the collection forbids removal. Null on clients.
override void Initialize()Sets up the network messages for sync, adding, removing and turning pages, opening, highlighting and flipping.
override void InitializeAsServer(SpawnData spawnData)Server: destroys the book if it has no collection, otherwise adds every collection page already unlocked globally and listens for new ones.
override void OnConnected(Player player)Server: sends a joining player the book's state, its spawned pages, page display and open state.
void Open()Opens the book if it is closed and not already changing state.
void SetCollection(PageCollection collection)Server: sets the page collection. Ignored on clients.
bool TurnLeft(bool isSkipping = false)Server: turns forward one page if the book is open and not at the back. Returns whether a turn started.
bool TurnRight(bool isSkipping = false)Server: turns back one page if the book is open and not at the front. Returns whether a turn started.
46 private and internal methods
MemberWhat it is
private void AddFlair(BookFlair baseFlair, BookFlair overrideFlair, Transform parent, Material material, List<Renderer>[] renderers)Adds the override flair, or the base one if none, at a cover position with the flair material.
private void AddPage(BookPageInstance newPage, int spawnedIndex, bool isFromMiddle = false)Puts a spawned page into the book at a slot and sets its animator; overload is the add-page message handler on clients.
private void AddPage(IPlayer player, Stream stream, NewPageArguments newPage)Puts a spawned page into the book at a slot and sets its animator; overload is the add-page message handler on clients.
private void ApplyFlairs()Rebuilds the cover decoration (emblem, glyph or letter, spine and edge flairs, book material) from the collection's flair settings.
private void Awake()Hooks pickup grab, drop and interaction filter, and hides the page drop area.
private void IBuildRendererBakeable.Bake(bool isSaving)Build step that removes the Android glyph renderer.
private bool CacheLods()Editor setup that caches the renderers of each LOD level, so flairs can be added later.
private IEnumerator ChangeOpen(bool isOpening) (iterator)Coroutine that opens or closes the book, spawning pages at the last opened page on open and removing them on close.
private void CheckGlobalPage(ValueTuple<int, uint> data)Server: when a page is unlocked globally, adds it to this book if it belongs to the collection.
private void CheckInsert(Interactable pickup, Interactor interactor)When a loose page is let go over the book, adds it, records it globally and destroys the loose page.
private void IBuildRendererBakeable.Clean()Build clean step; does nothing.
private void ClientMoveLeft(PlayerInput input)Client: asks the server to turn left when the move-left action starts.
private void ClientMoveRight(PlayerInput input)Client: asks the server to turn right when the move-right action starts.
private void ClientSwiped(PlayerInput playerInput)Client: asks the server to turn a page from a left or right swipe.
private IEnumerator DelayToInteract(BookPageInstance newPage, bool isFromMiddle) (iterator)Keeps a new page non-interactable for the open duration.
private void DestroyPage(int spawnedIndex)Removes a spawned page from the open book; on the server it also destroys the entity and tells clients.
private void Dropped(Interactable interactable, Interactor interactor)Unblocks locomotion, stops the held check and closes the book when nobody holds it.
private void Fill()Debug: on an empty server book, adds every page of the collection and unlocks them globally.
private IEnumerator FinishTurning(BookPageInstance page, int direction, bool isSkipping) (iterator)Coroutine that finishes a page turn, then on the server removes pages that went out of view.
private int GetCurrentPage()Index in bookPages of the page shown, or -1.
private int GetPageNumber()Page number to display: the shown page's index in the collection, or -1.
private IEnumerator Held(Interactor interactor) (iterator)Server coroutine while held: opens the book when it faces up and is still, closes it otherwise, and turns pages from input.
private IEnumerator InsertPage(PageData page) (iterator)Coroutine that waits for turns to stop, then reopens the book at a newly added page.
private void InteractionFilter(Interactor interactor, Interactable interactable, ref bool result)Stops a player grabbing the book with a second hand while already holding it.
private void ListenForPageDrop(Pickup pickup)When a loose page from this collection enters the drop area, shows the highlight and waits for it to be let go.
private void LocationChange(IPlayer player, Stream stream)Client handler that plays the page-location-changed event.
private void OnDestroy()Unhooks the global page-added event.
private void PickedUp(Interactable interactable, Interactor interactor)Blocks locomotion for the holder, starts the server held check and hooks the local player's page-turn input.
private void RemovePage(IPlayer player, Stream stream, int oldPage)Client handler for the remove-page message.
private void SerializeBook(Stream stream, bool isIncludingPages)Writes or reads the collection and page count (and the pages themselves, if asked).
private void ServerFlipPage(IPlayer player, Stream stream, bool flipRight)Server handler for a client's turn-page request.
private void SetDisplay(IPlayer player, Stream stream, DisplayArguments arguments)Message handler that updates the page count and page number text.
private void SetHighlight(IPlayer player, Stream stream, bool isHighlighted)Client handler that shows or hides the add-page highlight.
private void SetOpen(IPlayer player, Stream stream, bool isOpen)Client handler that opens or closes the book.
private void ShowHighlight(bool isShowing)Shows or hides the add-page highlight; the server also sends it to clients.
private PageInstance SpawnPage(int index, int spawnedIndex, bool isFromMiddle = false)Server: spawns a page entity for a book page and adds it at a slot on every side.
private void Start()Gets the networked Open animator bool and starts closed.
private void StartListeningForDrops()Turns on the drop area for adding loose pages.
private void StopListenForPageDrop(Pickup pickup)Stops watching a loose page that left the drop area.
private void StopListeningForDrops()Turns off the drop area and stops watching every hovering page.
private void Sync(IPlayer player, Stream stream)Initial sync handler: sends the collection and page count, and clients apply the flairs.
private void TurnLeftDebug()Editor button for TurnLeft.
private void TurnPage(int spawnedIndex, int direction, bool isSkipping)Plays a page turn locally; overload is the client handler for the turn message.
private void TurnPage(IPlayer player, Stream stream, TurnPageArguments turnPage)Plays a page turn locally; overload is the client handler for the turn message.
private void TurnPageAsServer(int spawnedIndex, int direction, bool isSkipping)Server: turns a spawned page, tells clients and spawns the next page in that direction.
private void TurnRightDebug()Editor button for TurnRight.

Used in​

Members of other types that take, return or hold a Book: