×

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
  • /
  • Arrow shooting example
1 / 2 › »
Arrow shooting example
Makinit
« Citoyen »
1383052380000
    • Makinit#0095
    • Profil
    • Derniers messages
    • Tribu
#1
  1
An example which uses mouse clicks, objects, physics, timing, text areas and a queue. I added some comments where needed. Feel free to comment and ask questions.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
93
94
95
96
97
98
99
100
101
102
103
104
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
local settings = {
map = "0",
ammo = 6,
force = 50,
recoil = 10,
maxObjects = 30,
ammoTicks = 2,
}

local players = {}
local objects = {}

function main()
objects = queue.new()
tfm.exec.disableAutoScore(false)
tfm.exec.disableAutoShaman(true)
tfm.exec.disableAutoNewGame(true)
tfm.exec.newGame(settings.map)
end

function eventNewGame()
tfm.exec.setGameTime(0, true)
players = {}
for name in pairs(tfm.get.room.playerList) do
initPlayer(name)
end
end

function initPlayer(name)
players[name] = {ammo = 0}
ui.addTextArea(0, "", name, 10, 30, settings.ammo * 15, 20, 0x010101, 0x000000, 0.5)
system.bindMouse(name, true)
end

function eventMouse(name, x, y)
local player = players[name]
if player and player.ammo > 0 then
-- remove one ammo
ui.removeTextArea(player.ammo * 2 - 1, name)
ui.removeTextArea(player.ammo * 2, name)
player.ammo = player.ammo - 1

local roomPlayer = tfm.get.room.playerList[name]

-- calculate angle between player and click
local dx = x - roomPlayer.x
local dy = y - roomPlayer.y
local angle = math.atan2(dy, dx)

-- calculate speeds to direct arrow and always have the same total speed
local vx = math.cos(angle)
local vy = math.sin(angle)

-- spawn arrow and add to queue
queue.insert(objects, tfm.exec.addShamanObject(35, roomPlayer.x + 20 * vx, roomPlayer.y + 20 * vy, angle*180/math.pi, settings.force * vx, settings.force * vy, false))

local recoil = -vx * settings.recoil
-- workaround to avoid argument exception bug
if recoil <= -1 or recoil >= 1 then
tfm.exec.movePlayer(name, 0, 0, true, recoil, 0, true)
end

-- remove first arrow when there are too many
if objects.size > settings.maxObjects then
tfm.exec.removeObject(queue.remove(objects))
end
end
end

local loopCount = 0
function eventLoop()
-- loopCount resets after a certain amount
if loopCount == 0 then
ammo()
end
loopCount = (loopCount + 1) % settings.ammoTicks
end

function ammo()
for name, player in pairs(players) do
local ammo = player.ammo
if ammo < settings.ammo then
-- add one ammo
player.ammo = ammo + 1
ui.addTextArea(ammo * 2 + 1, "", name, 14 + ammo * 15, 39, 3, 3, 0x990000, 0x990000, 1)
ui.addTextArea(ammo * 2 + 2, "", name, 15 + ammo * 15, 40, 1, 1, 0xff0000, 0xcc0000, 1)
end
end
end

function eventNewPlayer(name)
initPlayer(name)
tfm.exec.respawnPlayer(name)
end

function eventPlayerDied(name)
tfm.exec.respawnPlayer(name)
end

function eventPlayerWon(name)
tfm.exec.respawnPlayer(name)
end

-- simple queue for performance, much faster than system table queues, can contain nils
queue = {}
function queue.new()
return {
tail = nil,
head = nil,
size = 0
}
end
function queue.insert(self, v)
local i = {
value = v,
next = nil
}
if self.tail and self.head then
self.tail.next = i
else
self.head = i
end
self.tail = i
self.size = self.size + 1
end
function queue.peek(self)
if self.head then
return self.head.value
else
error("queue is empty")
end
end
function queue.remove(self)
local r = queue.peek(self)
self.head = self.head.next
if not self.head then
tail = nil
end
self.size = self.size - 1
return r
end

main()

Dernière modification le 1481368320000
Hophipmice
« Citoyen »
1383052500000
    • Hophipmice#0000
    • Profil
    • Derniers messages
    • Tribu
