summaryrefslogtreecommitdiff
path: root/gamemode/itemsystem/common_plantable.lua
blob: 4570dc88f3923c7b979c7bf12173df8c6098c506 (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
--[[
Provides:
  genericMakePlantable(table_item)
    You can call genericMakePlantable(table_item) on items to make them have a plant option. Before genericMakePlantable get called, be sure the item has an .GrowTime =number and a .OnGrow = function(player_player)
  GAMEMODE.MakeGenericPlantChild(player_ply, vector_pos, string_model, entity_parent)
    Creates a child on the plant(like oranges or melons on their bush), with player as the owner, at the given position(relative to the plant), with the given model, the parrent is the entity that will be removed once all the children are removed. If ply is nil, then the plant will be owned by world.
  GAMEMODE.MakeGenericPlant(player_ply, vector_pos, string_model, boolean_isWorld )
    Creates a plant with the specified string, at the position, with ply as the ower. If isWorld is true, the owner is the world instead (like trees)
]]

if (SERVER) then
  util.AddNetworkString( "gms_plantseed" )
end
local function plant(player, resourcename)
  if (CLIENT) then
    net.Start("gms_plantseed")
    net.WriteString(resourcename)
    net.SendToServer()
  end
  if (SERVER) then
    local tbl = GMS.Resources[resourcename]
    assert(tbl.GrowTime != nil,tbl.Name .. " .GrowTime is nil!")
    assert(isfunction(tbl.OnGrow),tbl.Name .. " .OnGrow is not a table")

    if (player:GetNWInt("plants") > GetConVar("gms_PlantLimit"):GetInt() - 1) then
      player:SendMessage( "You man only have " .. GetConVar("gms_PlantLimit"):GetInt() .. " plants at a time.", 3, Color( 10, 200, 10, 255 ) )
      return
    end

    local tr = player:GetEyeTrace()
    if (player:GetPos():Distance(tr.HitPos) > 180 ) then
      player:SendMessage( "Too far away to plant", 3, Color( 10, 200, 10, 255 ) )
      return
    end

    //Everything is good, make the plant
    player:SetNWInt( "plants", player:GetNWInt( "plants" ) + 1 )
    local pent = ents.Create("gms_generic_plantable")
    pent:SetPos(tr.HitPos)
    pent.GrowTime = tbl.GrowTime
    pent.OnGrow = tbl.OnGrow
    pent:SetOwner(player)
    pent:Spawn()
    player:DecResource( resourcename, 1 )
  end
end

net.Receive( "gms_plantseed", function(len,pl)
  local resourcename = net.ReadString()
  plant(pl,resourcename)
end)

function genericMakePlantable( tbl )
  local plantthis = function(player)
    plant(player,tbl.Name)
  end
  if (tbl.Actions == nil) then
    tbl.Actions = {}
  end
  tbl.Actions["Plant " .. tbl.Name] = plantthis
end

GAMEMODE = GAMEMODE or {}
function GAMEMODE.MakeGenericPlantChild( ply, pos, mdl, parent )
  local ent = ents.Create( "prop_physics" )
  ent:SetAngles( Angle( 0, math.random( 0, 360 ) , 0 ) )
  ent:SetModel( mdl )
  ent:SetPos( pos )
  ent:Spawn()
  ent.IsPlantChild = true

  ent:SetHealth( 99999 )
  ent:Fadein()

  local phys = ent:GetPhysicsObject()
  if ( phys ) then phys:EnableMotion( false ) end

  ent.PlantParent = parent
  ent.PlantParentName = parent:GetName()
  parent.Children = parent.Children + 1

  if ( IsValid( ply ) ) then
  	SPropProtection.PlayerMakePropOwner( ply, ent )
  else
  	ent:SetNWString( "Owner", "World" )
  end

  ent.PhysgunDisabled = true

  return ent
end

function GAMEMODE.MakeGenericPlant( ply, pos, mdl, isWorld )
  local ent = ents.Create( "prop_dynamic" )
  ent:SetAngles( Angle( 0, math.random( 0, 360 ), 0 ) )
  ent:SetSolid( SOLID_VPHYSICS )
  ent:SetModel( mdl )
  ent:SetPos( pos )
  ent:Spawn()
  ent.IsPlant = true
  ent:SetName( "gms_plant" .. ent:EntIndex() )

  ent:Fadein()
  ent:RiseFromGround( 1, 50 )

  if ( !isWorld && IsValid( ply ) ) then
    ent:SetNWEntity( "plantowner", ply )
    SPropProtection.PlayerMakePropOwner( ply, ent )
  else
    ent:SetNWString( "Owner", "World" )
  end

  local phys = ent:GetPhysicsObject()
  if ( IsValid( phys ) ) then phys:EnableMotion( false ) end
  ent.PhysgunDisabled = true

  return ent
end