aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/itemsystem/weapons/rustyaxe.lua
blob: f21500de67061ea591511312d51371a6b2c20c41 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
--[[
    An example item
]]
local item = {}

--Required, a name, all item names must be unique
item.Name = "Rusty Axe"

--Optional, a tooltip to display
item.Tooltip = "An old axe, probably good for fighting."

--Required Returns the data needed to rebuild this item, should only contain the minimum data nessessary since this gets sent over the network
item.Serialize = function(self)
    print("Trying to serailize!")
    return ""
end

--Required, Rebuilds the item from data created in Serialize, if the item is different from the "main" copy of the item, it should retun a tabl.Copy(self), with the appropriate fields set.
item.DeSerialize = function(self,string)
    print("Trying to deserialize!")
    return self
end

--Optional, when the player clicks this item, a menu will show up, if the menu item is clicked, the function is ran. This is all run client side, so if you want it to do something, you'll need to use the net library.
function item.GetOptions(self)
    local options = {}
    options["test"] = function() print("You pressed test!") end
    options["toste"] = function() print("You pressed toste!") end
    return options
end

function item.DoOnPanel(dimagebutton)
    dimagebutton:SetImage( "weapons/rustyaxe/rustyaxe.png")
end

function item.DoOnEqupPanel(dimagebutton)
    print("called with panel:",panel)
    dimagebutton:SetImage( "weapons/rustyaxe/rustyaxe_eq.png")
end

--Required, the shape of this item.
item.Shape = {
    {true,true},
    {true},
    {true},
}

--Optional, If this item can be equiped in any player slots, put them here.
item.Equipable = "Right"

--[[ returns the direction the player swung]]
local function playermovedir(player)
    local vel = player:GetVelocity():GetNormalized()
    vel.z = 0
    local swings = {
        {player:GetForward(),"forward"},
        {-player:GetForward(),"backward"},
        {player:GetRight(),"right"},
        {-player:GetRight(),"left"}
    }
    table.sort(swings,function(a,b)
        return vel:Dot(a[1]) > vel:Dot(b[1])
    end)
    return swings[1][2]
end

local positionset = {}

--[[
local function swingarc(player,times,positions,onhit)
    local positionpoints = {}
    table.insert(positionset,positionpoints)
    local hitents = {}
    for k,v in ipairs(times) do
        timer.Simple(v,function()
            local weaponpos = positions[k] + player:GetPos()
            table.insert(positionpoints,weaponpos)
            if #positionpoints > 1 then
                local tr = util.TraceLine({
                    start = positionpoints[#positionpoints-1],
                    endpos = positionpoints[#positionpoints],
                })
                if tr.Hit then
                    onhit(tr)
                end
            end
        end)
    end
    timer.Simple(times[#times],function()
        print("Inserted swing, drawn positions are now:")
        PrintTable(positionset)
    end)
end

hook.Add( "HUDPaint", "weaponswings", function()
	cam.Start3D() -- Start the 3D function so we can draw onto the screen.
        for k,v in pairs(positionset) do
            for i = 1,#v-1 do
                render.DrawLine( v[i], v[i+1], Color(255,0,0,255), false )
            end
        end
		--render.SetMaterial( material ) -- Tell render what material we want, in this case the flash from the gravgun
		--render.DrawSprite( pos, 16, 16, white ) -- Draw the sprite in the middle of the map, at 16x16 in it's original colour with full alpha.
	cam.End3D()
end )
]]

--Optional, what to do when the player clicks, and this item is in the slot in inventory. only works for items equipable in left and right
item.lastSwing = {}
item.onClick = function(self,owner)
    item.lastSwing[owner] = item.lastSwing[owner] or 0
    if item.lastSwing[owner] > CurTime() then
        print("returning because item.lastSwing is " .. item.lastSwing[owner], "but Curtime is",CurTime())
         return end
    item.lastSwing[owner] = CurTime()+1.33
    local dir = playermovedir(owner)
    local fow,rig,up = owner:GetForward(),owner:GetRight(),owner:GetUp()
    local movementtbl = {
        ["forward"] = function()
            owner:SetLuaAnimation("axe_swing_up")
            timer.Simple(2.33,function() owner:StopLuaAnimation("axe_swing_up") end)
            local hits = ART.swingarc(owner,{
                1,1.1,1.2,1.3
            },{
                fow*20 + up*26,
                fow*45 + up*6,
                fow*35 + up*-13,
                fow*20 + up*-34,
            },function(tr)
                if tr.Entity.TakeDamage ~= nil and tr.Entity ~= owner then
                    tr.Entity:TakeDamage(5, owner, owner:GetActiveWeapon())
                end
                print("Hit",tr.Entity)
            end)
        end,
        ["backward"] = function()

        end,
        ["left"] = function()
            owner:SetLuaAnimation("axe_swing_left")
            timer.Simple(2.33,function() owner:StopLuaAnimation("axe_swing_left") end)
            local hits = ART.swingarc(owner,{
                1,1.1,1.2,1.3
            },{
                rig*30 + up*-5,
                rig*10 + fow*30 + up*-9,
                rig*-10 + fow*30 + up*-10,
                rig*-30 + up*-15,
            },function(tr)
                if tr.Entity.TakeDamage ~= nil and tr.Entity ~= owner then
                    tr.Entity:TakeDamage(5, owner, owner:GetActiveWeapon())
                end
                print("Hit",tr.Entity)
            end)
        end,
        ["right"] = function()
            owner:SetLuaAnimation("axe_swing_right")
            timer.Simple(2.33,function() owner:StopLuaAnimation("axe_swing_right") end)
            local hits = ART.swingarc(owner,{
                1,1.1,1.2,1.3
            },{
                rig*-30 + up*-5,
                rig*-10 + fow*30 + up*-9,
                rig*10 + fow*30 + up*-10,
                rig*30 + up*-15,
            },function(tr)
                if tr.Entity.TakeDamage ~= nil and tr.Entity ~= owner then
                    tr.Entity:TakeDamage(5, owner, owner:GetActiveWeapon())
                end
                print("Hit",tr.Entity)
            end)
        end,
    }
    movementtbl[playermovedir(owner)]()
end

--Optional, if we should do something special on equip(like draw the PAC for this weapon)
item.onEquip = function(self,who)
    print("onEquip",who)
    if CLIENT then print("onEquip client!") end
    if SERVER then
        PrintTable(pac)
        who:GetActiveWeapon():SetHoldType("melee")
        ART.ApplyPAC(who,"rustyaxe")
        --local outfit = pac.luadata.ReadFile("pac3/mech.txt")
        --who:AttachPACPart(outfit)
        --print("onEquip server!")
    end
end

--Optional, if we should do something speical on unequip(like setting animations back to normal)
item.onUnEquip = function(self,who)
    who:GetActiveWeapon():SetHoldType("normal")
    ART.RemovePAC(who,"rustyaxe")
end

item.onDropped = function(self, ent)
    ART.ApplyPAC(ent,"rustyaxe")
end

print("Hello from scrapgun.lua")
--Don't forget to register the item!
ART.RegisterItem(item)