Last active
July 22, 2019 09:25
-
-
Save bartwttewaall/2bce530a2f4f17e53503cc27986a8fb3 to your computer and use it in GitHub Desktop.
Compress multiple boolean states into a single integer and unpack it on the server or vice versa
This file contains hidden or 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
using System; | |
using UnityEngine; | |
// https://dev.to/somedood/bitmasks-a-very-esoteric-and-impractical-way-of-managing-booleans-1hlf | |
[Flags] | |
public enum KeyFlags { | |
UP = 1 << 0, | |
DOWN = 1 << 1, | |
LEFT = 1 << 2, | |
RIGHT = 1 << 3, | |
JUMP = 1 << 4, | |
SPRINT = 1 << 5, | |
FIRE_MAIN = 1 << 6, | |
FIRE_ALT = 1 << 7, | |
RELOAD = 1 << 8, | |
TELEPORT = 1 << 9, | |
SHIELD = 1 << 10, | |
} | |
public class PlayerInputState { | |
public bool moveUp; | |
public bool moveDown; | |
public bool moveLeft; | |
public bool moveRight; | |
public bool jump; | |
public bool sprint; | |
public bool fireMain; | |
public bool fireAlt; | |
public bool reload; | |
public bool teleport; | |
public bool shield; | |
// Convert state to bitmask flags, either manually or through the FlagsHelper | |
public ushort ToBitflags () { | |
KeyFlags flags = 0; | |
if (moveUp) flags |= KeyFlags.UP; | |
if (moveDown) flags |= KeyFlags.DOWN; | |
if (moveLeft) flags |= KeyFlags.LEFT; | |
if (moveRight) flags |= KeyFlags.RIGHT; | |
if (jump) flags |= KeyFlags.JUMP; | |
if (sprint) flags |= KeyFlags.SPRINT; | |
if (fireMain) flags |= KeyFlags.FIRE_MAIN; | |
if (fireAlt) flags |= KeyFlags.FIRE_ALT; | |
if (reload) flags |= KeyFlags.RELOAD; | |
if (teleport) flags |= KeyFlags.TELEPORT; | |
if (shield) flags |= KeyFlags.SHIELD; | |
return (ushort) flags; | |
} | |
// Convert bitmask flags to booleans or values | |
public static PlayerInputState FromBitflags (int bitflags) { | |
KeyFlags flags = (KeyFlags) bitflags; | |
var newState = new InternalInputState (); | |
newState.moveUp = (flags & KeyFlags.UP) != 0; | |
newState.moveDown = (flags & KeyFlags.DOWN) != 0; | |
newState.moveLeft = (flags & KeyFlags.LEFT) != 0; | |
newState.moveRight = (flags & KeyFlags.RIGHT) != 0; | |
newState.jump = (flags & KeyFlags.JUMP) != 0; | |
newState.sprint = (flags & KeyFlags.SPRINT) != 0; | |
newState.fireMain = (flags & KeyFlags.FIRE_MAIN) != 0; | |
newState.fireAlt = (flags & KeyFlags.FIRE_ALT) != 0; | |
newState.reload = (flags & KeyFlags.RELOAD) != 0; | |
newState.teleport = (flags & KeyFlags.TELEPORT) != 0; | |
newState.shield = (flags & KeyFlags.SHIELD) != 0; | |
return newState; | |
} | |
} |
This file contains hidden or 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
export enum KeyFlags { | |
UP = 1 << 0, | |
DOWN = 1 << 1, | |
LEFT = 1 << 2, | |
RIGHT = 1 << 3, | |
JUMP = 1 << 4, | |
SPRINT = 1 << 5, | |
FIRE_MAIN = 1 << 6, | |
FIRE_ALT = 1 << 7, | |
RELOAD = 1 << 8, | |
TELEPORT = 1 << 9, | |
SHIELD = 1 << 10, | |
} | |
export class PlayerInputState { | |
public moveUp: boolean = false; | |
public moveDown: boolean = false; | |
public moveLeft: boolean = false; | |
public moveRight: boolean = false; | |
public jump: boolean = false; | |
public sprint: boolean = false; | |
public fireMain: boolean = false; | |
public fireAlt: boolean = false; | |
public reload: boolean = false; | |
public teleport: boolean = false; | |
public shield: boolean = false; | |
public async populate(bitflags: number) { | |
this.moveUp = (bitflags & KeyFlags.UP) !== 0; | |
this.moveDown = (bitflags & KeyFlags.DOWN) !== 0; | |
this.moveLeft = (bitflags & KeyFlags.LEFT) !== 0; | |
this.moveRight = (bitflags & KeyFlags.RIGHT) !== 0; | |
this.jump = (bitflags & KeyFlags.JUMP) !== 0; | |
this.sprint = (bitflags & KeyFlags.SPRINT) !== 0; | |
this.fireMain = (bitflags & KeyFlags.FIRE_MAIN) !== 0; | |
this.fireAlt = (bitflags & KeyFlags.FIRE_ALT) !== 0; | |
this.reload = (bitflags & KeyFlags.RELOAD) !== 0; | |
this.teleport = (bitflags & KeyFlags.TELEPORT) !== 0; | |
this.shield = (bitflags & KeyFlags.SHIELD) !== 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment