summaryrefslogtreecommitdiff
path: root/Bolvis.lua
blob: 8d53c4441fd9cc32a7dfa85b4a33374f01d958c7 (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
local component = require 'component'
local Drawable = require 'Drawable'
local Input = require 'Input'
local Sprite = require 'Sprite'
local Transform = require 'Transform'
local Camera = require 'Camera'

local Bolvis = component(
	{Drawable, {z = 50}},
	{Input},
	{Sprite, {name = "bolvis"}}
)

local Hand = component(
	{Drawable, {z = 49}},
	{Input},
	{Sprite, {name = "hand", z = 1}}
)

function Bolvis:init(inst)
	inst.hand = self.obj.world:object(Hand {bolvis = self.obj})
	inst.camera = self.obj.world:object(Camera {following = self.obj})
	inst.blink = 10
end

function Bolvis:update(dt)
	local sprite = self.obj:get(Sprite)
	self.i.blink = self.i.blink - (dt * 30)
	if self.i.blink <= 0 then
		if sprite.i.state == "blink" then
			sprite.i.state = "bolvis"
			self.i.blink = math.random(30, 256)
		else
			sprite.i.state = "blink"
			self.i.blink = 4
		end
	end
--	if self.sprite ~= self.sprite_blink then
--		if self.hand.grab_joint then
--			local offsx = self.hand:present_offset()
--			if offsx > 150 then
--				self.sprite = self.sprite_right
--			elseif offsx < -150 then
--				self.sprite = self.sprite_left
--			else
--				self.sprite = self.sprite_center
--			end
--		elseif self.sprite ~= self.sprite_center then
--			if math.random(1, 4) == 1 then
--				self.sprite = self.sprite_blink
--				self.blink = 4
--			else
--				self.sprite = self.sprite_center
--			end
--		end
--	end
end

function Hand:init(inst)
	assert(inst.bolvis)
	inst.offset = {100, -100}
end

local function clamp(n, max, min)
	return math.max(math.min(n, max), min)
end

function Hand:update()
	local this = self.obj:get(Transform)
	local bolvis = self.i.bolvis:get(Transform)
	this.i.angle = self:angle()
	if not self.i.grab then
		this.i.pos[1] = bolvis.i.pos[1] + self.i.offset[1]
		this.i.pos[2] = bolvis.i.pos[2] + self.i.offset[2]
	else
		-- self.pos = {self.grabbed.body:getWorldPoint(unpack(self.grab_pos))}
	end
end

function Hand:present_offset()
	local x, y = unpack(self.obj:get(Transform).i.pos)
	local bx, by = unpack(self.i.bolvis:get(Transform).i.pos)
	return x - bx, y - by
end

function Hand:angle()
	local ox, oy = self:present_offset()
	local a = math.atan(oy / math.abs(ox)) + math.pi / 2
	if ox < 0 then a = -a end
	return a
end


function Hand:mousemoved(_, _, dx, dy)
	self.i.offset[1] = clamp(self.i.offset[1] + dx, 1920 / 2, -1920 / 2)
	self.i.offset[2] = clamp(self.i.offset[2] + dy, 1080 / 2, -1080 / 2)
	--if self.grab_joint then
	--	local x, y = unpack(self.offset)
	--	self.grab_joint:setLinearOffset(x + self.grab_pos[1], y + self.grab_pos[2])
	--end
end

function Hand:mousepressed(_, _, button)
	if button == 1 then
		self.obj:get(Sprite).i.state = "closed"
--		local grabbed
--		for o in pairs(game.all_objects) do
--			if o.fixture then
--				if o.fixture:testPoint(unpack(self.pos)) then
--					if o ~= self.bolvis then
--						self.grabbed = o
--						break
--					end
--				end
--			end
--		end
--		if self.grabbed then
--			local x1, y1 = unpack(self.bolvis.pos)
--			local x2, y2 = unpack(self.bolvis.pos)
--			self.grab_joint = love.physics.newMotorJoint(
--				self.bolvis.body, self.grabbed.body, 0.5, true)
--			self.grab_joint:setMaxForce(500)
--			self.grab_pos = {self.grabbed.body:getLocalPoint(unpack(self.pos))}
--		end
	end
end

function Hand:mousereleased(_, _, button)
	if button == 1 then
		self.obj:get(Sprite).i.state = "hand"
--		if self.grab_joint then
--			self.grabbed = nil
--			self.grab_joint:destroy()
--			self.grab_joint = nil
--		end
--		self.offset = {self:present_offset()}
	end
end

function Hand:draw()
	local x1, y1 = unpack(self.i.bolvis[Transform].i.pos)
	local x2, y2 = unpack(self.obj[Transform].i.pos)
	love.graphics.setColor(0.73, 0.76, 0.91)
	love.graphics.setLineWidth(15)
	love.graphics.line(x1, y1, x2, y2)
	love.graphics.setColor(1, 1, 1)
end

return Bolvis