Skip to content

Instantly share code, notes, and snippets.

@DleanJeans
Last active February 7, 2020 20:00
Show Gist options
  • Save DleanJeans/f09447f5208ffb5b43eb35a280ff77db to your computer and use it in GitHub Desktop.
Save DleanJeans/f09447f5208ffb5b43eb35a280ff77db to your computer and use it in GitHub Desktop.
A Godot node I used for a AirConsole game of mine with https://github.com/DleanJeans/godot-airconsole
extends Node2D
signal player_joined(device_id)
signal player_quit(device_id)
const DPAD = 'dpad'
const A_BUTTON = 'a'
const B_BUTTON = 'b'
const KEY = 'key'
const HAS_DIRECTION = 'has_direction'
const PRESSED = 'pressed'
var airconsole:AirConsole
# AirConsole used as a node in the scene tree
# if you use it as a singleton, replace airconsole with AirConsole
func _ready():
if not airconsole: # and remove this block
push_warning('Messenger: airconsole not set')
return
airconsole.connect('message_received', self, '_process_message')
airconsole.connect('device_connected', self, '_emit_player_joined')
airconsole.connect('device_disconnected', self, '_emit_player_quit')
airconsole.connect('device_connected', self, '_send_player_hue')
func _send_player_hue(device_id):
yield(get_tree(), 'idle_frame')
for animal in get_tree().get_nodes_in_group('Animals'):
if animal.device_id == device_id:
airconsole.message(device_id, { 'hue':animal.color.h * 360 }) #, 'light':animal.color.v })
func _emit_player_joined(device_id):
emit_signal('player_joined', device_id)
func _emit_player_quit(device_id):
emit_signal('player_quit', device_id)
func _process_message(device_id, message):
var animal_movement = _get_animal_movement(device_id)
if not animal_movement:
return
if message.has(A_BUTTON):
animal_movement.a()
elif message.has(B_BUTTON):
animal_movement.b()
elif message.has(DPAD):
var data = message[DPAD]
_process_dpad(animal_movement, data)
func _get_animal_movement(device_id):
var animal_movements = get_tree().get_nodes_in_group('AnimalMovements')
for control in animal_movements:
if control.device_id == device_id:
return control
func _process_dpad(animal_movement, data):
if data.has(KEY):
animal_movement.set_movement(data[KEY], data[PRESSED])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment