×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • Function Library
« ‹ 3 / 3
Function Library
Onkei
« Citoyen »
1487696340000
    • Onkei#0000
    • Profil
    • Derniers messages
    • Tribu
#41
  0
just converted some common functions I use from p5.js for use here
constrain() probably has been done a million times but I think map() will be extremely useful for anyone coding

Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function constrain(value, min, max)
--[[
value - Incoming value to be converted
min - Minimum limit
max - Maxmimum limit
]]

local value2 = value
if value2 > max then value2 = max elseif value2 < min then value2 = min end

return value2
end

function map(value, start1, stop1, start2, stop2)
--[[
value - Incoming value to be converted
start1 - Lower bound of the value's current range
stop1 - Upper bound of the value's current range
start2 - Lower bound of the value's target range
stop2 - Upper bound of the value's target range
]]

local value2 = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
return value2
end


Sample use of map()

Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
local currentMap = {width = 800, height = 400}
local textAreaDisplay = {width = 200, height = 100}

function eventLoop()
for n in pairs(tfm.get.room.playerList) do
local data = tfm.get.room.playerList[n]
-- This uses the map function to create a simple textArea minimap of the player's position.
local textAreaCoords = {
x = (800 - textAreaDisplay.width) - 20,
y = 4
}
ui.addTextArea(0, "", n, textAreaCoords.x, textAreaCoords.y, textAreaDisplay.width, textAreaDisplay.height, 0x324650, 0x000000, 0.5, true)

local playerMapCoords = {
x = constrain(map(data.x, 0, currentMap.width, textAreaCoords.x, textAreaCoords.x + textAreaDisplay.width), textAreaCoords.x, textAreaCoords.x + textAreaDisplay.width),
y = constrain(map(data.y, 0, currentMap.height, textAreaCoords.y, textAreaCoords.y + textAreaDisplay.height), textAreaCoords.y, textAreaCoords.y + textAreaDisplay.height),
}
ui.addTextArea(1, "<p align='center'><font face='Wingdings' size='15'><rose>l", n, playerMapCoords.x, playerMapCoords.y, 30, 20, 0x324650, 0x000000, 0, true)
end
end


e - had a sneaky mistake with the constrain function, hopefully nobody saw that

Dernière modification le 1488656880000
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • Function Library
« ‹ 3 / 3
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27