-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.lua
153 lines (106 loc) · 4.03 KB
/
item.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
144
145
146
147
148
149
150
151
152
153
-- Sprint 1
-- Story "Item Class"
-- and "Basic Character Action: Lift Stuff"
-- and "Game: Damage/Health"
-- Created by Chux
function itemLoad()
graphic_itemSofa = love.graphics.newImage("images/itemSofa.png", love.image_optimize);
graphic_itemCrate4848 = love.graphics.newImage("images/items/crate4848.png", love.image_optimize);
graphic_itemBasketball = love.graphics.newImage("images/itemBasketball.png", love.image_optimize);
graphic_itemAnvil = love.graphics.newImage("images/items/anvil.png", love.image_optimize);
itemGraphic = {}
-- Tweakable values
constItemDmgAbsorb = 15; -- If damage dealt doesnt reach this minimum - no hitpoints are lost
end
-- x and y sets position,
-- width and height the size of the item
-- hp the "hitpoints"(health) it got
function createBox(x, y, width, height, mass, hp, graphic)
boxBody = love.physics.newBody( world_layer0, x, y, mass);
boxShape = love.physics.newRectangleShape(boxBody, width, height)
boxBody:setMassFromShapes();
bx,by= boxBody:getLocalCenter( );
boxBody:setMass(bx,by,mass,20000);
boxShape:setCategory(4);
boxShape:setRestitution(0);
boxShape:setFriction(3);
addEntity(boxBody,boxShape,"item", hp);
boxId = idOfLastCreatedEntity();
boxShape:setData(boxId);
table.insert(itemGraphic, boxId, graphic);
end
function createBall(x, y, radius, hp, graphic)
ballBody = love.physics.newBody( world_layer0, x, y );
ballShape = love.physics.newCircleShape(ballBody, radius);
ballBody:setMassFromShapes();
ballShape:setCategory(4);
ballShape:setRestitution(0.7);
addEntity(ballBody,ballShape,"item", hp);
ballId = idOfLastCreatedEntity();
ballShape:setData(ballId);
table.insert(itemGraphic, ballId, graphic);
end
function itemDraw(i)
-- Can't draw an item that has been destroyed(which it should have been if hitpoints drop below 1
if entityHitpoints[i] then
if itemGraphic[i] == "crate4848" then
love.graphics.draw(graphic_itemCrate4848, entityBody[i]:getX(), entityBody[i]:getY(), entityBody[i]:getAngle());
end
if itemGraphic[i] == "sofa" then
love.graphics.draw(graphic_itemSofa, entityBody[i]:getX(), entityBody[i]:getY(), entityBody[i]:getAngle());
end
if itemGraphic[i] == "basketball" then
love.graphics.draw(graphic_itemBasketball, entityBody[i]:getX(), entityBody[i]:getY(), entityBody[i]:getAngle());
end
if itemGraphic[i] == "anvil" then
love.graphics.draw(graphic_itemAnvil, entityBody[i]:getX(), entityBody[i]:getY(), entityBody[i]:getAngle());
end
end
end
function itemReceiveDmg(itemId, dmg)
if entityHitpoints[itemId] then
if dmg >= constItemDmgAbsorb then
entityHitpoints[itemId] = entityHitpoints[itemId] - dmg;
debugMsg("Item "..itemId.." lost "..dmg.." hps");
if entityHitpoints[itemId] <= 0 then
-- entityBody[itemId]:destroy();
-- entityShape[itemId]:destroy();
entityType[itemId]="destroyed";
entityBody[itemId]:setX(9999);
entityBody[itemId]:setY(9999);
debugMsg("Item "..itemId.." destroyed!");
if characterItemBeingHold == itemId then
characterReleaseItem();
end
-- itemsHitpoints is set to false!!! THIS NEED TO BE KNOWN!!! Bad solution perhaps?
entityHitpoints[itemId] = false;
end
end
end
end
function itemRegainHp(itemId, hp)
-- Bug, an item can regain more hp than it ever had
entityHitpoints[itemId] = entityHitpoints[itemId] + hp;
end
function createTestItems() -- used for testing purposes only
local test=40;
while test>0 do
local random=math.random(0,2);
if random == 0 then
createBox(test*200,700,48,48,100,5000,"crate4848");
elseif random == 1 then
-- createBox(test*200,700,35,11,200,5000,"sofa");
else
createBox(test*200,700,45,30,500,5000,"anvil");
end
test=test-1;
end
end
function itemCollision(a,b,c)
v = getVelocity(c);
a = tonumber(a);
power = getPower(v,entityBody[a]:getMass())
if power > constGamePowerAbsorb then
itemReceiveDmg(a,power);
end
end