#2
  0
[•] Init Error : [string "transformice.lua"]:37: 'then' expected
if player and player.ammo &gt; 0 then
broken because edit :)
Issey
« Citoyen »
1383065040000
    • Issey#0000
    • Profil
    • Derniers messages
#3
  0
dat maths
Lukkfly
« Citoyen »
1383065160000
    • Lukkfly#0000
    • Profil
    • Derniers messages
    • Tribu
#4
  0
Cool i LIKE This
Xanmeow
« Citoyen »
1383087960000
    • Xanmeow#0000
    • Profil
    • Derniers messages
    • Tribu
#5
  0
so... how do you change what you shoot?
Mikuhl
« Citoyen »
1383093600000
    • Mikuhl#3311
    • Profil
    • Derniers messages
    • Tribu
#6
  0
The maths hurt my brain!

Im getting some random [•] Argument error. also.
Makinit
« Citoyen »
1383126240000
    • Makinit#0095
    • Profil
    • Derniers messages
    • Tribu
#7
  0
Xanmeow a dit :
so... how do you change what you shoot?

Change the object type in tfm.exec.addShamanObject.
Jaackster a dit :
Im getting some random [•] Argument error. also.

That happens when both speed arguments in tfm.exec.movePlayer are between -1 and 1, it's a bug in the API.
Xanmeow
« Citoyen »
1383178320000
    • Xanmeow#0000
    • Profil
    • Derniers messages
    • Tribu
#8
  0
I found out how to break it by changing a number in it from nil to 10

[•] Runtime Error : transformice.lua:115: index expected, got number
Makinit
« Citoyen »
1383215460000
    • Makinit#0095
    • Profil
    • Derniers messages
    • Tribu
#9
  0
Xanmeow a dit :
I found out how to break it by changing a number in it from nil to 10

[•] Runtime Error : transformice.lua:115: index expected, got number

That's because you set an internal queue element to a number while they are supposed to be either a table or nil.
Kugineko
« Citoyen »
1383220680000
    • Kugineko#6324
    • Profil
    • Derniers messages
#10
  0
Makinit a dit :
loopCount = (loopCount + 1) % settings.ammoTicks

Neat way to make a counter for eventLoop 0:

That queue object is so useful, from what I've seen tables tend to be too slow when trying to store objects and remove then quickly, many of my experiments wouldn't be possible without custom data structures like this queue of yours.

Thanks for sharing :)
Makinit
« Citoyen »
1383231960000
    • Makinit#0095
    • Profil
    • Derniers messages
    • Tribu
#11
  0
Kugineko a dit :
Thanks for sharing :)

You're welcome.

Makinit a dit :
That happens when both speed arguments in tfm.exec.movePlayer are between -1 and 1, it's a bug in the API.

Updated the script to avoid this bug:
a dit :
local recoil = -vx * settings.recoil
-- workaround to avoid argument exception bug
if recoil <= -1 or recoil >= 1 then
tfm.exec.movePlayer(name, 0, 0, true, recoil, 0, true)
end
Noobzaii
« Citoyen »
1383237480000
    • Noobzaii#0000
    • Profil
    • Derniers messages
    • Tribu
#12
  0
I wonder if I can make ammos reload faster? :D
Makinit
« Citoyen »
1383239160000
    • Makinit#0095
    • Profil
    • Derniers messages
    • Tribu
#13
  0
Noobzaii a dit :
I wonder if I can make ammos reload faster? :D

You could change ammoTicks in settings to 1, which is the lowest possible value and will cause reloading to take half a second. You could also call ammo() in eventLoop more than once or remove ammo limitation altogether.
Empty
« Citoyen »
1383454680000
    • Empty#8994
    • Profil
    • Derniers messages
    • Tribu
#14
  0
I have created script like this , equal functions codes etc my script dont remove arrows

look

Code a dit :


local settings = {
mapa = "0",
flechas = 6,
forcadaflecha = 50,
recuodotiro = 10,
maximodeobjetos = 30,
flechasTicks = 2,
}

local players = {}
local objects = {}

