aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/main.rs
blob: 15c08632a16cdc9a2883c908629567e5e76b0163 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
#![feature(path_add_extension, iterator_try_collect)]
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use config::Config;
use game::Game;
use hurrycurry_client_lib::network::sync::Network;
use hurrycurry_protocol::glam::Vec2;
use menu::{ingame::IngameMenu, main::MainMenu};
use profiler::ProfilerOverlay;
use render::Renderer;
use sdl2::{event::Event, keyboard::KeyboardState, mouse::MouseState, pixels::Color};
use std::time::{Duration, Instant};
use strings::set_language;

pub mod config;
pub mod game;
pub mod helper;
pub mod menu;
pub mod profiler;
pub mod render;
pub mod strings;
pub mod tilemap;
pub mod ui;

#[derive(Debug, Parser)]
pub struct Args {
    #[clap(subcommand)]
    action: Option<Action>,
}

#[derive(Debug, Subcommand, Default)]
pub enum Action {
    #[default]
    Menu,
    Join {
        #[arg(default_value = "ws://127.0.0.1/")]
        server_address: String,
    },
}

pub enum State {
    Ingame(IngameMenu),
    MainMenu(MainMenu),
    Quit,
}

fn main() -> Result<()> {
    env_logger::init_from_env("LOG");

    let args = Args::parse();
    let mut config = Config::load()?;
    set_language("de");

    rustls::crypto::ring::default_provider()
        .install_default()
        .expect("failed to initialize crypto things");

    let sdl_context = sdl2::init().map_err(|e| anyhow!("sdl2 init failed: {e}"))?;

    let video_subsystem = sdl_context
        .video()
        .map_err(|e| anyhow!("sdl2 video subsystem init failed: {e}"))?;
    let window = video_subsystem
        .window("Pixel Curry!", 1280, 720)
        .position_centered()
        .resizable()
        .build()
        .map_err(|e| anyhow!("sdl2 window creation failed: {e}"))?;

    let mut canvas = window
        .into_canvas()
        .accelerated()
        .present_vsync()
        .build()
        .map_err(|e| anyhow!("sdl2 canvas creation failed: {e}"))?;

    let texture_creator = canvas.texture_creator();

    let mut renderer = Renderer::init(&texture_creator);

    let mut state = match args.action.unwrap_or_default() {
        Action::Menu => State::MainMenu(MainMenu::new(renderer.atlas_layout())),
        Action::Join { server_address } => State::Ingame(IngameMenu::new(Game::new(
            Network::connect(&server_address)?,
            &config,
            renderer.atlas_layout(),
        ))),
    };

    let mut events = sdl_context
        .event_pump()
        .map_err(|e| anyhow!("sdl2 event pump: {e}"))?;
    let mut last_tick = Instant::now();
    let mut profiler = ProfilerOverlay::new();

    'mainloop: loop {
        let (width, height) = canvas
            .output_size()
            .map_err(|_| anyhow!("cannot get canvas size"))?;
        renderer.size = Vec2::new(width as f32, height as f32);

        let keyboard = KeyboardState::new(&events);
        let mouse = MouseState::new(&events);

        let actual_dt = last_tick.elapsed();
        last_tick += actual_dt;
        let dt = actual_dt.min(Duration::from_secs_f32(1. / 30.));

        let next = match &mut state {
            State::Ingame(x) => {
                x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout())
            }
            State::MainMenu(x) => {
                x.tick(dt.as_secs_f32(), &keyboard, &mouse, renderer.atlas_layout())
            }
            State::Quit => break Ok(()),
        };
        if let Some(next) = next {
            state = *next;
        }

        renderer.set_ui_view(4.);
        match &mut state {
            State::Ingame(x) => x.draw(&mut renderer, &mut config),
            State::MainMenu(x) => x.draw(&mut renderer, &mut config),
            State::Quit => (),
        }

        profiler.update(&mut renderer);
        canvas.set_draw_color(Color::BLACK);
        canvas.clear();
        renderer.submit(&mut canvas);
        canvas.present();

        for event in events.poll_iter() {
            match event {
                Event::Quit { .. } => break 'mainloop Ok(()),
                Event::KeyUp {
                    keycode: Some(keycode),
                    ..
                } => match &mut state {
                    State::Ingame(g) => g.keyboard_event(keycode, false),
                    State::MainMenu(g) => g.keyboard_event(keycode, false),
                    _ => (),
                },
                Event::KeyDown {
                    keycode: Some(keycode),
                    ..
                } => match &mut state {
                    State::Ingame(g) => g.keyboard_event(keycode, true),
                    State::MainMenu(g) => g.keyboard_event(keycode, true),
                    _ => (),
                },
                Event::TextInput { text, .. } => match &mut state {
                    State::Ingame(g) => g.ui_state.text_input(text),
                    State::MainMenu(g) => g.ui_state.text_input(text),
                    _ => (),
                },
                _ => {}
            }
        }
    }
}