Class: LZRTag::Player::Hardware

Inherits:
Base
  • Object
show all
Defined in:
lib/lzrtag/player/hardware_player.rb

Overview

Hardware-handling player class. This class extends the base player, adding more hardware-related functionality and interfaces, such as:

  • Team setting

  • Brightness setting

  • Note playing

  • Gyro and button readout

  • Ping and Battery reading

etc.

Direct Known Subclasses

Effects

Instance Attribute Summary collapse

Attributes inherited from Base

#DeviceID, #handler, #hitIDTimetable, #id, #name, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connected?, #inspect

Constructor Details

#initialize(*data) ⇒ Hardware

Returns a new instance of Hardware.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lzrtag/player/hardware_player.rb', line 70

def initialize(*data)
	super(*data);

	@team = 0;
	@brightness = :idle;

	@dead = false;
	@deathChangeTime = Time.now();

	@ammo = 0;
	@maxAmmo = 0;
	@gunNo = 0;

	@gyroPose = :unknown;

	@position = {x: 0, y: 0}
	@zoneIDs  = Hash.new();

	@battery = 0; @ping = 0; @heap = 0;

	@BrightnessMap = self.class.getBrightnessKeys();

	# These values are configured for a DPS ~1, equal to all weapons
	# Including reload timings and other penalties
	@GunDamageMultipliers = [
		0.9138,
		1.85,
		0.6166,
	];
end

Instance Attribute Details

#ammoObject

Current ammo of the weapon. TODO one day this should be settable. Right now, it’s just reading



46
47
48
# File 'lib/lzrtag/player/hardware_player.rb', line 46

def ammo
  @ammo
end

#batteryObject (readonly)

Returns the value of attribute battery.



64
65
66
# File 'lib/lzrtag/player/hardware_player.rb', line 64

def battery
  @battery
end

#brightnessSymbol

Current brightness of the weapon. Possible brightnesses are:

  • :idle (low, slow brightness, white with slight team hue)

  • :teamSelect (team-colored with rainbow overlay)

  • :dead (low brightness, team colored with white overlay)

  • :active (bright, flickering and in team color)

A change will trigger the :playerBrightnessChanged event, with data [player, oldBrightness]

Returns:

  • (Symbol)

    Symbol describing the current brightness.



34
35
36
# File 'lib/lzrtag/player/hardware_player.rb', line 34

def brightness
  @brightness
end

#deadObject

Whether or not the player is currently dead. Set this to kill the player. Will trigger a :playerKilled or :playerRevived event, although kill_by is preferred to also specify which player killed.



39
40
41
# File 'lib/lzrtag/player/hardware_player.rb', line 39

def dead
  @dead
end

#deathChangeTimeObject (readonly)

Last time the death status changed (killed/revivied). Especially useful to determine when to revive a player



42
43
44
# File 'lib/lzrtag/player/hardware_player.rb', line 42

def deathChangeTime
  @deathChangeTime
end

#gunNoObject

Number of the current gun. The application can freely choose which gun profile the set is using, which influences shot speeds, sounds, reloading, etc.



52
53
54
# File 'lib/lzrtag/player/hardware_player.rb', line 52

def gunNo
  @gunNo
end

#gyroPoseObject (readonly)

Returns the gyro pose of the set. This is either:

  • :active

  • :laidDown

  • :pointsUp

  • :pointsDown

Changes are triggered by the set itself, if it has a gyro. The :poseChanged event is sent on change with [player, newPose] data



62
63
64
# File 'lib/lzrtag/player/hardware_player.rb', line 62

def gyroPose
  @gyroPose
end

#heapObject (readonly)

Returns the value of attribute heap.



64
65
66
# File 'lib/lzrtag/player/hardware_player.rb', line 64

def heap
  @heap
end

#maxAmmoObject (readonly)

Maximum ammo the weapon can have with the currently equipped gun.



48
49
50
# File 'lib/lzrtag/player/hardware_player.rb', line 48

def maxAmmo
  @maxAmmo
end

#pingObject (readonly)

Returns the value of attribute ping.