function principal()
objects = listadeespera.new()
tfm.exec.disableAutoScore(false)
tfm.exec.disableAutoShaman(true)
tfm.exec.disableAutoNewGame(true)
tfm.exec.newGame(settings.mapa)
end

function eventNewGame()
tfm.exec.setGameTime(0, true)
players = {}
for name in pairs(tfm.get.room.playerList) do
initPlayer(name)
end
end

function initPlayer(name)
players[name] = {flechas = 0}
system.bindMouse(name, true)
end

function eventMouse(name, x, y)
local player = players[name]
if player and player.flechas > 0 then
player.flechas = player.flechas - 1

local roomPlayer = tfm.get.room.playerList[name]

local dx = x - roomPlayer.x
local dy = y - roomPlayer.y
local angle = math.atan2(dy, dx)

local vx = math.cos(angle)
local vy = math.sin(angle)

listadeespera.insert(objects, tfm.exec.addShamanObject(35, roomPlayer.x + 20 * vx, roomPlayer.y + 20 * vy, angle*180/math.pi, settings.forcadaflecha * vx, settings.forcadaflecha * vy, false))

local recuodotiro = -vx * settings.recuodotiro
if recuodotiro <= -1 or recuodotiro >= 1 then
tfm.exec.movePlayer(name, 0, 0, true, recuodotiro, 0, true)
end

if objects.size > settings.maximodeobjetos then
tfm.exec.removeObject(listadeespera.remove(objects))
end
end
end

local loopCount = 0
function eventLoop()
if loopCount == 0 then
flechas()
end
loopCount = (loopCount + 1) % settings.flechasTicks
end

function flechas()
for name, player in pairs(players) do
local flechas = player.flechas
if flechas < settings.flechas then
player.flechas = flechas + 1
end
end
end

function eventNewPlayer(name)
initPlayer(name)
tfm.exec.respawnPlayer(name)
end

function eventPlayerDied(name)
tfm.exec.respawnPlayer(name)
end

function eventPlayerWon(name)
tfm.exec.respawnPlayer(name)
end

listadeespera = {}
function listadeespera.new()
return {
tail = nil,
head = nil,
size = 0
}
end
function listadeespera.insert(self, v)
local i = {
value = v,
next = nil
}
if self.tail and self.head then
self.tail.next = i
else
self.head = i
end
self.tail = i
self.size = self.size + 1

end
principal()
Walid
« Citoyen »
1383473640000
    • Walid#8154
    • Profil
    • Derniers messages
#15
  0
Makinit a dit :
An example which uses mouse clicks, objects, physics, timing, text areas and a queue. I added some comments where needed. Feel free to comment and ask questions.


local settings = {
map = "0",
ammo = 6,
force = 50,
recoil = 10,
maxObjects = 30,
ammoTicks = 2,
}

local players = {}
local objects = {}

function main()
objects = queue.new()
tfm.exec.disableAutoScore(false)
tfm.exec.disableAutoShaman(true)
tfm.exec.disableAutoNewGame(true)
tfm.exec.newGame(settings.map)
end

function eventNewGame()
tfm.exec.setGameTime(0, true)
players = {}
for name in pairs(tfm.get.room.playerList) do
initPlayer(name)
end
end

function initPlayer(name)
players[name] = {ammo = 0}
ui.addTextArea(0, "", name, 10, 30, settings.ammo * 15, 20, 0x010101, 0x000000, 0.5)
system.bindMouse(name, true)
end

function eventMouse(name, x, y)
local player = players[name]
if player and player.ammo &gt; 0 then
-- remove one ammo
ui.removeTextArea(player.ammo * 2 - 1, name)
ui.removeTextArea(player.ammo * 2, name)
player.ammo = player.ammo - 1

local roomPlayer = tfm.get.room.playerList[name]

-- calculate angle between player and click
local dx = x - roomPlayer.x
local dy = y - roomPlayer.y
local angle = math.atan2(dy, dx)

-- calculate speeds to direct arrow and always have the same total speed
local vx = math.cos(angle)
local vy = math.sin(angle)

