-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.lua
114 lines (93 loc) · 3.26 KB
/
background.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
-- Copyright (C) 2023 Alessandro Amatucci Girlanda
-- This file is part of Invaders in Space.
-- Invaders in Space is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- Invaders in Space is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with Invaders in Space. If not, see <https://www.gnu.org/licenses/>.
Background = Object:extend()
function Background:new()
require("star")
self:loadPictures()
self.stars = {}
self.planets = {}
self.star_timer_max = 1
self.star_timer = 0
self:initStars()
end
function Background:update(dt)
-- Generate, move and delete stars (and planets) outside the window borders
self:generateStars(dt)
self:updateStars(dt)
self:removeStars()
end
function Background:draw()
self:drawStars()
end
function Background:titleDraw()
love.graphics.draw(title_pic, WINDOW_WIDTH_CENTER - title_pic:getWidth() / 2, 100)
end
function Background:loadPictures()
star_pic = love.graphics.newImage("pictures/star.png")
planets_pic = love.graphics.newImage("pictures/planets.png")
end
function Background:initStars()
-- Generate an Initial background
for i=1,20 do
local rand = math.random(1,100)
if (rand <= 80) then
table.insert(self.stars, Star(star_pic, math.random(0, WINDOW_WIDTH), math.random(0,WINDOW_HEIGHT)))
else
table.insert(self.planets, Star(planets_pic, math.random(0, WINDOW_WIDTH), math.random(0,WINDOW_HEIGHT)))
end
end
end
function Background:generateStars(dt)
if (self.star_timer >= self.star_timer_max) then
-- Create a random number and select whether generate a star or planet
local rand = math.random(1,100)
if (rand <= 80) then
table.insert(self.stars, Star(star_pic, math.random(0, WINDOW_WIDTH), WINDOW_HEIGHT))
else
table.insert(self.planets, Star(planets_pic, math.random(0, WINDOW_WIDTH), WINDOW_HEIGHT + PLANETS_HEIGHT))
end
-- Reset timer and randomize a new max
self.star_timer = 0
self.star_timer_max = math.random(1, 2)
else
self.star_timer = self.star_timer + dt
end
end
function Background:drawStars(dt)
for i,star in ipairs(self.stars) do
star:drawStar()
end
for i,planet in ipairs(self.planets) do
planet:drawPlanet()
end
end
function Background:updateStars(dt)
for i,star in ipairs(self.stars) do
star:update(dt)
end
for i,planet in ipairs(self.planets) do
planet:update(dt)
end
end
function Background:removeStars()
for i=#self.stars,1,-1 do
if (self.stars[i].y <= 0) then
table.remove(self.stars, i)
end
end
for i=#self.planets,1,-1 do
if (self.planets[i].y <= -PLANETS_HEIGHT) then
table.remove(self.planets, i)
end
end
end