1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/game.lua

86 lines
2.3 KiB
Lua
Raw Normal View History

2018-09-23 00:24:47 +02:00
local Global = require 'utils.global'
2018-12-29 19:39:20 +02:00
local Color = require 'resources.color_presets'
2019-06-01 13:41:12 +02:00
local print = print
2018-09-23 00:24:47 +02:00
local Game = {}
local bad_name_players = {}
Global.register(
bad_name_players,
function(tbl)
bad_name_players = tbl
end
)
2018-09-23 00:24:47 +02:00
2019-01-29 01:59:58 +02:00
--- Returns a valid LuaPlayer if given a number, string, or LuaPlayer. Returns nil otherwise.
-- obj <number|string|LuaPlayer>
function Game.get_player_from_any(obj)
local o_type = type(obj)
local p
if o_type == 'number' or o_type == 'string' then
p = game.get_player(obj)
if p and p.valid then
return p
end
elseif o_type == 'table' and obj.valid and obj.is_player() then
return obj
2019-01-29 01:59:58 +02:00
end
end
--- Prints to player or console.
-- @param msg <string|table> table if locale is used
-- @param color <table> defaults to white
function Game.player_print(msg, color)
2019-02-13 00:04:50 +02:00
color = color or Color.white
2019-06-01 13:41:12 +02:00
local player = game.player
if player then
player.print(msg, color)
2018-11-06 13:55:52 +02:00
else
print(msg)
2018-11-06 13:55:52 +02:00
end
2018-11-10 01:54:18 +02:00
end
--[[
@param Position String to display at
@param text <string|table> table if locale is used
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
@param surface LuaSurface
@return the created entity
]]
function Game.print_floating_text(surface, position, text, color)
2018-12-29 19:39:20 +02:00
color = color or Color.white
return surface.create_entity {
name = 'tutorial-flying-text',
color = color,
text = text,
position = position
}
end
--[[
Creates a floating text entity at the player location with the specified color in {r, g, b} format.
Example: "+10 iron" or "-10 coins"
@param text String to display
@param color table in {r = 0~1, g = 0~1, b = 0~1}, defaults to white.
@return the created entity
]]
function Game.print_player_floating_text_position(player_index, text, color, x_offset, y_offset)
local player = game.get_player(player_index)
if not player or not player.valid then
return
end
local position = player.position
return Game.print_floating_text(player.surface, {x = position.x + x_offset, y = position.y + y_offset}, text, color)
end
function Game.print_player_floating_text(player_index, text, color)
Game.print_player_floating_text_position(player_index, text, color, 0, -1.5)
2018-11-06 13:55:52 +02:00
end
2018-09-23 00:24:47 +02:00
return Game