2021-06-24 15:50:47 +02:00
|
|
|
// Copyright 2021 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2022-10-21 08:23:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
2021-06-24 15:50:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
scale = 64
|
2022-07-12 18:55:03 +02:00
|
|
|
starsCount = 1024
|
2021-06-24 15:50:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Star struct {
|
2022-10-21 08:23:09 +02:00
|
|
|
fromx, fromy, tox, toy, brightness float32
|
2021-06-24 15:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Star) Init() {
|
2022-10-21 08:23:09 +02:00
|
|
|
s.tox = rand.Float32() * screenWidth * scale
|
2021-06-24 15:50:47 +02:00
|
|
|
s.fromx = s.tox
|
2022-10-21 08:23:09 +02:00
|
|
|
s.toy = rand.Float32() * screenHeight * scale
|
2021-06-24 15:50:47 +02:00
|
|
|
s.fromy = s.toy
|
2022-10-21 08:23:09 +02:00
|
|
|
s.brightness = rand.Float32() * 0xff
|
2021-06-24 15:50:47 +02:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:23:09 +02:00
|
|
|
func (s *Star) Update(x, y float32) {
|
2021-06-24 15:50:47 +02:00
|
|
|
s.fromx = s.tox
|
|
|
|
s.fromy = s.toy
|
|
|
|
s.tox += (s.tox - x) / 32
|
|
|
|
s.toy += (s.toy - y) / 32
|
|
|
|
s.brightness += 1
|
|
|
|
if 0xff < s.brightness {
|
|
|
|
s.brightness = 0xff
|
|
|
|
}
|
|
|
|
if s.fromx < 0 || screenWidth*scale < s.fromx || s.fromy < 0 || screenHeight*scale < s.fromy {
|
|
|
|
s.Init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Star) Draw(screen *ebiten.Image) {
|
2022-10-21 08:23:09 +02:00
|
|
|
c := color.RGBA{
|
|
|
|
R: uint8(0xbb * s.brightness / 0xff),
|
2022-04-13 11:42:04 +02:00
|
|
|
G: uint8(0xdd * s.brightness / 0xff),
|
|
|
|
B: uint8(0xff * s.brightness / 0xff),
|
|
|
|
A: 0xff}
|
2022-10-21 08:23:09 +02:00
|
|
|
vector.StrokeLine(screen, s.fromx/scale, s.fromy/scale, s.tox/scale, s.toy/scale, 1, c)
|
2021-06-24 15:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
2022-07-12 18:55:03 +02:00
|
|
|
stars [starsCount]Star
|
2021-06-24 15:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGame() *Game {
|
|
|
|
g := &Game{}
|
2022-07-12 18:55:03 +02:00
|
|
|
for i := 0; i < starsCount; i++ {
|
2021-06-24 15:50:47 +02:00
|
|
|
g.stars[i].Init()
|
|
|
|
}
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
x, y := ebiten.CursorPosition()
|
2022-07-12 18:55:03 +02:00
|
|
|
for i := 0; i < starsCount; i++ {
|
2022-10-21 08:23:09 +02:00
|
|
|
g.stars[i].Update(float32(x*scale), float32(y*scale))
|
2021-06-24 15:50:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2022-07-12 18:55:03 +02:00
|
|
|
for i := 0; i < starsCount; i++ {
|
2021-06-24 15:50:47 +02:00
|
|
|
g.stars[i].Draw(screen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2021-09-23 18:55:12 +02:00
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Stars (Ebitengine Demo)")
|
2021-06-24 15:50:47 +02:00
|
|
|
if err := ebiten.RunGame(NewGame()); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|