Advertisement
lunkums

Keyboard Controller (good for multiplayer or game states)

Oct 16th, 2022
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | Gaming | 0 0
  1. namespace Lunkums.Controller
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using UnityEngine;
  6.  
  7.     public class KeyboardController : IController
  8.     {
  9.         private KeyBinding keyPressBindings;
  10.         private KeyBinding keyReleaseBindings;
  11.         private KeyBinding keyHeldBindings;
  12.  
  13.         public KeyboardController() : this(new Dictionary<KeyCode, Action>(),
  14.             new Dictionary<KeyCode, Action>(),
  15.             new Dictionary<KeyCode, Action>()){}
  16.  
  17.         public KeyboardController(Dictionary<KeyCode, Action> keyPressActions,
  18.             Dictionary<KeyCode, Action> keyReleaseActions,
  19.             Dictionary<KeyCode, Action> keyHeldActions)
  20.         {
  21.             keyPressBindings = new KeyBinding(keyPressActions, Input.GetKeyDown);
  22.             keyReleaseBindings = new KeyBinding(keyReleaseActions, Input.GetKeyUp);
  23.             keyHeldBindings = new KeyBinding(keyHeldActions, Input.GetKey);
  24.         }
  25.  
  26.         public void Update()
  27.         {
  28.             keyPressBindings.Iterate();
  29.             keyReleaseBindings.Iterate();
  30.             keyHeldBindings.Iterate();
  31.         }
  32.  
  33.         public void RebindKeyPressActions(Dictionary<KeyCode, Action> bindings)
  34.         {
  35.             keyPressBindings.Rebind(bindings);
  36.         }
  37.  
  38.         public void RebindKeyReleaseActions(Dictionary<KeyCode, Action> bindings)
  39.         {
  40.             keyReleaseBindings.Rebind(bindings);
  41.         }
  42.  
  43.         public void RebindKeyHeldActions(Dictionary<KeyCode, Action> bindings)
  44.         {
  45.             keyHeldBindings.Rebind(bindings);
  46.         }
  47.     }
  48. }
  49.  
Tags: C# Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement