2026-02-10 16:39:21 +01:00
|
|
|
package linea;
|
|
|
|
|
|
|
|
|
|
import java.awt.BasicStroke;
|
|
|
|
|
import java.awt.Color;
|
|
|
|
|
import java.awt.Graphics;
|
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
public class Ligne extends ObjetGraphique{
|
|
|
|
|
|
2026-03-28 08:16:44 +01:00
|
|
|
private int nbSegments = 4000;
|
2026-02-10 16:39:21 +01:00
|
|
|
private double xCercle = 400;
|
|
|
|
|
private Segment SegCourant;
|
2026-02-23 11:10:15 +01:00
|
|
|
// vitesse de déplacement (augmente légèrement chaque frame)
|
2026-03-28 14:18:17 +01:00
|
|
|
private double vitesse = 4.8;
|
2026-02-23 11:10:15 +01:00
|
|
|
// croissance initiale (fractionnelle) appliquée chaque frame
|
2026-03-28 14:18:17 +01:00
|
|
|
private double croissance = 0.0008; // montée plus progressive
|
2026-02-23 11:10:15 +01:00
|
|
|
// facteur qui amplifie la croissance elle-même (pour accélérer la montée)
|
2026-03-28 14:18:17 +01:00
|
|
|
private double facteurCroissance = 1.00002; // accélération plus douce
|
2026-02-10 16:39:21 +01:00
|
|
|
|
2026-03-16 15:36:24 +01:00
|
|
|
private int niveau = 1; // niveau de difficulté, à augmenter pour rendre le jeu plus difficile
|
|
|
|
|
|
2026-02-10 16:39:21 +01:00
|
|
|
// liste des segments
|
|
|
|
|
private ArrayList<Segment> segments = new ArrayList<Segment>();
|
|
|
|
|
|
2026-03-16 15:36:24 +01:00
|
|
|
// écran fixe actuellement 800x600 (voir ZoneDessin)
|
|
|
|
|
private static final int SCREEN_WIDTH = 800;
|
|
|
|
|
private static final int SCREEN_HEIGHT = 600;
|
|
|
|
|
private static final int VERTICAL_MARGIN = 20; // éviter de toucher les bords
|
|
|
|
|
|
|
|
|
|
public Ligne(int _niveaux){
|
|
|
|
|
niveau = _niveaux;
|
|
|
|
|
|
|
|
|
|
double x = SCREEN_WIDTH; // commence au bord droit
|
|
|
|
|
double y = SCREEN_HEIGHT/2.0; // milieu vertical
|
2026-02-10 16:39:21 +01:00
|
|
|
double dx,dy;
|
|
|
|
|
|
2026-03-16 15:36:24 +01:00
|
|
|
nbSegments += niveau * 50; // augmenter le nombre de segments avec les niveaux
|
|
|
|
|
|
2026-02-10 16:39:21 +01:00
|
|
|
Segment s;
|
|
|
|
|
for (int i=0; i<nbSegments; i++){
|
|
|
|
|
dx = Math.random()*20+80;
|
2026-03-16 15:36:24 +01:00
|
|
|
// amplitude verticale selon le niveau, mais on réduit l'impact
|
|
|
|
|
double baseAmp = 20;
|
|
|
|
|
double levelFactor = 10; // coeff pour monter la difficulté
|
|
|
|
|
double amplitude = baseAmp + niveau * levelFactor;
|
2026-03-28 17:19:34 +01:00
|
|
|
double centreY = SCREEN_HEIGHT / 2.0;
|
2026-03-16 15:36:24 +01:00
|
|
|
|
|
|
|
|
// pente maximale par segment pour rester jouable
|
|
|
|
|
double maxStep = 40;
|
|
|
|
|
double rawDy = (Math.random()*2 - 1) * amplitude;
|
2026-03-28 17:19:34 +01:00
|
|
|
|
|
|
|
|
// Biais de recentrage: plus on s'éloigne du milieu, plus on est ramené vers le centre.
|
|
|
|
|
double ecartCentre = centreY - y;
|
|
|
|
|
rawDy += ecartCentre * 0.14;
|
|
|
|
|
|
|
|
|
|
// Si on est très proche des bords, renforcer le retour vers le milieu.
|
|
|
|
|
if (y < VERTICAL_MARGIN + 45 || y > SCREEN_HEIGHT - VERTICAL_MARGIN - 45) {
|
|
|
|
|
rawDy += Math.signum(ecartCentre) * 14.0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:36:24 +01:00
|
|
|
// limiter la variation brute
|
|
|
|
|
if (rawDy > maxStep) rawDy = maxStep;
|
|
|
|
|
if (rawDy < -maxStep) rawDy = -maxStep;
|
|
|
|
|
|
|
|
|
|
double tentativeY = y + rawDy;
|
|
|
|
|
// si on dépasserait les marges, repositionner vers l'intérieur
|
|
|
|
|
if (tentativeY < VERTICAL_MARGIN) {
|
|
|
|
|
tentativeY = VERTICAL_MARGIN + Math.random()*10;
|
|
|
|
|
} else if (tentativeY > SCREEN_HEIGHT - VERTICAL_MARGIN) {
|
|
|
|
|
tentativeY = SCREEN_HEIGHT - VERTICAL_MARGIN - Math.random()*10;
|
|
|
|
|
}
|
|
|
|
|
dy = tentativeY - y;
|
|
|
|
|
// éviter pente nulle (ligne plate) en forçant un petit mouvement
|
|
|
|
|
if (Math.abs(dy) < 1.0) {
|
|
|
|
|
dy = (rawDy < 0 ? -1.0 : 1.0);
|
|
|
|
|
tentativeY = y + dy;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 16:39:21 +01:00
|
|
|
s = new Segment(x,y,dx,dy);
|
|
|
|
|
s.setCouleur(new Color(0.2f,0.2f,0.2f));
|
2026-03-16 15:36:24 +01:00
|
|
|
|
2026-02-10 16:39:21 +01:00
|
|
|
segments.add(s);
|
2026-03-16 15:36:24 +01:00
|
|
|
|
|
|
|
|
x += dx;
|
|
|
|
|
y = tentativeY;
|
2026-02-10 16:39:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void Afficher(Graphics g){
|
|
|
|
|
Graphics2D g2D = (Graphics2D) g;
|
|
|
|
|
g2D.setStroke(new BasicStroke(3.0f));
|
|
|
|
|
|
|
|
|
|
for (Segment seg : segments) {
|
|
|
|
|
seg.Afficher(g);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void Animer() {
|
|
|
|
|
// déplace tous les segments vers la gauche
|
2026-02-23 11:10:15 +01:00
|
|
|
double delta = vitesse; // vitesse de déplacement (exponentielle)
|
2026-02-10 16:39:21 +01:00
|
|
|
for (Segment seg : segments) {
|
|
|
|
|
seg.x -= delta;
|
|
|
|
|
}
|
2026-02-23 11:10:15 +01:00
|
|
|
|
|
|
|
|
// appliquer la croissance (vitesse *= 1 + croissance)
|
2026-03-28 14:18:17 +01:00
|
|
|
vitesse *= (1.0 + croissance + (niveau * 0.000006)); // hausse niveau plus progressive
|
2026-02-23 11:10:15 +01:00
|
|
|
// augmenter légèrement la croissance pour que l'accélération s'amplifie
|
|
|
|
|
croissance *= facteurCroissance;
|
2026-02-23 09:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-03-28 14:18:17 +01:00
|
|
|
// Trouve le segment couvrant l'abscisse cx (une seule itération)
|
|
|
|
|
private Segment trouverSegmentAuX(double cx) {
|
2026-02-23 09:41:54 +01:00
|
|
|
for (Segment seg : segments) {
|
|
|
|
|
double x1 = seg.x;
|
|
|
|
|
double x2 = seg.x + seg.xLong;
|
2026-03-28 14:18:17 +01:00
|
|
|
if (cx >= Math.min(x1, x2) && cx <= Math.max(x1, x2)) {
|
|
|
|
|
return seg;
|
2026-02-23 09:41:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-28 14:18:17 +01:00
|
|
|
return null;
|
2026-02-23 09:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 14:18:17 +01:00
|
|
|
// Résultat de contact : null = pas sur la ligne, sinon double[]{ligneY, distance}
|
|
|
|
|
public double[] contactInfo(double cx, double cy, double rayon) {
|
|
|
|
|
Segment seg = trouverSegmentAuX(cx);
|
|
|
|
|
if (seg == null) return null;
|
|
|
|
|
double ligneY = (seg.xLong != 0) ? seg.y + ((cx - seg.x) / seg.xLong) * seg.yLong : seg.y;
|
|
|
|
|
double dist = pointSegmentDistance(cx, cy, seg.x, seg.y, seg.x + seg.xLong, seg.y + seg.yLong);
|
|
|
|
|
return new double[]{ligneY, dist};
|
|
|
|
|
}
|
2026-02-23 09:41:54 +01:00
|
|
|
|
2026-03-28 14:18:17 +01:00
|
|
|
// Indique si l'axe horizontal du cercle (cx) se trouve au niveau
|
|
|
|
|
// d'un des segments de la ligne -> le cercle est "sur la ligne"
|
|
|
|
|
public boolean estSurLaLigne(double cx) {
|
|
|
|
|
return trouverSegmentAuX(cx) != null;
|
|
|
|
|
}
|
2026-02-23 09:41:54 +01:00
|
|
|
|
2026-03-28 14:18:17 +01:00
|
|
|
// Vérifie la collision entre la ligne (segments) et un cercle
|
|
|
|
|
public boolean collisionAvec(Cercle c) {
|
|
|
|
|
double[] info = contactInfo(c.getX(), c.getY(), c.getRayon());
|
|
|
|
|
return info != null && info[1] <= c.getRayon();
|
2026-02-23 09:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-28 14:18:17 +01:00
|
|
|
// Retourne la coordonnée Y de la ligne à l'abscisse cx
|
|
|
|
|
public double getYAuX(double cx) {
|
|
|
|
|
Segment seg = trouverSegmentAuX(cx);
|
|
|
|
|
if (seg == null || seg.xLong == 0) return 300;
|
|
|
|
|
return seg.y + ((cx - seg.x) / seg.xLong) * seg.yLong;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 08:16:44 +01:00
|
|
|
// Obtenir la vitesse actuelle de la ligne (pour les boules bonus)
|
|
|
|
|
public double getVitesse() {
|
|
|
|
|
return vitesse;
|
|
|
|
|
}
|
2026-03-28 14:18:17 +01:00
|
|
|
|
|
|
|
|
public void setCouleurLigne(Color couleur) {
|
|
|
|
|
for (Segment seg : segments) {
|
|
|
|
|
seg.setCouleur(couleur);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 09:41:54 +01:00
|
|
|
// distance minimale entre un point (px,py) et un segment (x1,y1)-(x2,y2)
|
|
|
|
|
private double pointSegmentDistance(double px, double py, double x1, double y1, double x2, double y2) {
|
|
|
|
|
double vx = x2 - x1;
|
|
|
|
|
double vy = y2 - y1;
|
|
|
|
|
double wx = px - x1;
|
|
|
|
|
double wy = py - y1;
|
|
|
|
|
double c = vx*vx + vy*vy;
|
|
|
|
|
if (c == 0) {
|
|
|
|
|
return Math.hypot(px - x1, py - y1);
|
2026-02-10 16:39:21 +01:00
|
|
|
}
|
2026-02-23 09:41:54 +01:00
|
|
|
double t = (vx*wx + vy*wy) / c;
|
|
|
|
|
if (t < 0) t = 0;
|
|
|
|
|
else if (t > 1) t = 1;
|
|
|
|
|
double projx = x1 + t * vx;
|
|
|
|
|
double projy = y1 + t * vy;
|
|
|
|
|
return Math.hypot(px - projx, py - projy);
|
2026-02-10 16:39:21 +01:00
|
|
|
}
|
2026-02-23 09:41:54 +01:00
|
|
|
}
|