-- spawn arrow and add to queue
queue.insert(objects, tfm.exec.addShamanObject(35, roomPlayer.x + 20 * vx, roomPlayer.y + 20 * vy, angle*180/math.pi, settings.force * vx, settings.force * vy, false))

local recoil = -vx * settings.recoil
-- workaround to avoid argument exception bug
if recoil &lt;= -1 or recoil &gt;= 1 then
tfm.exec.movePlayer(name, 0, 0, true, recoil, 0, true)
end

-- remove first arrow when there are too many
if objects.size &gt; settings.maxObjects then
tfm.exec.removeObject(queue.remove(objects))
end
end
end

local loopCount = 0
function eventLoop()
-- loopCount resets after a certain amount
if loopCount == 0 then
ammo()
end
loopCount = (loopCount + 1) % settings.ammoTicks
end

function ammo()
for name, player in pairs(players) do
local ammo = player.ammo
if ammo &lt; settings.ammo then
-- add one ammo
player.ammo = ammo + 1
ui.addTextArea(ammo * 2 + 1, "", name, 14 + ammo * 15, 39, 3, 3, 0x990000, 0x990000, 1)
ui.addTextArea(ammo * 2 + 2, "", name, 15 + ammo * 15, 40, 1, 1, 0xff0000, 0xcc0000, 1)
end
end
end

function eventNewPlayer(name)
initPlayer(name)
tfm.exec.respawnPlayer(name)
end

function eventPlayerDied(name)
tfm.exec.respawnPlayer(name)
end

function eventPlayerWon(name)
tfm.exec.respawnPlayer(name)
end

-- simple queue for performance, much faster than system table queues, can contain nils
queue = {}
function queue.new()
return {
tail = nil,
head = nil,
size = 0
}
end
function queue.insert(self, v)
local i = {
value = v,
next = nil
}
if self.tail and self.head then
self.tail.next = i
else
self.head = i
end
self.tail = i
self.size = self.size + 1
end
function queue.peek(self)
if self.head then
return self.head.value
else
error("queue is empty")
end
end
function queue.remove(self)
local r = queue.peek(self)
self.head = self.head.next
if not self.head then
tail = nil
end
self.size = self.size - 1
return r
end

main()

Pastebein pls '-'
Safwanrockz
« Censeur »
1383476940000
    • Safwanrockz#0095
    • Profil
    • Derniers messages
    • Tribu
#16
  0
Chadgraygui a dit :
I have created script like this , equal functions codes etc my script dont remove arrows

look

Weird how it looks just like Mak's script with some change of variable names.

Walidpokemon a dit :
Pastebein pls '-'

http://pastebin.com/GupGVyHg
Empty
« Citoyen »
1383484380000
    • Empty#8994
    • Profil
    • Derniers messages
    • Tribu
#17
  0
Yeah scripts do not change much code

Makinit give-me your permission for you queue remove?

Y have catch the part of infinite time

this

Code i catch a dit :
function principal()
objects = listadeespera.new()
tfm.exec.disableAutoScore(false)
tfm.exec.disableAutoShaman(true)
tfm.exec.disableAutoNewGame(true)
tfm.exec.newGame(settings.mapa)
end
Fluffyshine
« Citoyen »
1383484500000
    • Fluffyshine#0000
    • Profil
    • Derniers messages
    • Tribu
#18
  0
wow.. I like this a lot, good job!

Chadgraygui a dit :
I have created script like this , equal functions codes etc my script dont remove arrows

look

It’s exactly the same script..
Empty
« Citoyen »
1383484620000
    • Empty#8994
    • Profil
    • Derniers messages
    • Tribu
#19
  0
I catch some parts i solicited permission

queue


my is a simple

create arrows and not remove

Iinsert credits fluffy
Fluffyshine
« Citoyen »
1383485520000
    • Fluffyshine#0000
    • Profil
    • Derniers messages
    • Tribu
#20
  0
Chadgraygui a dit :
I catch some parts i solicited permission

queue


my is a simple

create arrows and not remove

Iinsert credits fluffy

It’s the same script. As Safwan said, you only edited some variables.
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • Arrow shooting example
1 / 2 › »
© Atelier801 2018

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

Version 1.27