64
65
66
# File 'lib/lzrtag/player/hardware_player.rb', line 64

def ping
  @ping
end

#teamObject

The team (0..7) of this player. Setting it to a number will publish to /DeviceID/CFG/Team, changing the color of the weapon. Interpret it as binary string, with 1 being red, 2 green and 4 blue.

Changes trigger the :playerTeamChanged event, with [player, oldTeam] data



24
25
26
# File 'lib/lzrtag/player/hardware_player.rb', line 24

def team
  @team
end

Class Method Details

.getBrightnessKeysObject



66
67
68
# File 'lib/lzrtag/player/hardware_player.rb', line 66

def self.getBrightnessKeys()
	return [:idle, :teamSelect, :dead, :active]
end

Instance Method Details

#_set_dead(d, player = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/lzrtag/player/hardware_player.rb', line 185

def _set_dead(d, player = nil)
	dead = (d ? true : false);
	return if @dead == dead;
	@dead = dead;

	@deathChangeTime = Time.now();

	_pub_to "CFG/Dead", @dead ? "1" : "0", retain: true;
	@handler.send_event(@dead ? :playerKilled : :playerRevived, self, player);
end

#clear_all_topicsObject



242
243
244
245
246
247
248
# File 'lib/lzrtag/player/hardware_player.rb', line 242

def clear_all_topics()
	super();

	[	"CFG/Dead", "CFG/GunNo", "CFG/Brightness", "CFG/Team"].each do |t|
		_pub_to(t, "", retain: true);
	end
end

#gunDamage(number = nil) ⇒ Object

Return the averaged damage the player’s gun should do. This function is very useful to calculate the damage a player did per shot. The returned number tries to average damage to “1 DPS” for all weapons regardless of speed etc., which the application can multiply for a given total damage, creating a more balanced game.



236
237
238
239
240
# File 'lib/lzrtag/player/hardware_player.rb', line 236

def gunDamage(number = nil)
	number ||= @gunNo

	return @GunDamageMultipliers[number-1] || 1;
end

#kill_by(player) ⇒ Object



198
199
200
201
# File 'lib/lzrtag/player/hardware_player.rb', line 198

def kill_by(player)
	return if @dead;
	_set_dead(true, player);
end

#on_mqtt_data(data, topic) ⇒ Object

This function processes incoming MQTT data. The user must not call this, since it is handled by the LZRTag base handler



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/lzrtag/player/hardware_player.rb', line 105

def on_mqtt_data(data, topic)
	case topic[1..topic.length].join("/")
	when "HW/Ping"
		if(data.size == 3*4)
			parsedData = data.unpack("L<*");

			@battery = parsedData[0].to_f/1000;
			@ping 	= parsedData[2].to_f;
		end
	when "CFG/Dead"
		dead = (data == "1")
		return if @dead == dead;
		@dead = dead;

		@deathChangeTime = Time.now();

		@handler.send_event(@dead ? :playerKilled : :playerRevived, self);
	when "Stats/Ammo"
		return if(data.size != 8)

		outData = data.unpack("L<*");
		@ammo = outData[0];
		@maxAmmo = outData[1];
	when "Position"
		begin
			@position = JSON.parse(data, symbolize_names: true);
		rescue
		end
	when "HW/NSwitch"
		@handler.send_event(:navSwitchPressed, self, data.to_i)
	when "HW/Gyro"
		@gyroPose = data.to_sym
		@handler.send_event(:poseChanged, self, @gyroPose);
	when "ZoneUpdate"
		begin
			data = JSON.parse(data, symbolize_names: true);
		rescue JSON::ParserError
			return;
		end

		@zoneIDs = data[:data];
		if(data[:entered])
			@handler.send_event(:playerEnteredZone, self, data[:entered])
		end
		if(data[:exited])
			@handler.send_event(:playerExitedZone, self, data[:exited])
		end
	else
		super(data, topic);
	end
end

#revive_by(player) ⇒ Object



202
203
204
205
# File 'lib/lzrtag/player/hardware_player.rb', line 202

def revive_by(player)
	return unless @dead
	_set_dead(false, player)
end