Class: LZRTag::Player::Life

Inherits:
Effects show all
Defined in:
lib/lzrtag/player/life_player.rb

Instance Attribute Summary collapse

Attributes inherited from Effects

#heartbeat, #marked

Attributes inherited from Hardware

#ammo, #battery, #brightness, #dead, #deathChangeTime, #gunNo, #gyroPose, #heap, #maxAmmo, #ping, #team

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Effects

#hit, #noise, #sound, #vibrate

Methods inherited from Hardware

#_set_dead, getBrightnessKeys, #gunDamage, #kill_by, #on_mqtt_data, #revive_by

Methods inherited from Base

#connected?, #inspect, #on_mqtt_data

Constructor Details

#initialize(*data) ⇒ Life

Returns a new instance of Life.



24
25
26
27
28
29
30
31
32
33
# File 'lib/lzrtag/player/life_player.rb', line 24

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

	@life    = 0;
	@maxLife = 100;

	@lastDamageTime = Time.at(0);

	regenerate(@maxLife);
end

Instance Attribute Details

#lastDamageTimeObject (readonly)

Returns the last time the player was damaged. This can be useful to apply buffs and other data, as well as “post-damage regenration delay”



22
23
24
# File 'lib/lzrtag/player/life_player.rb', line 22

def lastDamageTime
  @lastDamageTime
end

#lifeObject (readonly)

Return the amount of life a player currently has. This value can not be set directly, but should instead be modified via #regenerate and #damage_by. It is a number from 0 to maxLife (default 100 Any change in a player’s life is send to MQTT, as well as signalled via :playerRegenerated and :playerHurt events



14
15
16
# File 'lib/lzrtag/player/life_player.rb', line 14

def life
  @life
end

#maxLifeObject

Set the maximum amount of life a player has. Can be useful to introduce tank classes with more life.



17
18
19
# File 'lib/lzrtag/player/life_player.rb', line 17

def maxLife
  @maxLife
end

Instance Method Details

#clear_all_topicsObject



109
110
111
112
113
# File 'lib/lzrtag/player/life_player.rb', line 109

def clear_all_topics()
	super();
	_pub_to("Stats/HP", "", retain: true);
	_pub_to("CFG/MaxLife", "", retain: true);
end

#damage_by(amount, sourcePlayer = nil) ⇒ Object

Damage a player by a given amount with given source. This function will damage a player by “amount”, but will not decrease a player’s life below zero. Instead, it will kill the player. This function will send out :playerHurt [player, sourcePlayer, deltaLife] and potentially also :playerKilled

Parameters:

  • amount (Numeric)

    Amount to damage the player by

  • sourcePlayer (nil, Player::Base) (defaults to: nil)

    The player this damage originated from - optional



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lzrtag/player/life_player.rb', line 68

def damage_by(amount, sourcePlayer = nil)
	unless(amount.is_a? Numeric)
		raise ArgumentError, "Amount needs to be a number!";
	end
	unless (sourcePlayer.is_a? Base) or sourcePlayer.nil?
		raise ArgumentError, "sourcePlayer needs to be a Player::Base!"
	end

	oLife = @life;
	nLife = @life - amount;
	nLife = [0, nLife].max;

	@lastDamageTime = Time.now();

	if(nLife != @life)
		@life = nLife;

		@handler.send_event :playerHurt, self, sourcePlayer, oLife - @life;
		_pub_to("Stats/HP", @life.to_s, retain: true);

		kill_by(sourcePlayer) if(nLife <= 0);
	end

	return oLife - nLife;
end

#regenerate(amount, source = nil) ⇒ Object

Regenerate the player by a given amount. This function will update the player’s life, increasing it by amount, and will send :playerRegenerated [player, deltaLife] or :playerFullyRegenerated [player] if a player’s life was increased to the maximum value.

Parameters:

  • amount (Numeric)

    Amount of life to add



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lzrtag/player/life_player.rb', line 41

def regenerate(amount, source = nil)
	unless((amount.is_a? Numeric) && amount >= 0)
		raise ArgumentError, "Amount needs to be a positive number!"
	end

	nLife = @life + amount;
	nLife = @maxLife if(nLife > @maxLife)
	return if nLife == @life;

	oLife = @life;
	@life = nLife;

	@handler.send_event(:playerRegenerated, self, @life - oLife, source);
	if(@life == maxLife)
		@handler.send_event(:playerFullyRegenerated, self, source);
	end

	_pub_to("Stats/HP", @life.to_s, retain: true);
end